예제 #1
0
 /**
  * Returns an array of logged events based on supplied parameters. This function integrates with a jQuery
  * datagrid control in the admin screens, allowing arbitrary sorting and filtering of events.
  *
  * @param array $args | Args in the form: array("col"=>column_name, "op" => "<, >, =, !=", "val" => "int | string | array()")
  *	    => ARR @param array $args | Args in the form: array("col"=>column_name, "op"=>"<, >, =, !=", "val"=>"int | string | array()")
  *		=> ARR @param int '' | Array index
  *		    => VAL @param string $col | Name of the column this key describes	 *
  *		    => VAL @param string $op | SQL comparison operator to use: ">=" | "<=" | ">" | "<" | "=" | "!=" | "<>"
  *		    => VAL @param int/string/array $val | Value or values to test against. Single value as int/string. Multiple values as array.	 *
  *
  * @param object/array $columns | Either array with specific columns to include/exclue or anything else to return all columns.
  *	=> ARR @param int '' | Array index
  *	    => VAL @param string $table_class | Class of table that the column is from.
  *	    => VAL @param string $table_alias | Alias table that the column is from. Used instead of table_class not required
  *	    => VAL @param string $col_name | Name of the column
  *	    => VAL @param string $col_alias | Alias of the column
  *	    => VAL @param bool $count | True to count this column
  *	    => VAL @param bool/array $sum | True sum this column, array sums multiple columns
  *	        => ARR @param int '' | Array index
  *			=> VAL @param string $table_alias | Table alias of table that the column is from. if table alias is not set the default is t(number)
  *			=> VAL @param string $col_name | Name of the column
  *			=> VAL @param string $col_alias | Column alias used instead of table alias and col_name
  *			=> VAL @param bool $count | True to count this column
  *			=> VAL @param string $op | Operation to perform on column value +,- or *(muliple) default +
  *
  * @param array $ctrl | Control parameters for the query
  *	=> VAL @param int $page | Set current page (used when traversing multi-page data sets)
  *	=> VAL @param int $per_page | Max number of rows to return in a query / number of rows to return per page when transversing a multi-page data set
  *	=> VAL @param int $offset | Shift results page forward or backward "n" items within the returned data set
  *	=> ARR @param array $sort | Sort results by supplied parameters. Multi-dimensional sorts possible by passing multiple arrays.
  *	    => ARR @param int '' | Array index
  *		=> VAL @param string $class | Class name that owns the table
  *		=> VAL @param string $col | Name of column to sort by
  *		=> VAL @param string $col_alias | Column alias used instead of class and col. not required
  *		=> VAL @param string/array $sort | Direction to sort in. "ASC", "DESC", array(val, val, val) where position in array
  *						   is the desired position in the results set for the row or rows with columm matching val
  *	=> ARR @param array $group | Apply SQL GROUP to columns. Multi-dimensional group possible by passing multiple arrays.
  *	    => ARR @param int '' | Array index
  *		=> VAL @param string $class | Class name that owns the table
  *		=> VAL @param string $col | Name of column to apply GROUP to
  *		=> VAL @param string $col_alias | Column alias used instead of class and col. not required
  *		=> VAL @param string $sort | Direction to sort in. "ASC" | "DESC"
  *	=> VAL @param string $format | @see FOX_db::runQuery() for detailed info on format string
  *	=> VAL @param string $key_col | Column name to get key names from when using $format="key" or $format="asc"
  *	=> VAL @param string $asc_col | Column name to use as value when using $format="asc"
  *
  * @param bool $return_tokens | True to use dictionary and return words for tree, branch and node
  *
  * @return bool/int/array | False on failure. Int on count. Array of rows on success.
  */
 public function query($args = null, $columns = null, $ctrl = null, $return_tokens = true)
 {
     if (!isset($ctrl)) {
         $ctrl = array();
     }
     if (!isset($ctrl['format'])) {
         $ctrl['format'] = "array_array";
     }
     $join_args = array("tree" => array(), "branch" => array(), "node" => array());
     if (is_array($args)) {
         // Process args in case dic entries are used instead of numbers
         foreach ($args as $arg) {
             // check dic columns
             if (in_array($arg["col"], array('tree', 'branch', 'node'))) {
                 // if val is string or array of strings
                 if (!is_numeric($arg["val"]) || is_array($arg["val"]) && !is_numeric($arg["val"][0])) {
                     // if val is string or array of strings then add to join_args and don't add to processed_args
                     $join_args[$arg['col']][] = $args;
                     continue;
                 }
             }
             $processed_args[] = $arg;
         }
     }
     $db = new FOX_db();
     // Check if need to use runSelectLeftJoin
     $num_join_args = count($join_args['tree']) + count($join_args['branch']) + count($join_args['node']);
     if ($return_tokens || $num_join_args > 0) {
         if (!isset($ctrl['group'])) {
             $ctrl["group"] = array(array("class" => self::$struct, "col" => "id", "sort" => "ASC"));
         }
         $primary = array("class" => self::$struct, "args" => $processed_args);
         $join = array(array("class" => "FOX_log_dictionary_tree", "on" => array("pri" => "tree", "op" => "=", "sec" => "id"), "args" => $join_args['tree']), array("class" => "FOX_log_dictionary_branch", "on" => array("pri" => "branch", "op" => "=", "sec" => "id"), "args" => $join_args['branch']), array("class" => "FOX_log_dictionary_node", "on" => array("pri" => "node", "op" => "=", "sec" => "id"), "args" => $join_args['node']));
         if (is_null($columns)) {
             if ($return_tokens) {
                 $columns = array(array("table_alias" => "t1", "col_name" => "id", "col_alias" => "id"), array("table_alias" => "t2", "col_name" => "token", "col_alias" => "tree"), array("table_alias" => "t3", "col_name" => "token", "col_alias" => "branch"), array("table_alias" => "t4", "col_name" => "token", "col_alias" => "node"), array("table_alias" => "t1", "col_name" => "user_id", "col_alias" => "user_id"), array("table_alias" => "t1", "col_name" => "level", "col_alias" => "level"), array("table_alias" => "t1", "col_name" => "date", "col_alias" => "date"), array("table_alias" => "t1", "col_name" => "summary", "col_alias" => "summary"), array("table_alias" => "t1", "col_name" => "data", "col_alias" => "data"));
             } else {
                 $columns = array(array("table_alias" => "t1", "col_name" => "id", "col_alias" => "id"), array("table_alias" => "t1", "col_name" => "tree", "col_alias" => "tree"), array("table_alias" => "t1", "col_name" => "branch", "col_alias" => "branch"), array("table_alias" => "t1", "col_name" => "node", "col_alias" => "node"), array("table_alias" => "t1", "col_name" => "user_id", "col_alias" => "user_id"), array("table_alias" => "t1", "col_name" => "level", "col_alias" => "level"), array("table_alias" => "t1", "col_name" => "date", "col_alias" => "date"), array("table_alias" => "t1", "col_name" => "summary", "col_alias" => "summary"), array("table_alias" => "t1", "col_name" => "data", "col_alias" => "data"));
             }
         }
         try {
             $result = $db->runSelectQueryLeftJoin($primary, $join, $columns, $ctrl);
         } catch (FOX_exception $child) {
             throw new FOX_exception(array('numeric' => 3, 'text' => "Error reading from DB \n", 'data' => array('primary' => $primary, 'join' => $join, 'columns' => $columns, 'ctrl' => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child));
             return false;
         }
     } else {
         if (!is_null($columns) && count($columns) > 0) {
             $select_columns["mode"] = "include";
             if (count($columns) == 1) {
                 $select_columns["col"] = $columns[0]["col_name"];
             } else {
                 foreach ($columns as $col) {
                     $select_columns["col"][] = $col["col_name"];
                 }
             }
         }
         try {
             $result = $db->runSelectQuery(self::$struct, $processed_args, $select_columns, $ctrl);
         } catch (FOX_exception $child) {
             throw new FOX_exception(array('numeric' => 4, 'text' => "Error reading from DB \n", 'data' => array('primary' => $primary, 'join' => $join, 'columns' => $columns, 'ctrl' => $ctrl), 'file' => __FILE__, 'line' => __LINE__, 'method' => __METHOD__, 'child' => $child));
             return false;
         }
     }
     if ($result == 0) {
         return false;
     } else {
         return $result;
     }
 }