public function GetFilterString($uid, &$args, $fieldNameOverride = NULL, $fieldTypeOverride = NULL) { //,$filterSection) { $filterData = $this->GetFilterInfo($uid); if (!is_array($filterData)) { return ''; } $fieldName = $fieldNameOverride ? $fieldNameOverride : $filterData['fieldName']; $compareType = $filterData['ct']; $inputType = $filterData['it']; if ($compareType === ctIGNORE) { return ''; } $value = $this->GetFilterValue($uid); //$filterData['value']; $fieldToCompare = $fieldName; if (is_array($fieldToCompare) && is_callable($fieldToCompare)) { return $this->ReplaceFields(call_user_func_array($fieldToCompare, array($value, &$args))); } if ($filterData['type'] == FILTER_WHERE) { $fieldToCompare = $this->ReplaceFields($fieldToCompare); } //echo "$uid::$value<br/>"; if ($value === NULL && $compareType !== ctCUSTOM && (!isset($filterData['linkFrom']) || !$filterData['linkFrom'] || !isset($_REQUEST['_f_' . $uid]))) { return ''; } // set filter VALUE if ($compareType === ctLIKE && strpos($value, '%') === FALSE) { $value = "%{$value}%"; } // find field type from tabledef // set filter NAME // if where, ignore type $fieldName = $this->FormatFieldName($fieldName, $fieldTypeOverride); // do value switch (true) { case $compareType == ctMATCH: // any fields $args = array_merge($args, (array) $value); return 'MATCH (' . $fieldToCompare . ') AGAINST (?)'; break; case $compareType == ctMATCHBOOLEAN: // any fields $args = array_merge($args, (array) $value); return 'MATCH (' . $fieldToCompare . ') AGAINST (? IN BOOLEAN MODE)'; break; case $compareType == ctCUSTOM: if ($count = preg_match_all('/(?<!\\\\)\\?/', $fieldToCompare, $_)) { // has unescaped slashes, add values to args $value = (array) $value; $vcount = count($value); if ($count === 1 && $vcount > 1) { // if count ==1 and count($value) > 1, repeat fTC and values for count(value) $f = array(); for ($i = 0; $i < $vcount; $i++) { $f[] = $fieldToCompare; } $fieldToCompare = '(' . implode(' OR ', $f) . ')'; } else { if ($count > 1 && $vcount === 1) { // if count >1 and count(value) 1, repeat value in args $v = array(); for ($i = 0; $i < $count; $i++) { $v[] = $value[0]; } $value = $v; } } $args = array_merge($args, $value); } return $fieldToCompare; break; case $compareType == ctANY: $constants = get_defined_constants(true); foreach ($constants['user'] as $cName => $cVal) { if (strtolower(substr($cName, 0, 2)) == 'ct' && stripos($value, $cVal) !== FALSE) { $val = $value; break; } } if (!$val) { $val = "= ?"; $args[] = $value; } break; case $compareType == ctIS: case $compareType == ctISNOT: $val = $value; break; case $compareType == ctIN: if (IsSelectStatement($fieldName)) { return trim("{$val} {$compareType} {$fieldName}"); } $vals = $value; if (!is_array($vals)) { $vals = explode(',', $vals); } $args = array_merge($args, $vals); foreach ($vals as $k => $v) { $vals[$k] = '?'; } $val = '(' . join(',', $vals) . ')'; break; // convert dates to mysql version for filter // convert dates to mysql version for filter case $inputType == itDATE: $value = strftime(FORMAT_DATE, utopia::strtotime($value)); $val = "(STR_TO_DATE(?, '" . FORMAT_DATE . "'))"; $args[] = $value; break; case $inputType == itTIME: $value = strftime(FORMAT_TIME, utopia::strtotime($value)); $val = "(STR_TO_DATE(?, '" . FORMAT_TIME . "'))"; $args[] = $value; break; case $inputType == itDATETIME: $value = strftime(FORMAT_DATETIME, utopia::strtotime($value)); $val = "(STR_TO_DATE(?, '" . FORMAT_DATETIME . "'))"; $args[] = $value; break; default: $val = '?'; $args[] = $value; break; } // $fieldToCompare = $fieldToCompare ? $fieldToCompare : $fieldName; return trim("{$fieldToCompare} {$compareType} {$val}"); }
public function UpdateField($fieldName, $newValue, &$pkVal = NULL, $fieldType = NULL) { $this->AssertTable(); uEvents::TriggerEvent('BeforeUpdateField', $this, array($fieldName, $newValue, &$pkVal, $fieldType)); //AjaxEcho('//'.str_replace("\n",'',get_class($this)."@UpdateField($fieldName,,$pkVal)\n")); if ($fieldType === NULL) { $fieldType = $this->fields[$fieldName]['type']; } if (is_array($newValue)) { $newValue = json_encode($newValue); } if ($newValue) { switch ($fieldType) { case ftRAW: break; case ftDATE: case ftTIME: case ftDATETIME: // datetime // datetime case ftTIMESTAMP: $parsed = utopia::strtotime($newValue); $newValue = $newValue == '' ? 'NULL' : date('Y-m-d H:i:s', $parsed); break; case ftFLOAT: // float // float case ftDECIMAL: $l = setlocale(LC_ALL, 'en_US'); $newValue = floatval($newValue); setlocale(LC_ALL, $l); break; case ftBOOL: // bool // bool case ftPERCENT: // percent // percent case ftCURRENCY: // currency // currency case ftNUMBER: $newValue = $newValue === '' ? '' : preg_replace('/[^0-9\\.-]/', '', $newValue); break; } } if (($newValue === '' || $newValue === NULL) && $this->GetFieldProperty($fieldName, 'null') !== SQL_NOT_NULL) { $newValue = NULL; } $updateQry = array(); $raw = $fieldType == ftRAW; $args = array(); if ($pkVal === NULL) { $query = 'INSERT INTO ' . $this->tablename . ' (`' . $fieldName . '`) VALUES (' . ($raw ? $newValue : '?') . ')'; if (!$raw) { $args[] = $newValue; } } else { $query = 'UPDATE ' . $this->tablename . ' SET `' . $fieldName . '` = ' . ($raw ? $newValue : '?') . ' WHERE `' . $this->GetPrimaryKey() . '` = ?'; if (!$raw) { $args[] = $newValue; } $args[] = $pkVal; } database::query($query, $args); if ($fieldName == $this->GetPrimaryKey() && $newValue !== NULL) { // this allows us to get the real evaluated value of the new primary key $stm = database::query('SELECT ? AS new_pk', array($newValue)); $row = $stm->fetch(); $pkVal = $row['new_pk']; } elseif ($pkVal === NULL) { $pkVal = database::connect()->lastInsertId(); } uEvents::TriggerEvent('AfterUpdateField', $this, array($fieldName, $newValue, &$pkVal, $fieldType)); }