* @copyright Copyright (c) 2010, ConsultorPC * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html * @link http://smartclientphp.com/ * @since Version 0.1 * @filesource */ /** * Smart Client Advanced Criteria to SQL converter * */ class ScAdvancedCriteriaToSql { /** * Array that stores all criterias * * @var $_criterias */ protected $_criterias = array(); /** * Operator * * @var $_operator string */ protected $_operator; /** * Array contaning a convert table between SC nomeclature and SQL * * @var $_operatorToSql array */ protected $_operatorToSql = array( 'iContains' => '[fieldName] LIKE "%[value]%"', 'iStartsWith' => '[fieldName] LIKE "[value]%"', 'iEndsWith' => '[fieldName] LIKE "%[value]"', 'iNotContains' => '[fieldName] NOT LIKE "%[value]%"', 'iNotStartsWith' => '[fieldName] NOT LIKE "[value]%"', 'iNotEndsWith' => '[fieldName] NOT LIKE "%[value]"', 'equals' => '[fieldName] = "[value]"', 'notEqual' => '[fieldName] != "[value]"', 'lessThan' => '[fieldName] < "[value]"', 'greaterThan' => '[fieldName] > "[value]"', 'lessOrEqual' => '[fieldName] <= "[value]"', 'greaterOrEqual' => '[fieldName] >= "[value]"', 'between' => '[fieldName] BETWEEN "[start]" AND "[end]"', 'isNull' => '[fieldName] IS NULL', 'notNull' => '[fieldName] NOT NULL' ); /** * Construct function */ public function __construct() { } /** * Add a new criteria * * @param array|string $criteria * @return void */ public function addCriteria( $criteria ) { if (! is_array($criteria)) { $criteria = json_decode(stripslashes($criteria), true); } $this->_criterias[] = $criteria; } /** * Get all criterias * * @return array */ public function getCriterias() { return $this->_criterias; } /** * Clear all criterias * * @return void */ public function clearCriterias() { unset($this->_criterias); } /** * Set operator * * @param string $operator * @return void */ public function setOperator( $operator ) { // TODO: validate operator $this->_operator = $operator; } /** * Get operator * * @return string */ public function getOperator() { return $this->_operator; } /** * Set criterias and operator from post, coming directly from SC component * * @param array $post * @return boolean */ public function setFromPost( $post ) { // Check if constructor is advanced criteria if ($post['_constructor'] == 'AdvancedCriteria') { // Check if multiple criterias are coming on an array if (is_array($post['criteria'])) { // Loop on all criterias foreach ($post['criteria'] as $criteria) { // Add criteria $this->addCriteria($criteria); } } else { // Single criteria, set it $this->addCriteria($post['criteria']); } // Set operator $this->setOperator($post['operator']); return true; } else { return false; } } /** * Process criteria and operators, returning the SQL * * @return string|boolean */ public function getSql() { // Check if criterias are set and is an array if (empty($this->_criterias) && is_array($this->_criterias)) { return false; } // Check if we have an operator value if (empty($this->_operator)) { $this->_operator = 'and'; } $criteriaWhere = ' ( '; // Loop on all criterias adding them $totalCriterias = count($this->_criterias); for ($i = 0; $i < $totalCriterias; $i ++) { // Do not add operator on first criteria if ($i != 0) { $criteriaWhere .= ' ) ' . $this->_operator . ' ( '; } // Process this criteria $criteriaWhere .= $this->_processCriteria($this->_criterias[$i]); } $criteriaWhere .= ' ) '; // Return sql return $criteriaWhere; } /** * Process criteria and return sql * * @param mixed $criteria * @return string */ protected function _processCriteria( $criteria ) { // Check if criteria is an array. If it is loop on all criterias and process them if (is_array($criteria['criteria'])) { $returnString = ''; $totalCriterias = count($criteria['criteria']); for ($i = 0; $i < $totalCriterias; $i ++) { if ($i != 0) { $returnString .= ' ' . $criteria['operator'] . ' '; } $returnString .= $this->_processCriteria($criteria['criteria'][$i]); } return $returnString; } else { // Use operatorToSql table to convert criteria to SQL $returnString = str_replace('[fieldName]', $criteria['fieldName'], $this->_operatorToSql[$criteria['operator']]); $returnString = str_replace('[value]', $criteria['value'], $returnString); $returnString = str_replace('[start]', $criteria['start'], $returnString); $returnString = str_replace('[end]', $criteria['end'], $returnString); return $returnString; } } }