/** * @param RequestInterface $request A PSR-7 compatible Request instance. * @param ResponseInterface $response A PSR-7 compatible Response instance. * @return ResponseInterface */ public function run(RequestInterface $request, ResponseInterface $response) { unset($request); $climate = $this->climate(); $climate->underline()->out('List objects'); $this->setData(['obj_type' => $this->argOrInput('obj-type'), 'page' => $climate->arguments->get('page'), 'num_per_page' => $climate->arguments->get('num')]); $model = $this->modelFactory()->create($this->objType()); $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $loader->setModel($model); $loader->setPagination(['page' => $this->page(), 'num_per_page' => $this->numPerPage()]); $collection = $loader->load(); $collection = $this->collection(); $table = []; $rows = $this->objectRows(); foreach ($collection as $c) { $obj = []; $props = $model->properties(); foreach ($props as $property_ident => $unused) { $prop = $c->p($property_ident); $label = (string) $prop->label(); $val = (string) $prop->displayVal(); $obj[$label] = $val; } $table[] = $obj; } $climate->table($table); return $response; }
/** * @param RequestInterface $request A PSR-7 compatible Request instance. * @param ResponseInterface $response A PSR-7 compatible Response instance. * @return ResponseInterface */ public function run(RequestInterface $request, ResponseInterface $response) { unset($request); $this->startLock(); $climate = $this->climate(); $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->scheduleFactory()]); $loader->setModel($this->scheduleProto()); $loader->addFilter(['property' => 'processed', 'val' => 0]); $loader->addFilter(['property' => 'scheduled_date', 'val' => date('Y-m-d H:i:s'), 'operator' => '<']); $loader->addOrder(['property' => 'scheduled_date', 'mode' => 'asc']); $scheduled = $loader->load(); $callback = function ($obj) use($climate) { // No default callback }; $successCallback = function ($obj) use($climate) { $climate->green()->out(sprintf('Object %s : %s schedule was successfully ran.', $obj->targetType(), $obj->targetId())); }; $failureCallback = function ($obj) use($climate) { $climate->red()->out(sprintf('Object %s : %s schedule could not be ran.', $obj->targetType(), $obj->targetId())); }; foreach ($scheduled as $schedule) { $schedule->setModelFactory($this->modelFactory()); $schedule->process($callback, $successCallback, $failureCallback); } $this->stopLock(); return $response; }
/** * Retrieves all revisions for the current objet * * @param callable $callback Optional object callback. * @return array */ public function allRevisions(callable $callback = null) { $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $loader->setModel($this->createRevisionObject()); $loader->addFilter('target_type', $this->objType()); $loader->addFilter('target_id', $this->id()); $loader->addOrder('rev_ts', 'desc'); if ($callback !== null) { $loader->setCallback($callback); } $revisions = $loader->load(); return $revisions->objects(); }
/** * Return all the objs with geographical information * * @return Collection */ public function mapObjects() { if ($this->mapObjects === null) { $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $loader->setModel($this->objProto()); $that = $this; $loader->setCallback(function ($obj) use($that) { $obj->mapInfoboxTemplate = $that->infoboxTemplate(); if ($that->latProperty() && $that->latProperty()) { $obj->mapShowMarker = true; $obj->mapLat = call_user_func([$obj, $that->latProperty()]); $obj->mapLon = call_user_func([$obj, $that->lonProperty()]); } else { $obj->mapShowMarker = false; } if ($that->pathProperty()) { $mapPath = call_user_func([$obj, $that->pathProperty()]); if ($mapPath) { $obj->mapShowPath = true; // Same type of coords. $obj->mapPath = $that->formatPolygon($mapPath); } else { $obj->mapShowPath = false; } } if ($that->polygonProperty()) { $mapPolygon = call_user_func([$obj, $that->polygonProperty()]); if ($mapPolygon) { $obj->mapShowPolygon = true; $obj->mapPolygon = $that->formatPolygon($mapPolygon); } else { $obj->mapShowPolygon = false; } } }); $this->mapObjects = $loader->load(); } foreach ($this->mapObjects as $obj) { $GLOBALS['widget_template'] = $obj->mapInfoboxTemplate; (yield $obj); } }
/** * Delete all object routes. * * Should be called on object deletion {@see \Charcoal\Model\AbstractModel::preDelete()}. * * @return boolean Success or failure. */ protected function deleteObjectRoutes() { if (!$this->objType()) { return false; } if (!$this->id()) { return false; } $model = $this->modelFactory()->get($this->objectRouteClass()); $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $loader->setModel($model)->addFilter('route_obj_type', $this->objType())->addFilter('route_obj_id', $this->id()); $collection = $loader->load(); foreach ($collection as $route) { $route->delete(); } return true; }
/** * Actual object collection * @throws Exception If collection config is not set. * @return Collection Collection from the export config. */ public function collection() { if ($this->collection) { return $this->collection; } if (!$this->collectionConfig()) { throw new Exception('No collection config set for ' . $this->objType() . ' in Charcoal\\Admin\\Helper\\Exporter'); } $collection = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $collection->setModel($this->proto()); $collection->setData($this->collectionConfig()); $this->collection = $collection->load(); return $this->collection; }
/** * Retrieve the items of the current queue. * * @return Collection */ public function loadQueueItems() { $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->queueItemFactory()]); $loader->setModel($this->queueItemProto()); $loader->addFilter(['property' => 'processed', 'val' => 0]); $loader->addFilter(['property' => 'processing_date', 'val' => date('Y-m-d H:i:s'), 'operator' => '<']); $queueId = $this->queueId(); if ($queueId) { $loader->addFilter(['property' => 'queue_id', 'val' => $queueId]); } $loader->addOrder(['property' => 'queued_date', 'mode' => 'asc']); $queued = $loader->load(); return $queued; }
/** * HierarchicalTrait > loadChildren * * @return Co */ public function loadChildren() { $loader = new CollectionLoader(['logger' => $this->logger, 'factory' => $this->modelFactory()]); $loader->setModel($this); $loader->addFilter(['property' => 'master', 'val' => $this->id()]); $loader->addFilter(['property' => 'active', 'val' => true]); $loader->addOrder(['property' => 'position', 'mode' => 'asc']); return $loader->load(); }