fromUrlKey() public static method

Initiliaze from url and parent
public static fromUrlKey ( string $urlKey, mixed $parentId = false ) : Model
$urlKey string Url key
$parentId mixed Parent id
return Model
Example #1
0
 /**
  * Returns documents
  * $data can be mixed
  * if data is an array, return all documents
  * if data is numeric, return all documents with parent_id equal to $data
  * if data is string, return all documents with parent's url key equal to $data
  *
  * @param mixed $data Data
  *
  * @return array \Gc\Document\Model
  */
 public function __invoke($data = null)
 {
     $elements = array();
     $documents = new DocumentCollection();
     if (empty($data)) {
         $elements = $documents->load(0)->getDocuments();
     } else {
         if (is_numeric($data)) {
             $elements = $documents->load($data)->getDocuments();
         } elseif (is_string($data)) {
             $document = DocumentModel::fromUrlKey($data);
             $elements = $document->getChildren();
         } elseif (is_array($data)) {
             foreach ($data as $documentId) {
                 if (empty($documentId)) {
                     continue;
                 }
                 $document = $this->getView()->document($documentId);
                 if (!empty($document)) {
                     $elements[] = $document;
                 }
             }
         }
     }
     return $elements;
 }
Example #2
0
 /**
  * Returns document from id.
  *
  * @param mixed $identifier Identifier
  *
  * @return \Gc\Document\Model
  */
 public function __invoke($identifier = '')
 {
     if (is_numeric($identifier)) {
         return DocumentModel::fromId($identifier);
     }
     return DocumentModel::fromUrlKey($identifier);
 }
Example #3
0
 /**
  * Find document from request uri
  *
  * @param string  $path      Path from request uri
  * @param boolean $isPreview Is the current page is a preview
  *
  * @return Document\Model
  */
 protected function findDocument($path, $isPreview)
 {
     $explodePath = $this->explodePath($path);
     $documentTmp = null;
     $children = null;
     $hasDocument = false;
     $parentId = null;
     foreach ($explodePath as $urlKey) {
         if ($hasDocument === false) {
             $documentTmp = Document\Model::fromUrlKey($urlKey, $parentId);
             if (empty($documentTmp) and $parentId === null and ($homeDocument = Document\Model::fromUrlKey('')) !== false) {
                 $documentTmp = Document\Model::fromUrlKey($urlKey, $homeDocument->getId());
             }
         }
         if (is_array($children) and !in_array($documentTmp, $children)) {
             $hasDocument = true;
         } else {
             if (!empty($documentTmp) and ($documentTmp->isPublished() or $isPreview)) {
                 $parentId = $documentTmp->getId();
                 $children = $documentTmp->getChildren();
             }
         }
     }
     return $documentTmp;
 }
Example #4
0
 /**
  * Test
  *
  * @return void
  */
 public function testFromFakeUrlKey()
 {
     $model = Model::fromUrlKey($this->object->getUrlKey(), 1000);
     $this->assertFalse($model);
 }
Example #5
0
 public function testImportScript()
 {
     $this->assertFalse($this->object->import('<xml></xml>'));
     $this->assertFalse($this->object->import(''));
     $this->assertFalse($this->object->import('<a></b>'));
     $this->createUser();
     $this->createContent();
     $data = $this->object->export($this->what);
     $this->removeContent();
     $this->assertTrue($this->object->import($data));
     $view = ViewModel::fromIdentifier('ViewContentIdentifier');
     $layout = LayoutModel::fromIdentifier('LayoutContentIdentifier');
     $script = ScriptModel::fromIdentifier('ScriptContentIdentifier');
     $this->assertInstanceOf('Gc\\View\\Model', $view);
     $this->assertInstanceOf('Gc\\Layout\\Model', $layout);
     $this->assertInstanceOf('Gc\\Script\\Model', $script);
     //Test datatype
     $datatype = new DatatypeModel();
     $datatype = $datatype->fromArray($datatype->fetchRow($datatype->select(array('name' => 'DatatypeTest'))));
     $this->assertInstanceOf('Gc\\Datatype\\Model', $datatype);
     //Test document type
     $documentType = new DocumentTypeModel();
     $documentType = $documentType->fromArray($documentType->fetchRow($documentType->select(array('name' => 'DocumentType'))));
     $this->assertInstanceOf('Gc\\DocumentType\\Model', $documentType);
     $this->assertCount(1, $documentType->getDependencies());
     $this->assertCount(1, $documentType->getAvailableViews()->getViews());
     $tabs = $documentType->getTabs();
     $this->assertCount(1, $tabs);
     $properties = $tabs[0]->getProperties();
     $this->assertCount(1, $properties);
     //Test document
     $document = DocumentModel::fromUrlKey('');
     $this->assertInstanceOf('Gc\\Document\\Model', $document);
     $property = $document->getProperty('azd');
     $this->assertInstanceof('Gc\\Property\\Model', $property);
     $this->assertEquals('string', $property->getValue());
     $document->delete();
     //Delete data
     $properties[0]->delete();
     $tabs[0]->delete();
     $datatype->delete();
     $view->delete();
     $script->delete();
     $layout->delete();
     $documentType->delete();
     $this->removeUser();
 }
