/** * Execute replacements of terms in SQL's, URL's and others */ public static function replaceExpr($text) { $callbacks = array(); $callbacks['P_QUOTED'] = function ($var) { return isset($_REQUEST[$var]) ? \Meta\Core\Db::quote($_REQUEST[$var]) : 'NULL'; }; $callbacks['P_NO_QUOTED'] = function ($var) { return isset($_REQUEST[$var]) ? $_REQUEST[$var] : NULL; }; // change all occurrences of $<function>[<param>] to call php function return preg_replace_callback('/\\$(\\w+)\\[(\\w+)]/', function ($matches) use($callbacks) { $func = $matches[1]; if (!isset($callbacks[$func])) { throw new Exception(t('The public static function ' . $func . ' used in expression do not exists')); } $value = $callbacks[$func]($matches[2]); // callback execute return $value; }, $text); }
/** * Aplica as condicoes no WHERE do SQL para busca generica. * * @see print_crud_search() * * @param string $sql * @param array $search_fields * @return array */ public static function searchWhere($sql = array(), $search_fields = array(), $text) { // aplica filtros de busca vindos do campo, caso exista if (!isset($sql['where']) || isset($sql['where']) && strlen(trim($sql['where'])) == 0) { $sql['where'] = '1=1'; } $sql['where'] .= ' AND ( 1=0 '; foreach ($search_fields as $col) { $sql['where'] .= ' OR CONVERT(' . $col . ', CHAR) like ' . \Meta\Core\Db::quote('%' . $text . '%'); } $sql['where'] .= ')'; return $sql; }