l__('Database error. Please contact your developer.', 'woo-product-filter')); } return false; } public function insert( $data ) { return $this->store($data); } public function update( $data, $where ) { if (is_numeric($where)) { $where = array($this->_id => $where); } return $this->store($data, 'UPDATE', $where); } public function alias( $alias = null ) { if (!is_null($alias)) { $this->_alias = $alias; } return $this->_alias; } /** * Delete record(s) * * @param mixed $where condition to use in query, if numeric givven - use delete by ID column * @return query result */ public function delete( $where = '' ) { if ($where) { $q = 'DELETE FROM ' . $this->_table; if (is_numeric($where)) { $where = array($this->_id => $where); } $q .= ' WHERE ' . $this->_getQueryString($where, 'AND'); } else { $q = 'TRUNCATE TABLE ' . $this->_table; } return DbWpf::query($q); } /** * Convert to database query * * @param mixed $data if array given - convert it into string where key - is column name, value - database value to set; * if key == "additionalCondition" then we will just add value to string * if string givven - just return it without changes * @param string $delim delimiter to use in query, recommended - ',', 'AND', 'OR' * @return string query string */ public function _getQueryString( $data, $delim = ',', $validate = false ) { $res = ''; if (is_array($data) && !empty($data)) { foreach ($data as $k => $v) { if (array_key_exists($k, $this->_fields) || $k == $this->_id) { $val = $v; if (isset($this->_fields[$k]) && $this->_fields[$k]->adapt['dbTo']) { $val = FieldAdapterWpf::_($val, $this->_fields[$k]->adapt['dbTo'], FieldAdapterWpf::DB); } if ($validate) { if (isset($this->_fields[$k]) && is_object($this->_fields[$k])) { $objForValidation = clone $this->_fields[$k]; $objForValidation->setValue($val); $errors = ValidatorWpf::_($objForValidation); if ($errors) { $this->_addError($errors); } } } if (isset($this->_fields[$k])) { switch ($this->_fields[$k]->type) { case 'int': case 'tinyint': $res .= $k . ' = ' . ( (int) $val ) . ' ' . $delim . ' '; break; case 'float': $res .= $k . ' = ' . ( (float) $val ) . ' ' . $delim . ' '; break; case 'decimal': $res .= $k . ' = ' . ( (float) $val ) . ' ' . $delim . ' '; break; case 'free': //Just set it as it is $res .= $k . ' = ' . $val . ' ' . $delim . ' '; break; default: $res .= $k . ' = \'' . $val . '\' ' . $delim . ' '; break; } } else { $res .= $k . ' = \'' . $val . '\' ' . $delim . ' '; } } elseif ('additionalCondition' == $k) { //just add some string to query $res .= $v . ' ' . $delim . ' '; } } $res = substr($res, 0, -( strlen($delim) + 1 )); } elseif (is_string($data)) { $res = $data; } return $res; } /** * Add new FieldWpfWpf for children table (@see class field) * * @param string $name name of a field * @param string $html html type of field (text, textarea, etc. @see html class) * @param string $type database type (int, varcahr, etc.) * @param mixed $default default value for this field * @return object $this - pointer to current object */ protected function _addField( $name, $html = 'text', $type = 'other', $default = '', $label = '', $maxlen = 0, $dbAdapt = '', $htmlAdapt = '', $description = '' ) { $this->_fields[$name] = toeCreateObjWpf('FieldWpf', array($name, $html, $type, $default, $label, $maxlen, $dbAdapt, $htmlAdapt, $description)); return $this; } /** * Public alias for _addField() method */ public function addField() { $args = func_get_args(); return call_user_func_array(array($this, '_addField'), $args); } public function getFields() { return $this->_fields; } public function getField( $name ) { return $this->_fields[$name]; } public function exists( $value, $field = '' ) { if (!$field) { $field = $this->_id; } return DbWpf::get('SELECT ' . $this->_id . ' FROM ' . $this->_table . ' WHERE ' . $field . ' = "' . $value . '"', 'one'); } protected function _addError( $error ) { if (is_array($error)) { $this->_errors = array_merge($this->_errors, $error); } else { $this->_errors[] = $error; } } public function getErrors() { return $this->_errors; } protected function _clearErrors() { $this->_errors = array(); } /** * Prepare data before send it to database */ public function prepareInput( $d = array() ) { $ignore = isset($d['ignore']) ? $d['ignore'] : array(); foreach ($this->_fields as $key => $f) { if ('tinyint' == $f->type) { if ('true' == $d[$key]) { $d[$key] = 1; } if (empty($d[$key]) && !in_array($key, $ignore)) { $d[$key] = 0; } } if ('date' == $f->type) { if (empty($d[$key]) && !in_array($key, $ignore)) { $d[$key] = '0000-00-00'; } elseif (!empty($d[$key])) { $d[$key] = DbWpf::timeToDate($d[$key]); } } } $d[$this->_id] = isset($d[$this->_id]) ? intval($d[$this->_id]) : 0; return $d; } /** * Prepare data after extracting it from database */ public function prepareOutput( $d = array() ) { $ignore = isset($d['ignore']) ? $d['ignore'] : array(); foreach ($this->_fields as $key => $f) { switch ($f->type) { case 'date': if ('0000-00-00' == $d[$key] || empty($d[$key])) { $d[$key] = ''; } else { $d[$key] = gmdate(WPF_DATE_FORMAT, DbWpf::dateToTime($d[$key])); } break; case 'int': case 'tinyint': if ('true' == $d[$key]) { $d[$key] = 1; } if ('false' == $d[$key]) { $d[$key] = 0; } $d[$key] = (int) $d[$key]; break; } } $d[$this->_id] = isset($d[$this->_id]) ? intval($d[$this->_id]) : 0; return $d; } public function install( $d = array() ) { } public function uninstall( $d = array() ) { } public function activate() { } public function getLastInsertID() { return DbWpf::get('SELECT MAX(' . $this->_id . ') FROM ' . $this->_table, 'one'); } public function adaptHtml( $val ) { return htmlspecialchars($val, ENT_COMPAT); } }