Example #6
0
 /**
  * Import Documents
  *
  * @param array &$ids     Ids
  * @param array &$errors  Errors
  * @param array $children Children list
  *
  * @return void
  */
 protected function importDocuments(&$ids, &$errors, $children)
 {
     foreach ($children['children'] as $child) {
         $urlKey = (string) $child->url_key;
         $model = Document\Model::fromUrlKey($urlKey);
         $attributes = $child->attributes();
         $id = (int) $attributes['id'];
         if (empty($model)) {
             $model = Document\Model::fromId($id);
             if (empty($model)) {
                 $model = new Document\Model();
             }
         }
         $documentTypeId = isset($ids['document_types'][(int) $child->document_type_id]) ? $ids['document_types'][(int) $child->document_type_id] : $model->getDocumentTypeId();
         $viewId = isset($ids['views'][(int) $child->view_id]) ? $ids['views'][(int) $child->view_id] : $model->getViewId();
         $layoutId = isset($ids['layouts'][(int) $child->layout_id]) ? $ids['layouts'][(int) $child->layout_id] : $model->getLayoutId();
         $parentId = isset($ids['layouts'][(int) $child->parent_id]) ? $ids['layouts'][(int) $child->parent_id] : $model->getParentId();
         $name = (string) $child->name;
         $status = (string) $child->status;
         $userId = (int) $child->user_id;
         $sortOrder = (int) $child->sort_order;
         $showInNav = (int) $child->show_in_nav;
         $model->addData(array('name' => empty($name) ? $model->getName() : $name, 'url_key' => $urlKey, 'status' => empty($status) ? $model->getStatus() : $status, 'show_in_nav' => empty($showInNav) ? $model->getShowInNav() : $showInNav, 'sort_order' => empty($sortOrder) ? $model->getSortOrder() : $sortOrder, 'icon_id' => (int) $child->icon_id, 'view_id' => $viewId, 'parent_id' => $parentId, 'user_id' => $userId, 'layout_id' => $layoutId, 'document_type_id' => empty($documentTypeId) ? $model->getDocumentTypeId() : $documentTypeId));
         if ($model->getUserId() === null) {
             $model->setUserId($this->serviceLocator->get('Auth')->getIdentity()->getId());
         }
         try {
             if (!empty($model)) {
                 $model->save();
                 $ids['documents'][$id] = $model->getId();
                 $values = (array) $child->properties;
                 if (isset($values['property_value']) and is_array($values['property_value'])) {
                     $values = $values['property_value'];
                 }
                 foreach ($values as $value) {
                     $documentId = (int) $value->document_id;
                     $propertyId = (int) $value->property_id;
                     $valueModel = new Property\Value\Model();
                     $valueModel->load(null, isset($ids['documents'][$documentId]) ? $ids['documents'][$documentId] : $documentId, isset($ids['properties'][$propertyId]) ? $ids['properties'][$propertyId] : $propertyId);
                     $valueModel->setValue((string) base64_decode($value->value));
                     $valueModel->save();
                 }
             }
         } catch (Exception $e) {
             $errors[] = sprintf($this->serviceLocator->get('MvcTranslator')->translate('Cannot save document with id (%d)'), $id);
         }
     }
 }
