Example #1
0
 function handler_ajax_read($page, $ids, $state)
 {
     S::assert_xsrf_token();
     $ids = explode(',', $ids);
     try {
         $news = Collection::fromArray($ids, 'News');
         $news->read($state == 1);
     } catch (NotAnIdException $e) {
     }
     return PL_JSON;
 }
Example #2
0
 /**
  * Main entry point to select fields
  * @param array|Collection $metas metaobjects to be selected
  * @return Collection upated $metas
  * @throws Exception if an error happened
  */
 public function select($metas)
 {
     if (empty($metas)) {
         return;
     }
     if (is_array($metas)) {
         $metas = Collection::fromArray($metas);
     }
     $tobefetched = $this->fields;
     $handlers = $this->handlers();
     foreach ($handlers as $handler => $fields) {
         $intersect = array_intersect($fields, $tobefetched);
         if (!empty($intersect)) {
             $tobefetched = array_diff($tobefetched, $intersect);
             $handler = 'handler_' . $handler;
             $this->{$handler}($metas, $intersect);
         }
     }
     if (!empty($tobefetched)) {
         throw new Exception("Some fields (" . implode(', ', $tobefetched) . ")" . " couldn't be fetched in class " . $this->className());
     }
     if (is_callable($this->callback)) {
         $cb = $this->callback;
         $cb($metas);
     }
     return $metas;
 }
Example #3
0
 /**
  * @param $post_id
  *
  * @return Collection
  */
 public function get_post_ratings($post_id)
 {
     $ratings = (array) get_post_meta($post_id, 'wpkb_ratings', true);
     return Collection::fromArray($ratings);
 }
Example #4
0
 public function __call($method, $arguments)
 {
     $className = $this->className;
     $inferedMethod = 'batch' . ucfirst($method);
     if (method_exists($className, $inferedMethod)) {
         // Call $className->batch$method($this->collected, ...)
         array_unshift($arguments, $this->collected);
         $r = forward_static_call_array(array($className, $inferedMethod), $arguments);
         if (!is_array($r)) {
             return $r;
         }
         $c = new Collection($className);
         if (!empty($r)) {
             $c->add($r);
         }
         return $c;
     }
     // If there is no argument, build an array with values from $collected->$method
     if (empty($arguments)) {
         $values = array_map(function ($mixed) use($method) {
             return $mixed->{$method}();
         }, $this->collected);
         // Try magic things with the schema
         $schema = Schema::get($className);
         if ($schema->isScalar($method)) {
             // Return an array of scalar values
             return $values;
         } elseif ($schema->isObject($method)) {
             // Return a collection
             return Collection::fromArray($values, $schema->objectType($method));
         } elseif ($schema->isFlagset($method)) {
             // Return a merged flagset
             $fs = new PlFlagSet();
             foreach ($values as $flags) {
                 foreach ($flags as $flag) {
                     $fs->addFlag($flag);
                 }
             }
             return $fs;
         } elseif ($schema->isCollection($method)) {
             // Return a merged collection
             $col = new Collection();
             foreach ($values as $c) {
                 $col->merge($c);
             }
             return $col;
         }
         throw new Exception("Unknown automatic field {$method} is schema for {$className}");
     }
     throw new Exception("The method {$className}::{$inferedMethod} doesn't exist");
 }