/** * Creates a searchcondition for the field, * was once part of searchCondition, however, * searchcondition() also immediately adds the search condition. * * @param Query $query The query object where the search condition should be placed on * @param string $table The name of the table in which this attribute * is stored * @param mixed $value The value the user has entered in the searchbox * @param string $searchmode The searchmode to use. This can be any one * of the supported modes, as returned by this * attribute's getSearchModes() method. * @param string $fieldname * * @return string The searchcondition to use. */ public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '') { if ($this->createDestination() && is_array($value)) { // we are a relation, so instead of hooking ourselves into the // query, hook the attributes in the destination node onto the query foreach ($value as $key => $val) { // if we aren't searching for anything in this field, there is no need // to look any further: if ($val === '' || $val === null) { continue; } $p_attrib = $this->m_destInstance->m_attribList[$key]; if (is_object($p_attrib)) { if ($this->m_refKey && $this->createDestination()) { // master mode $new_table = $this->fieldName(); } else { // slave mode $new_table = $this->m_destInstance->m_table; // we need to left join the destination table into the query // (don't worry ATK won't add it when it's already there) $query->addJoin($new_table, $new_table, $this->getJoinCondition($query), false); } $p_attrib->searchCondition($query, $new_table, $val, $this->getChildSearchMode($searchmode, $p_attrib->fieldName())); } else { // attribute not found in destination, so it should // be in the owner (this is the case when extra fields // are in the relation $p_attrib = $this->m_ownerInstance->m_attribList[$key]; if (is_object($p_attrib)) { $p_attrib->searchCondition($query, $p_attrib->getOwnerInstance()->getTable(), $val, $this->getChildSearchMode($searchmode, $p_attrib->fieldName())); } else { Tools::atkdebug("Field {$key} was not found in this relation (this is very weird)"); } } } } else { // we were passed a value that is not an array, so appearantly the function calling us // does not know we are a relation, not just another attrib // so we assume that it is looking for something in the descriptor def of the destination if ($this->createDestination()) { $descfields = $this->m_destInstance->descriptorFields(); foreach ($descfields as $key) { $p_attrib = $this->m_destInstance->m_attribList[$key]; if (is_object($p_attrib)) { if ($this->m_refKey && $this->createDestination()) { // master mode $new_table = $this->fieldName(); } else { // slave mode $new_table = $this->m_destInstance->m_table; // we need to left join the destination table into the query // (don't worry ATK won't add it when it's already there) $query->addJoin($new_table, $new_table, $this->getJoinCondition($query), false); } $p_attrib->searchCondition($query, $new_table, $value, $searchmode); } } } } }
/** * Transform raw database row to node compatible row. * * @param array $row raw database row * @param Query $query query object * @param array $attrsByLoadType attributes by load type * * @return array node compatible row */ protected function _transformRow($row, Query $query, array $attrsByLoadType) { $query->deAlias($row); Tools::atkDataDecode($row); $result = []; foreach ($attrsByLoadType[Attribute::ADDTOQUERY] as $attr) { $result[$attr->fieldName()] = $attr->db2value($row); } if (!$this->m_ignorePrimaryKey) { $result['atkprimkey'] = $this->_getNode()->primaryKey($result); } foreach ($attrsByLoadType[Attribute::POSTLOAD] as $attr) { $result[$attr->fieldName()] = $attr->load($this->_getDb(), $result, $this->m_mode); } return $result; }
/** * Creates a smart search condition for a given search value, and adds it * to the query that will be used for performing the actual search. * * @param int $id The unique smart search criterium identifier. * @param int $nr The element number in the path. * @param array $path The remaining attribute path. * @param Query $query The query to which the condition will be added. * @param string $ownerAlias The owner table alias to use. * @param mixed $value The value the user has entered in the searchbox. * @param string $mode The searchmode to use. */ public function smartSearchCondition($id, $nr, $path, $query, $ownerAlias, $value, $mode) { // one-to-many join means we need to perform a distinct select $query->setDistinct(true); if (count($path) > 0) { $this->createDestination(); $destAlias = "ss_{$id}_{$nr}_" . $this->fieldName(); $query->addJoin($this->m_destInstance->m_table, $destAlias, $this->getJoinCondition($query, $ownerAlias, $destAlias), false); $attrName = array_shift($path); $attr = $this->m_destInstance->getAttribute($attrName); if (is_object($attr)) { $attr->smartSearchCondition($id, $nr + 1, $path, $query, $destAlias, $value, $mode); } } else { $this->searchCondition($query, $ownerAlias, $value, $mode); } }
/** * Creates a searchcondition for the field, * was once part of searchCondition, however, * searchcondition() also immediately adds the search condition. * * @param Query $query The query object where the search condition should be placed on * @param string $table The name of the table in which this attribute * is stored * @param mixed $value The value the user has entered in the searchbox * @param string $searchmode The searchmode to use. This can be any one * of the supported modes, as returned by this * attribute's getSearchModes() method. * @param string $fieldname * * @return string The searchcondition to use. */ public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '') { $searchcondition = ''; if (is_array($value) && count($value) > 0 && $value[0] != '') { // This last condition is for when the user selected the 'search all' option, in which case, we don't add conditions at all. $field = $table . '.' . $this->fieldName(); if (count($value) == 1) { // exactly one value $query->addSearchCondition($field . ' & ' . $value[0]); } else { $mask = '(' . implode('|', $value) . ')'; $searchcondition = $field . '&' . $mask . '=' . $mask; } } return $searchcondition; }
/** * Creates an search condition for a given search value. * * @param Query $query The query to which the condition will be added. * @param string $table The name of the table in which this attribute * is stored * @param mixed $value The value the user has entered in the searchbox * @param string $searchmode The searchmode to use. This can be any one * of the supported modes, as returned by this * attribute's getSearchModes() method. * @param string $fieldaliasprefix optional prefix for the fieldalias in the table */ public function searchCondition($query, $table, $value, $searchmode, $fieldaliasprefix = '') { $ownerFields = $this->getOwnerFields(); // We only support 'exact' matches. // But you can select more than one value, which we search using the IN() statement, // which should work in any ansi compatible database. if (is_array($value) && count($value) > 0 && $value[0] != '') { // This last condition is for when the user selected the 'search all' option, in which case, we don't add conditions at all. $this->createLink(); $query->addJoin($this->m_linkInstance->m_table, $this->fieldName(), $table . '.' . $ownerFields[0] . '=' . $this->fieldName() . '.' . $this->getLocalKey(), false); $query->setDistinct(true); if (count($value) == 1) { // exactly one value $query->addSearchCondition($query->exactCondition($this->fieldName() . '.' . $this->getRemoteKey(), $this->escapeSQL($value[0]))); } else { // search for more values using IN() $query->addSearchCondition($this->fieldName() . '.' . $this->getRemoteKey() . " IN ('" . implode("','", $value) . "')"); } } }
public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '') { if (is_array($value)) { $value = $value[$this->fieldName()]; } if (isset($value)) { return $query->exactCondition($table . '.' . $this->fieldName(), $this->escapeSQL($value)); } return ''; }
/** * Creates a searchcondition for the field, * was once part of searchCondition, however, * searchcondition() also immediately adds the search condition. * * @param Query $query The query object where the search condition should be placed on * @param string $table The name of the table in which this attribute * is stored * @param mixed $value The value the user has entered in the searchbox * @param string $searchmode The searchmode to use. This can be any one * of the supported modes, as returned by this * attribute's getSearchModes() method. * @param string $fieldname * * @return string The searchcondition to use. */ public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '') { // We only support 'exact' matches. // But you can select more than one value, which we search using the IN() statement, // which should work in any ansi compatible database. $searchcondition = ''; if (is_array($value) && count($value) > 0 && $value[0] != '') { // This last condition is for when the user selected the 'search all' option, in which case, we don't add conditions at all. if (count($value) == 1 && $value[0] != '') { // exactly one value if ($value[0] == '__NONE__') { return $query->nullCondition($table . '.' . $this->fieldName(), true); } else { return $query->exactCondition($table . '.' . $this->fieldName(), $this->escapeSQL($value[0])); } } elseif (count($value) > 1) { // search for more values if (in_array('__NONE__', $value)) { unset($value[array_search('__NONE__', $value)]); return sprintf('(%s OR %s)', $query->nullCondition($table . '.' . $this->fieldName(), true), $table . '.' . $this->fieldName() . " IN ('" . implode("','", $value) . "')"); } else { return $table . '.' . $this->fieldName() . " IN ('" . implode("','", $value) . "')"; } } } return $searchcondition; }
public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '') { // Multiselect attribute has only 1 searchmode, and that is substring. $searchcondition = ''; if (is_array($value) && $value[0] != '' && count($value) > 0) { $searchcondition = []; foreach ($value as $str) { $searchcondition[] = $query->substringCondition($table . '.' . $this->fieldName(), $this->escapeSQL($str)); } $searchcondition = implode(' OR ', $searchcondition); } return $searchcondition; }
/** * Adds this attribute to database queries. * * Database queries (select, insert and update) are passed to this method * so the attribute can 'hook' itself into the query. * * Framework method. It should not be necessary to call this method * directly. Derived attributes that consist of more than a single simple * database field (like relations for example), may have to reimplement * this method. * * @param Query $query The SQL query object * @param string $tablename The name of the table of this attribute * @param string $fieldaliasprefix Prefix to use in front of the alias * in the query. * @param array $record The record that contains the value of this attribute. * @param int $level Recursion level if relations point to eachother, an * endless loop could occur if they keep loading * eachothers data. The $level is used to detect this * loop. If overriden in a derived class, any subcall to * an addToQuery method should pass the $level+1. * @param string $mode Indicates what kind of query is being processing: * This can be any action performed on a node (edit, * add, etc) Mind you that "add" and "update" are the * actions that store something in the database, * whereas the rest are probably select queries. */ public function addToQuery($query, $tablename = '', $fieldaliasprefix = '', &$record, $level = 0, $mode = '') { if ($mode == 'add' || $mode == 'update') { if ($mode == 'add' && $this->hasFlag(self::AF_AUTO_INCREMENT)) { $query->addSequenceField($this->fieldName(), $record[$this->fieldName()], $this->getOwnerInstance()->m_seq); return; } if ($this->isEmpty($record) && !$this->hasFlag(self::AF_OBLIGATORY) && !$this->isNotNullInDb()) { $query->addField($this->fieldName(), 'NULL', '', '', false, true); } else { $query->addField($this->fieldName(), $this->value2db($record), '', '', !$this->hasFlag(self::AF_NO_QUOTES), true); } } else { $query->addField($this->fieldName(), '', $tablename, $fieldaliasprefix, !$this->hasFlag(self::AF_NO_QUOTES), true); } }
/** * Makes the search conditions if the normal conditions are * not met and if given date is an array, * for example when only the year or year-month is given. * * @param Query $query Query which is given in getSearchCondition * @param string $table Table on which the condition must be executed * @param array $value Array with values given for the search * * @return string YYYY-MM or YYYY */ public function _getDateArraySearchCondition($query, $table, $value) { $db = $this->getDb(); $fromvalue = $this->_MakeDateForCondition($value['from']); $tovalue = $this->_MakeDateForCondition($value['to']); $datearraysearchcondition = ''; if ($fromvalue != '') { $field = $db->func_datetochar($table . '.' . $this->fieldName(), $this->_SetDateFormat($value['from'])); $datearraysearchcondition = $query->greaterthanequalCondition($field, $fromvalue); // check if tovalue is set, if so add the AND if ($tovalue != '') { $datearraysearchcondition .= ' AND '; } } if ($tovalue != '') { $field = $db->func_datetochar($table . '.' . $this->fieldName(), $this->_SetDateFormat($value['to'])); $datearraysearchcondition .= $query->lessthanequalCondition($field, $tovalue); } return $datearraysearchcondition; }
/** * Get the between search condition. * * @param Query $query The query object where the search condition should be placed on * @param string $fieldname The name of the field in the database * @param array $value The processed search value * * @return string query where clause for searching */ public function getBetweenCondition($query, $fieldname, $value) { if ($value['from'] != '' && $value['to'] != '') { if ($value['from'] > $value['to']) { // User entered fields in wrong order. Let's fix that. $tmp = $value['from']; $value['from'] = $value['to']; $value['to'] = $tmp; } return $query->betweenCondition($fieldname, $this->escapeSQL($value['from']), $this->escapeSQL($value['to'])); } elseif ($value['from'] != '' && $value['to'] == '') { return $query->greaterthanequalCondition($fieldname, $value['from']); } elseif ($value['from'] == '' && $value['to'] != '') { return $query->lessthanequalCondition($fieldname, $value['to']); } return ''; }