Example #7
0
 /**
  * Dashboard widget
  *
  * @param Event $e Event
  *
  * @return false|null
  */
 public function dashboard(Event $e)
 {
     $addthis = $e->getTarget()->getServiceLocator()->get('AddThisModel');
     $options = $addthis->getConfig();
     if (empty($options['show_stats'])) {
         return false;
     }
     if (isset($options['username'])) {
         $username = $options['username'];
     } else {
         return false;
     }
     if (isset($options['password'])) {
         $password = $options['password'];
     } else {
         return false;
     }
     if (isset($options['profile_id'])) {
         $profile = $options['profile_id'];
     } else {
         return false;
     }
     $document = DocumentModel::fromUrlKey('');
     $domain = parse_url($document->getUrl(true), PHP_URL_HOST);
     $requests = array(array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'day'), array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'week'), array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'day'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'week'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'month'), array('metric' => 'shares', 'dimension' => 'service', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => 'service', 'domain' => $domain, 'period' => 'month'), array('metric' => 'shares', 'dimension' => 'url', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => 'url', 'domain' => $domain, 'period' => 'month'));
     $stats = array();
     foreach ($requests as $request) {
         $dimension = $metric = $domain = $period = '';
         extract($request);
         $dimension = $dimension != '' ? '/' . $dimension : '';
         $stats[$metric . $dimension . $period] = $this->executeQuery($metric, $dimension, $domain, $period, $username, $password, $profile);
         if (!$stats[$metric . $dimension . $period]) {
             $data = array('error' => true);
             break;
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 401) {
             $data = array('unauthorized' => true);
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 500) {
             $data = array('error' => true);
             break;
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 501) {
             $data = array('error' => true);
             break;
         }
     }
     if (empty($data) and !empty($stats['sharesday']) and $stats['sharesday']->getStatusCode() == 200) {
         if ($stats['sharesmonth']->getBody() == '[]') {
             $data = array('noData' => true);
         } else {
             $yesterday = array();
             $shareurls = json_decode($stats['shares/urlmonth']->getBody());
             $clickbackurls = json_decode($stats['clickbacks/urlmonth']->getBody());
             $yesterday['shares'] = json_decode($stats['sharesday']->getBody());
             $yesterday['shares'] = isset($yesterday['shares'][0]->shares) ? $yesterday['shares'][0]->shares : '';
             $yesterday['clickbacks'] = json_decode($stats['clickbacksday']->getBody());
             $yesterday['clickbacks'] = isset($yesterday['clickbacks'][0]->clickbacks) ? $yesterday['clickbacks'][0]->clickbacks : '';
             $yesterday['viral'] = $yesterday['shares'] > 0 && $yesterday['clickbacks'] > 0 ? $yesterday['clickbacks'] / $yesterday['shares'] * 100 . '%' : 'n/a';
             if (!$yesterday['clickbacks']) {
                 $yesterday['clickbacks'] = 0;
             }
             if (!$yesterday['shares']) {
                 $yesterday['shares'] = 0;
             }
             $lastweek = array();
             $decodedLastWeek = json_decode($stats['sharesweek']->getBody());
             $lastweek['shares'] = 0;
             foreach ($decodedLastWeek as $share) {
                 $lastweek['shares'] += $share->shares;
             }
             $decodedLastWeek = json_decode($stats['clickbacksweek']->getBody());
             $lastweek['clickbacks'] = 0;
             foreach ($decodedLastWeek as $clickback) {
                 $lastweek['clickbacks'] += $clickback->clickbacks;
             }
             $lastweek['viral'] = $lastweek['shares'] > 0 && $lastweek['clickbacks'] > 0 ? $lastweek['clickbacks'] / $lastweek['shares'] * 100 . '%' : 'n/a';
             $decodedLastMonth = json_decode($stats['sharesmonth']->getBody());
             $lastmonth = array();
             $lastmonth['shares'] = 0;
             foreach ($decodedLastMonth as $share) {
                 $lastmonth['shares'] += $share->shares;
             }
             $decodedLastMonth = json_decode($stats['clickbacksmonth']->getBody());
             $lastmonth['clickbacks'] = 0;
             foreach ($decodedLastMonth as $clickback) {
                 $lastmonth['clickbacks'] += $clickback->clickbacks;
             }
             $lastmonth['viral'] = $lastmonth['shares'] > 0 && $lastmonth['clickbacks'] ? $lastmonth['clickbacks'] / $lastmonth['shares'] * 100 . '%' : 'n/a';
             $services = array();
             $services['shares'] = json_decode($stats['shares/servicemonth']->getBody());
             if (is_null($services['shares'])) {
                 $services['shares'] = array();
             }
             $services['clickbacks'] = json_decode($stats['clickbacks/servicemonth']->getBody());
             if (is_null($services['clickbacks'])) {
                 $services['clickbacks'] = array();
             }
             $data = array('yesterday' => $yesterday, 'lastweek' => $lastweek, 'lastmonth' => $lastmonth, 'shareurls' => $shareurls, 'clickbackurls' => $clickbackurls, 'domain' => $domain);
         }
     }
     if (!empty($data)) {
         $widgets = $e->getParam('widgets');
         $widgets['addthis']['id'] = 'addthis';
         $widgets['addthis']['title'] = 'AddThis';
         $widgets['addthis']['content'] = $this->addPath(__DIR__ . '/views')->render('dashboard.phtml', $data);
         $e->setParam('widgets', $widgets);
     }
 }
