Ejemplo n.º 1
0
 /**
  * @param   string  $param1
  * @param   string  $param2
  * @param   mixed   $options
  * @return  mixed
  */
 public static function value($param1, $param2, $options = array())
 {
     if (!$param2) {
         return null;
     }
     // third param can be a db connection resource
     if (is_object($options) && get_class($options) == 'ADODB_postgres7') {
         $db_conn = $options;
         $options = array();
     }
     // get connection
     $db_conn = $db_conn ?: $options['db'];
     $db_conn = $db_conn ?: self::getDB();
     $is_aql = aql::is_aql($param1);
     // normalize primary table and aql
     if ($is_aql) {
         $aql = $param1;
         $primary_table = aql::get_primary_table($aql);
     } else {
         list($primary_table, $field) = explode('.', $param1);
         $aql = "{$primary_table} { {$field} }";
     }
     // get where
     $multiple = false;
     $where = call_user_func(function () use($primary_table, $param2, &$multiple) {
         $spr = '%s.%s = \'%s\'';
         $decrypt = function ($r) use($primary_table) {
             return is_numeric($r) ? $r : decrypt($r, $primary_table);
         };
         if (is_numeric($param2)) {
             return sprintf($spr, $primary_table, 'id', $param2);
         }
         if (!is_array($param2)) {
             // check for ide
             $id = $decrypt($param2);
             if (is_numeric($id)) {
                 return sprintf($spr, $primary_table, 'id', $id);
             }
             // otherwise check for slug field on table
             if (!aql2array::table_field_exists($primary_table, 'slug')) {
                 return;
             }
             return sprintf($spr, $primary_table, 'slug', $param2);
         }
         // this is an array
         $multiple = true;
         $param2 = array_filter(array_map($decrypt, $param2));
         $param2[] = -1;
         $ids = implode(',', $param2);
         return "{$primary_table}.id in ({$ids})";
     });
     // return if we dont find a where clause
     if (!$where) {
         return false;
     }
     $clause = array($primary_table => array('where' => array($where), 'order by' => 'id asc'));
     $rs = aql::select($aql, $clause, null, null, null, $db_conn);
     if ($multiple) {
         return $rs;
     }
     if ($is_aql) {
         return $rs[0];
     }
     return $rs[0][$field];
 }