Example #8
0
 /**
  * Paste document
  *
  * @return \Zend\View\Model\JsonModel
  */
 public function pasteAction()
 {
     $parentId = $this->getRouteMatch()->getParam('id', null);
     $session = $this->getSession();
     if (!empty($parentId)) {
         $parentDocument = DocumentModel::fromId($parentId);
         if (empty($parentDocument)) {
             return $this->returnJson(array('success' => false));
         }
     }
     if (!empty($session['document-cut'])) {
         $document = DocumentModel::fromId($session['document-cut']);
         if (empty($document)) {
             return $this->returnJson(array('success' => false));
         }
         if (!empty($parentDocument)) {
             $availableChildren = $parentDocument->getDocumentType()->getDependencies();
             if (!in_array($document->getDocumentType()->getId(), $availableChildren)) {
                 return $this->returnJson(array('success' => false));
             }
         }
         $searchDocument = DocumentModel::fromUrlKey($document->getUrlKey(), $parentId);
         if (!empty($searchDocument)) {
             return $this->returnJson(array('success' => false));
         }
         $document->setParentId($parentId);
         $document->save();
         unset($session['document-cut']);
         return $this->returnJson(array('success' => true));
     } elseif (!empty($session['document-copy'])) {
         $urlKey = $this->getRequest()->getQuery('url_key');
         $searchDocument = DocumentModel::fromUrlKey($urlKey, $parentId);
         if (!empty($searchDocument)) {
             return $this->returnJson(array('success' => false));
         }
         $document = DocumentModel::fromId($session['document-copy']);
         if (empty($document)) {
             return $this->returnJson(array('success' => false));
         }
         if (!empty($parentDocument)) {
             $availableChildren = $parentDocument->getDocumentType()->getDependencies();
             if (!in_array($document->getDocumentType()->getId(), $availableChildren)) {
                 return $this->returnJson(array('success' => false));
             }
         }
         $copyDocument = new DocumentModel();
         $copyDocumentProperties = new Property\Collection();
         $copyDocumentProperties->load(null, null, $document->getId());
         $copyDocument->addData($document->getData());
         $copyDocument->setData('id', null);
         $copyDocument->setParentId($parentId);
         $copyDocument->setName($this->getRequest()->getQuery('name'));
         $copyDocument->setUrlKey($urlKey);
         $copyDocument->save();
         foreach ($copyDocumentProperties->getProperties() as $property) {
             $value = $property->getValueModel();
             if (empty($value)) {
                 continue;
             }
             $copyProperty = new Property\Value\Model();
             $copyProperty->addData($value->getData());
             $copyProperty->setData('id', null);
             $copyProperty->setDocumentId($copyDocument->getId());
             $copyProperty->save();
         }
         return $this->returnJson(array('success' => true));
     } else {
         return $this->returnJson(array('success' => false));
     }
 }