<?php
-class CubeIT_Navigation_Page extends Zend_Navigation_Page_Uri implements Zend_Acl_Resource_Interface {
-
- protected $_editable = false;
- protected $_name;
- protected $_sitemap = true;
- protected $_breadcrumbs = true;
- protected $_controller;
- protected $_action = 'index';
- protected $_params;
- protected $_menu = true;
- protected $_aclEdit = 'inherit';
- protected $_resourceId = 'page';
- protected $_template = 'default';
- protected $_redirection = '';
- protected $_longTitle = '';
- protected $_isRedirection = false;
- protected $_online = true;
- protected $_domain = '';
- protected $_hidden = false;
- protected $_translate = true;
- protected $_variant = 0;
- protected static $_autoUrlDatas;
-
- public function getOnline() {
- return $this->_online;
- }
-
- public function setOnline($online) {
- $this->_online = $online;
- if (!$online) {
- $this->setCustomHtmlAttrib('data-offline', '1');
- } else {
- $this->removeCustomHtmlAttrib('data-offline');
- }
- return $this;
- }
-
- /**
- * @return int
- */
- public function getVariant() {
- return $this->_variant;
- }
-
- /**
- * @param int $variant
- * @return CubeIT_Navigation_Page
- */
- public function setVariant($variant) {
- $this->_variant = $variant;
- return $this;
- }
-
-
- /**
- * @return boolean
- */
- public function getTranslate() {
- return $this->_translate;
- }
-
- /**
- * @param boolean $translate
- * @return $this
- */
- public function setTranslate($translate) {
- $this->_translate = $translate;
- return $this;
- }
-
-
- public function getDomain() {
- return $this->_domain;
- }
-
- public function setDomain($domain) {
- $this->_domain = $domain;
- return $this;
- }
-
- function getBreadcrumbs() {
- return $this->_breadcrumbs;
- }
-
- function setBreadcrumbs($breadcrumbs) {
- $this->_breadcrumbs = $breadcrumbs;
- return $this;
- }
-
- public function getLongTitle() {
- return $this->_longTitle;
- }
-
- public function setLongTitle($longTitle) {
- $this->_longTitle = $longTitle;
- return $this;
- }
-
- public function getRedirection() {
- return $this->_redirection;
- }
-
- public function setRedirection($redirection) {
- $this->_redirection = $redirection;
- return $this;
- }
-
- public function getResourceId() {
- return $this->_resourceId;
- }
-
- public function setResourceId($resourceId) {
- $this->_resourceId = $resourceId;
- return $this;
- }
-
- public function getTemplate() {
- return $this->_template;
- }
-
- public function setTemplate($template) {
- $this->_template = $template;
- return $this;
- }
-
- public function getAclEdit() {
- if ($this->_aclEdit == 'inherit') {
- if (null !== $this->getParent() && $this->getParent() instanceof CubeIT_Navigation_Page) {
- return $this->getParent()->getAclEdit();
- } else {
- return array();
- }
- }
- return explode(',', $this->_aclEdit);
- }
-
- public function setAclEdit($aclEdit) {
- $this->_aclEdit = $aclEdit;
- return $this;
- }
-
- public function getMenu() {
- return $this->_menu;
- }
-
- public function setMenu($_menu) {
- $this->_menu = $_menu;
- return $this;
- }
-
- public function getSitemap() {
- return $this->_sitemap;
- }
-
- public function setSitemap($_sitemap) {
- $this->_sitemap = $_sitemap;
- return $this;
- }
-
- public function getEditable() {
- return $this->_editable;
- }
-
- public function setEditable($_editable) {
- $this->_editable = $_editable;
- return $this;
- }
-
- public function isEditable() {
- return $this->getEditable();
- }
-
- public function getName() {
- return $this->_name;
- }
-
- public function setName($_name) {
- $this->_name = $_name;
- return $this;
- }
-
- public function getController() {
- return $this->_controller;
- }
-
- public function setController($_controller) {
- $this->_controller = $_controller;
-
- return $this;
- }
-
- public function getAction() {
- return $this->_action;
- }
-
- public function setAction($_action) {
- $this->_action = $_action;
- return $this;
- }
-
- public function getParams() {
- return $this->_params;
- }
-
- public function setParams($_params) {
- $this->_params = $_params;
- return $this;
- }
-
- /**
- * @return bool
- */
- public function getHidden() {
- return $this->_hidden;
- }
-
- /**
- * @param bool $hidden
- * @return self
- */
- public function setHidden($hidden) {
- $this->_hidden = $hidden;
- if ($hidden) {
- $this->setCustomHtmlAttrib('data-hidden', '1');
- } else {
- $this->removeCustomHtmlAttrib('data-hidden');
- }
- return $this;
- }
-
- public function getPageId() {
- return $this->getId();
- }
-
- public function getFirstEditablePageId() {
- if ($this->isEditable()) {
- return $this->getPageId();
- }
- if ($this->getParent() instanceof CubeIT_Navigation_Page) {
- return $this->getParent()->getFirstEditablePageId();
- }
- return null;
- }
-
- /**
- *
- * @return array
- */
- public function getContents() {
- profile(__FILE__, __LINE__, 'Get page contents');
- $form = $this->getFormInstance();
- if (is_null($form)) {
- return null;
- }
- $res = array();
-
- profile(__FILE__, __LINE__, 'Get static values');
- foreach ($form->getStaticValues() as $key => $value) {
- $res[$key] = $value;
- }
-
- profile(__FILE__, __LINE__, 'Get dynamic values');
- foreach ($form as $element) {
- $k = $element->getName();
- $v = $form->getValue($k);
-
- $res[$k] = $v;
- }
-
-
- return $res;
- }
-
- /**
- * @return CubeIT_Form_CMS
- */
- public function getFormInstance() {
- if ($this instanceof CubeIT_Navigation_Page_Locale) {
- $locale = $this->getLocale();
- $cacheId = 'form_instance_' . $locale . '_' . sha1($this->getFinalUri());
- } else {
- $cacheId = 'form_instance_' . sha1($this->getFinalUri());
- }
-
- $bootstrap = Bootstrap::getInstance();
- $cache = $bootstrap->getCoreCache();
- if (!($form = $cache->load($cacheId))) {
- profile(__FILE__, __LINE__, 'Get form instance::init');
-
- $db = $bootstrap->getDb();
- if ($this->getTemplate() == '') {
- profile(__FILE__, __LINE__, 'Get form instance::no template');
- $parent = $this->getParent();
- if ($parent instanceof CubeIT_Navigation_Page) {
- return $parent->getFormInstance($db);
- }
- return new CubeIT_Form_CMS();
- }
-
- profile(__FILE__, __LINE__, 'Get form instance::find class name');
- $suffix = "_Form_CMS_";
- $e = explode('_', $this->getTemplate());
- foreach ($e as $k => $v) {
- $suffix .= ucfirst($v);
- }
-
- $prefixs = array($bootstrap->getAppNamespace(), 'CubeIT');
- /* @var $form CubeIT_Form_CMS */
- foreach ($prefixs as $p) {
- $class = $p . $suffix;
- if (class_exists($class, true)) {
- profile(__FILE__, __LINE__, 'Get form instance::instanciate ' . $class);
- $form = new $class();
- break;
- }
- }
-
- if (!isset($form) || is_null($form) || !$form) {
- return null;
- }
-
- if (!is_null($db)) {
- profile(__FILE__, __LINE__, 'Get form instance::get data');
- if ($this instanceof CubeIT_Navigation_Page_Locale) {
- $form->setLocale($locale);
- }
- $form->setVariant($this->getVariant());
- $form->setPageId($this->getPageId());
- $form->loadDatasFromDatabase($db);
- }
- $cache->save($form, $cacheId, ['contents', 'forms']);
- }
-
- return $form;
- }
-
- public function saveContents($data, $skipConsistencyChecks = false) {
- $subtree = array($data['id']);
- $it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST);
- foreach ($it as $sub) {
- if ($sub instanceof CubeIT_Navigation_Page && $sub->isEditable()) {
- $subtree[] = $sub->getPageId();
- }
- }
- $subtree = array_unique($subtree);
-
- $form = $this->getFormInstance();
- $form->setDefaults($data);
-
- if (!$skipConsistencyChecks) {
- // If the form supports it, do extra consistency checks on it before saving
- if ($form instanceof CubeIT_Form_CMSBase) {
-
- if (!$form->canBeSaved()) {
- fb('saveContents() aborted due to consistency check failure...');
- return false;
- }
- }
- }
-
- $db = Bootstrap::getInstance()->getDb();
-
- // Get the list of existing datas
- $exists = array();
- $rows = $db->select()->from('cubeit_content')->query()->fetchAll();
- foreach ($rows as $r) {
- $exists[$r->locale][$r->id][$r->label][$r->variant] = true;
- }
-
- $toInsert = array();
-
- foreach ($form->getValues() as $k => $v) {
- $propagatelocale = isset($data['propagatelocale'][$k]) && $data['propagatelocale'][$k] == 1;
- $propagatesubtree = isset($data['propagatesubtree'][$k]) && $data['propagatesubtree'][$k] == 1;
-
- if ($k == 'id' || $k == 'locale' || $k == 'variant') {
- continue;
- }
-
- if (is_array($v) || is_object($v)) {
- if (isset($v['cubeitbase'])) {
- unset($v['cubeitbase']);
- $v = json_encode(array_values($v));
- } else {
- $v = json_encode($v);
- }
- }
-
- if ($propagatelocale) {
- $localesToPropagate = Bootstrap::getInstance()->getAppLocales();
- } else {
- $localesToPropagate = array($data['locale']);
- }
-
- if ($propagatesubtree) {
- $idsToPropagate = $subtree;
- } else {
- $idsToPropagate = array($data['id']);
- }
-
- foreach ($localesToPropagate as $locale) {
- foreach ($idsToPropagate as $id) {
- $toInsert[] = array('label' => $k, 'content' => $v, 'id' => $id, 'locale' => $locale, 'variant' => $data['variant']);
- }
- }
- }
-
- foreach ($toInsert as $datas) {
-
- try {
- if (!isset($exists[$datas['locale']][$datas['id']][$datas['label']][$datas['variant']])) {
- $exists[$datas['locale']][$datas['id']][$datas['label']][$datas['variant']] = true;
- $db->insert('cubeit_content', $datas);
- } else {
- $where = $db->quoteInto('locale = ?', $datas['locale']);
- $where .= ' AND ' . $db->quoteInto('id = ?', $datas['id']);
- $where .= ' AND ' . $db->quoteInto('label = ?', $datas['label']);
- $where .= ' AND ' . $db->quoteInto('variant = ?', $datas['variant']);
-
- $db->update('cubeit_content', $datas, $where);
- }
- } catch (Exception $e) {
-
- }
- }
- }
-
- /**
- *
- * @param string $uri
- * @param Zend_Db_Adapter_Abstract $db
- * @return Zend_Navigation_Page
- */
- public function findOneByFormerUri($uri, Zend_Db_Adapter_Abstract $db = null) {
- if (is_null($db)) {
- $db = Bootstrap::getInstance()->getDb();
- }
- $r = $db->select()->from('cubeit_url')->where('uri = ?', $uri)->query()->fetch();
- die($r->id);
-
- if (!is_null($r) && is_object($r)) {
- return $this->findOneBy('id', $r->id);
- }
- return null;
- }
-
- /**
- * @return int
- */
- public function getLevel() {
- $level = 0;
- $page = $this;
- while ($page instanceof Zend_Navigation_Page && !is_null($page->getParent())) {
- $page = $page->getParent();
- $level++;
- }
- return $level;
- }
-
- /**
- *
- * @param int $level
- * @return Zend_Navigation_Page
- */
- public function getParentAt($level) {
- $page = $this;
-
- for ($i = $this->getLevel() - 1; $i > $level; $i--) {
- $parent = $page->getParent();
- if (null === $parent) {
- return $page;
- }
- $page = $parent;
- }
- return $page;
- }
-
- public function getFinalUri() {
- if (in_array($this->getTemplate(), Bootstrap::getInstance()->_getNextRedirectionTemplates())) {
- $pages = $this->getPages();
- foreach ($pages as $p) {
- return $p->getUri();
- }
- } else {
- $root = $this->getParentAt(-1);
- $id = $this->_makeFullId($this->getRedirection());
- $p = $root->findOneById($id);
- if (null !== $p) {
- return $p->getUri();
- }
- }
- return $this->getUri();
- }
-
- protected function _makeFullId($id) {
- return trim($id, '/');
- }
-
- /**
- *
- * @return array
- */
- public function getSiblings() {
- $parent = $this->getParent();
- return $parent->getPages();
- }
-
- /**
- * @return Zend_Navigation_Page
- */
- public function getNextSibling() {
- $siblings = array_values($this->getSiblings());
- $k = array_search($this, $siblings, true);
- if (isset($siblings[$k + 1])) {
- return $siblings[$k + 1];
- }
- return null;
- }
-
- /**
- * @return Zend_Navigation_Page
- */
- public function getPrevSibling() {
- $siblings = array_values($this->getSiblings());
- $k = array_search($this, $siblings, true);
- if (isset($siblings[$k - 1])) {
- return $siblings[$k - 1];
- }
- return null;
- }
-
- /**
- * @return Zend_Navigation_Page
- */
- public function getFirstChild() {
- $children = $this->getPages();
- if (is_null($children)) {
- return null;
- }
- foreach ($children as $child) {
- return $child;
- }
- return null;
- }
-
- /**
- * @return this[]
- */
- public function getParents($reverse = false) {
- $parents = array();
- $page = $this;
- while ($page instanceof Zend_Navigation_Page) {
- $parent = $page->getParent();
- if (is_null($parent)) {
- break;
- }
- $parents[] = $parent;
- $page = $parent;
- }
- if ($reverse) {
- $parents = array_reverse($parents);
- }
-
- return $parents;
- }
-
- /**
- * return boolean
- */
- public function findInParents($key, $value) {
- $parents = $this->getParents();
- foreach ($parents as $page) {
- /* @var $page Zend_Navigation_Page */
-
- if ($page instanceof Zend_Navigation_Page && $page->get($key) == $value) {
- return $page;
- }
- }
- return false;
- }
-
- public function findClosestBy($key, $value) {
- $page = $this;
- while (true) {
- if (!$page instanceof Zend_Navigation_Page) {
- return false;
- }
- if ($page->get($key) == $value) {
- return $page;
- }
- $page = $page->getParent();
- }
- }
-
- public function setAutoUri($datas, $template, $locale = null) {
- return $this->setUri(self::generateAutoUri($datas, $template, $locale));
- }
-
- public static function generateAutoUri($datas, $template, $locale = null) {
- self::$_autoUrlDatas = CubeIT_Util_Cms::unserialize($datas, $locale);
-
- $u = preg_replace_callback('|\%([a-z]+)\%|', array('self', '_replaceAutoUrl'), $template);
- if (!is_null($locale)) {
- $domain = Bootstrap::getInstance()->getLocaleDomain($locale);
- if ($domain['domain']) {
- $res = '';
- if ($domain['segment']) {
- $res = '/' . $domain['segment'];
- }
- $res .= '/' . $u;
- return $res;
- } else {
- return '/' . $locale . '/' . $u;
- }
- } else {
- return $u;
- }
- }
-
- protected static function _replaceAutoUrl($m) {
-
- $key = $m[1];
-
- if (is_object(self::$_autoUrlDatas)) {
- $url = self::$_autoUrlDatas->$key;
- } else {
- $url = self::$_autoUrlDatas[$m[1]];
- }
-
- return CubeIT_Text::str2URL($url);
- }
-
- public function getHref() {
- $href = parent::getHref();
- if (CubeIT_Util_Url::isDistant($href) || strpos($href, 'http') === 0) {
- return $href;
- }
- $domain = $this->getDomain();
- if (!$domain) {
- return $href;
- }
- return SITE_PROTOCOL . $domain . $href;
- }
-
- public function getAbsoluteHref() {
- $href = $this->getHref();
- if (CubeIT_Util_Url::isDistant($href) || strpos($href, 'http') === 0) {
- return $href;
- }
- return SITE_PROTOCOL . Bootstrap::getInstance()->getOption('webhost') . $href;
- }
-
-
- /**
- * @param $locale
- * @return $this
- */
- public function getLocaleVariant($locale) {
- return $this;
- }
-
- public function isRoot() {
- return !$this->hasParent();
- }
-
- public function hasParent() {
- return null !== $this->getParent();
- }
-
-
- /**
- * @return CubeIT_Navigation
- */
- public function getRoot() {
- return $this->getParentAt(0)->getParent();
- }
+class CubeIT_Navigation_Page extends Zend_Navigation_Page_Uri implements Zend_Acl_Resource_Interface
+{
+
+ protected $_editable = false;
+ protected $_name;
+ protected $_sitemap = true;
+ protected $_breadcrumbs = true;
+ protected $_controller;
+ protected $_action = 'index';
+ protected $_params;
+ protected $_menu = true;
+ protected $_aclEdit = 'inherit';
+ protected $_resourceId = 'page';
+ protected $_template = 'default';
+ protected $_redirection = '';
+ protected $_longTitle = '';
+ protected $_isRedirection = false;
+ protected $_online = true;
+ protected $_domain = '';
+ protected $_hidden = false;
+ protected $_translate = true;
+ protected $_variant = 0;
+ protected static $_autoUrlDatas;
+
+ public function getOnline()
+ {
+ return $this->_online;
+ }
+
+ public function setOnline($online)
+ {
+ $this->_online = $online;
+ if (!$online) {
+ $this->setCustomHtmlAttrib('data-offline', '1');
+ } else {
+ $this->removeCustomHtmlAttrib('data-offline');
+ }
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getVariant()
+ {
+ return $this->_variant;
+ }
+
+ /**
+ * @param int $variant
+ * @return CubeIT_Navigation_Page
+ */
+ public function setVariant($variant)
+ {
+ $this->_variant = $variant;
+ return $this;
+ }
+
+
+ /**
+ * @return boolean
+ */
+ public function getTranslate()
+ {
+ return $this->_translate;
+ }
+
+ /**
+ * @param boolean $translate
+ * @return $this
+ */
+ public function setTranslate($translate)
+ {
+ $this->_translate = $translate;
+ return $this;
+ }
+
+
+ public function getDomain()
+ {
+ return $this->_domain;
+ }
+
+ public function setDomain($domain)
+ {
+ $this->_domain = $domain;
+ return $this;
+ }
+
+ function getBreadcrumbs()
+ {
+ return $this->_breadcrumbs;
+ }
+
+ function setBreadcrumbs($breadcrumbs)
+ {
+ $this->_breadcrumbs = $breadcrumbs;
+ return $this;
+ }
+
+ public function getLongTitle()
+ {
+ return $this->_longTitle;
+ }
+
+ public function setLongTitle($longTitle)
+ {
+ $this->_longTitle = $longTitle;
+ return $this;
+ }
+
+ public function getRedirection()
+ {
+ return $this->_redirection;
+ }
+
+ public function setRedirection($redirection)
+ {
+ $this->_redirection = $redirection;
+ return $this;
+ }
+
+ public function getResourceId()
+ {
+ return $this->_resourceId;
+ }
+
+ public function setResourceId($resourceId)
+ {
+ $this->_resourceId = $resourceId;
+ return $this;
+ }
+
+ public function getTemplate()
+ {
+ return $this->_template;
+ }
+
+ public function setTemplate($template)
+ {
+ $this->_template = $template;
+ return $this;
+ }
+
+ public function getAclEdit()
+ {
+ if ($this->_aclEdit == 'inherit') {
+ if (null !== $this->getParent() && $this->getParent() instanceof CubeIT_Navigation_Page) {
+ return $this->getParent()->getAclEdit();
+ } else {
+ return array();
+ }
+ }
+ return explode(',', $this->_aclEdit);
+ }
+
+ public function setAclEdit($aclEdit)
+ {
+ $this->_aclEdit = $aclEdit;
+ return $this;
+ }
+
+ public function getMenu()
+ {
+ return $this->_menu;
+ }
+
+ public function setMenu($_menu)
+ {
+ $this->_menu = $_menu;
+ return $this;
+ }
+
+ public function getSitemap()
+ {
+ return $this->_sitemap;
+ }
+
+ public function setSitemap($_sitemap)
+ {
+ $this->_sitemap = $_sitemap;
+ return $this;
+ }
+
+ public function getEditable()
+ {
+ return $this->_editable;
+ }
+
+ public function setEditable($_editable)
+ {
+ $this->_editable = $_editable;
+ return $this;
+ }
+
+ public function isEditable()
+ {
+ return $this->getEditable();
+ }
+
+ public function getName()
+ {
+ return $this->_name;
+ }
+
+ public function setName($_name)
+ {
+ $this->_name = $_name;
+ return $this;
+ }
+
+ public function getController()
+ {
+ return $this->_controller;
+ }
+
+ public function setController($_controller)
+ {
+ $this->_controller = $_controller;
+
+ return $this;
+ }
+
+ public function getAction()
+ {
+ return $this->_action;
+ }
+
+ public function setAction($_action)
+ {
+ $this->_action = $_action;
+ return $this;
+ }
+
+ public function getParams()
+ {
+ return $this->_params;
+ }
+
+ public function setParams($_params)
+ {
+ $this->_params = $_params;
+ return $this;
+ }
+
+ /**
+ * @return bool
+ */
+ public function getHidden()
+ {
+ return $this->_hidden;
+ }
+
+ /**
+ * @param bool $hidden
+ * @return self
+ */
+ public function setHidden($hidden)
+ {
+ $this->_hidden = $hidden;
+ if ($hidden) {
+ $this->setCustomHtmlAttrib('data-hidden', '1');
+ } else {
+ $this->removeCustomHtmlAttrib('data-hidden');
+ }
+ return $this;
+ }
+
+ public function getPageId()
+ {
+ return $this->getId();
+ }
+
+ public function getFirstEditablePageId()
+ {
+ if ($this->isEditable()) {
+ return $this->getPageId();
+ }
+ if ($this->getParent() instanceof CubeIT_Navigation_Page) {
+ return $this->getParent()->getFirstEditablePageId();
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @return array
+ */
+ public function getContents()
+ {
+ profile(__FILE__, __LINE__, 'Get page contents');
+ $form = $this->getFormInstance();
+ if (is_null($form)) {
+ return null;
+ }
+ $res = array();
+
+ profile(__FILE__, __LINE__, 'Get static values');
+ foreach ($form->getStaticValues() as $key => $value) {
+ $res[$key] = $value;
+ }
+
+ profile(__FILE__, __LINE__, 'Get dynamic values');
+ foreach ($form as $element) {
+ $k = $element->getName();
+ $v = $form->getValue($k);
+
+ $res[$k] = $v;
+ }
+
+
+ return $res;
+ }
+
+ /**
+ * @return CubeIT_Form_CMS
+ */
+ public function getFormInstance()
+ {
+ if ($this instanceof CubeIT_Navigation_Page_Locale) {
+ $locale = $this->getLocale();
+// $cacheId = 'form_instance_' . $locale . '_' . sha1($this->getFinalUri());
+// } else {
+ }
+// $cacheId = 'form_instance_' . sha1($this->getFinalUri());
+// }
+// if (Bootstrap::getInstance()->isAllowed('administration')) {
+// $cacheId .= '_admin';
+// }else{
+// $cacheId .= '_user';
+// }
+
+ $bootstrap = Bootstrap::getInstance();
+// $cache = $bootstrap->getCoreCache();
+// if (!($form = $cache->load($cacheId))) {
+ profile(__FILE__, __LINE__, 'Get form instance::init');
+
+ $db = $bootstrap->getDb();
+ if ($this->getTemplate() == '') {
+ profile(__FILE__, __LINE__, 'Get form instance::no template');
+ $parent = $this->getParent();
+ if ($parent instanceof CubeIT_Navigation_Page) {
+ return $parent->getFormInstance($db);
+ }
+ return new CubeIT_Form_CMS();
+ }
+
+ profile(__FILE__, __LINE__, 'Get form instance::find class name');
+ $suffix = "_Form_CMS_";
+ $e = explode('_', $this->getTemplate());
+ foreach ($e as $v) {
+ $suffix .= ucfirst($v);
+ }
+
+ $prefixs = array($bootstrap->getAppNamespace(), 'CubeIT');
+ /* @var $form CubeIT_Form_CMS */
+ foreach ($prefixs as $p) {
+ $class = $p . $suffix;
+ if (class_exists($class, true)) {
+ profile(__FILE__, __LINE__, 'Get form instance::instanciate ' . $class);
+ $form = new $class();
+ break;
+ }
+ }
+
+ if (!isset($form) || is_null($form) || !$form) {
+ return null;
+ }
+
+ if (!is_null($db)) {
+ profile(__FILE__, __LINE__, 'Get form instance::get data');
+ if ($this instanceof CubeIT_Navigation_Page_Locale) {
+ $form->setLocale($locale);
+ }
+ $form->setVariant($this->getVariant());
+ $form->setPageId($this->getPageId());
+ $form->loadDatasFromDatabase($db);
+ }
+ // $cache->save($form, $cacheId, ['contents', 'forms']);
+ // }
+
+ return $form;
+ }
+
+ public function saveContents($data, $skipConsistencyChecks = false)
+ {
+ $subtree = array($data['id']);
+ $it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST);
+ foreach ($it as $sub) {
+ if ($sub instanceof CubeIT_Navigation_Page && $sub->isEditable()) {
+ $subtree[] = $sub->getPageId();
+ }
+ }
+ $subtree = array_unique($subtree);
+
+ $form = $this->getFormInstance();
+ $form->setDefaults($data);
+
+ if (!$skipConsistencyChecks) {
+ // If the form supports it, do extra consistency checks on it before saving
+ if ($form instanceof CubeIT_Form_CMSBase) {
+
+ if (!$form->canBeSaved()) {
+ fb('saveContents() aborted due to consistency check failure...');
+ return false;
+ }
+ }
+ }
+
+ $db = Bootstrap::getInstance()->getDb();
+
+ // Get the list of existing datas
+ $exists = array();
+ $rows = $db->select()->from('cubeit_content')->query()->fetchAll();
+ foreach ($rows as $r) {
+ $exists[$r->locale][$r->id][$r->label][$r->variant] = true;
+ }
+
+ $toInsert = array();
+
+ foreach ($form->getValues() as $k => $v) {
+ $propagatelocale = isset($data['propagatelocale'][$k]) && $data['propagatelocale'][$k] == 1;
+ $propagatesubtree = isset($data['propagatesubtree'][$k]) && $data['propagatesubtree'][$k] == 1;
+
+ if ($k == 'id' || $k == 'locale' || $k == 'variant') {
+ continue;
+ }
+
+ if (is_array($v) || is_object($v)) {
+ if (isset($v['cubeitbase'])) {
+ unset($v['cubeitbase']);
+ $v = json_encode(array_values($v));
+ } else {
+ $v = json_encode($v);
+ }
+ }
+
+ if ($propagatelocale) {
+ $localesToPropagate = Bootstrap::getInstance()->getAppLocales();
+ } else {
+ $localesToPropagate = array($data['locale']);
+ }
+
+ if ($propagatesubtree) {
+ $idsToPropagate = $subtree;
+ } else {
+ $idsToPropagate = array($data['id']);
+ }
+
+ foreach ($localesToPropagate as $locale) {
+ foreach ($idsToPropagate as $id) {
+ $toInsert[] = array('label' => $k, 'content' => $v, 'id' => $id, 'locale' => $locale, 'variant' => $data['variant']);
+ }
+ }
+ }
+
+ foreach ($toInsert as $datas) {
+
+ try {
+ if (!isset($exists[$datas['locale']][$datas['id']][$datas['label']][$datas['variant']])) {
+ $exists[$datas['locale']][$datas['id']][$datas['label']][$datas['variant']] = true;
+ $db->insert('cubeit_content', $datas);
+ } else {
+ $where = $db->quoteInto('locale = ?', $datas['locale']);
+ $where .= ' AND ' . $db->quoteInto('id = ?', $datas['id']);
+ $where .= ' AND ' . $db->quoteInto('label = ?', $datas['label']);
+ $where .= ' AND ' . $db->quoteInto('variant = ?', $datas['variant']);
+
+ $db->update('cubeit_content', $datas, $where);
+ }
+ } catch (Exception $e) {
+
+ }
+ }
+ }
+
+ /**
+ *
+ * @param string $uri
+ * @param Zend_Db_Adapter_Abstract $db
+ * @return Zend_Navigation_Page
+ */
+ public function findOneByFormerUri($uri, Zend_Db_Adapter_Abstract $db = null)
+ {
+ if (is_null($db)) {
+ $db = Bootstrap::getInstance()->getDb();
+ }
+ $r = $db->select()->from('cubeit_url')->where('uri = ?', $uri)->query()->fetch();
+ die($r->id);
+
+ if (!is_null($r) && is_object($r)) {
+ return $this->findOneBy('id', $r->id);
+ }
+ return null;
+ }
+
+ /**
+ * @return int
+ */
+ public function getLevel()
+ {
+ $level = 0;
+ $page = $this;
+ while ($page instanceof Zend_Navigation_Page && !is_null($page->getParent())) {
+ $page = $page->getParent();
+ $level++;
+ }
+ return $level;
+ }
+
+ /**
+ *
+ * @param int $level
+ * @return Zend_Navigation_Page
+ */
+ public function getParentAt($level)
+ {
+ $page = $this;
+
+ for ($i = $this->getLevel() - 1; $i > $level; $i--) {
+ $parent = $page->getParent();
+ if (null === $parent) {
+ return $page;
+ }
+ $page = $parent;
+ }
+ return $page;
+ }
+
+ public function getFinalUri()
+ {
+ if (in_array($this->getTemplate(), Bootstrap::getInstance()->_getNextRedirectionTemplates())) {
+ $pages = $this->getPages();
+ foreach ($pages as $p) {
+ return $p->getUri();
+ }
+ } else {
+ $root = $this->getParentAt(-1);
+ $id = $this->_makeFullId($this->getRedirection());
+ $p = $root->findOneById($id);
+ if (null !== $p) {
+ return $p->getUri();
+ }
+ }
+ return $this->getUri();
+ }
+
+ protected function _makeFullId($id)
+ {
+ return trim($id, '/');
+ }
+
+ /**
+ *
+ * @return array
+ */
+ public function getSiblings()
+ {
+ $parent = $this->getParent();
+ return $parent->getPages();
+ }
+
+ /**
+ * @return Zend_Navigation_Page
+ */
+ public function getNextSibling()
+ {
+ $siblings = array_values($this->getSiblings());
+ $k = array_search($this, $siblings, true);
+ if (isset($siblings[$k + 1])) {
+ return $siblings[$k + 1];
+ }
+ return null;
+ }
+
+ /**
+ * @return Zend_Navigation_Page
+ */
+ public function getPrevSibling()
+ {
+ $siblings = array_values($this->getSiblings());
+ $k = array_search($this, $siblings, true);
+ if (isset($siblings[$k - 1])) {
+ return $siblings[$k - 1];
+ }
+ return null;
+ }
+
+ /**
+ * @return Zend_Navigation_Page
+ */
+ public function getFirstChild()
+ {
+ $children = $this->getPages();
+ if (is_null($children)) {
+ return null;
+ }
+ foreach ($children as $child) {
+ return $child;
+ }
+ return null;
+ }
+
+ /**
+ * @return this[]
+ */
+ public function getParents($reverse = false)
+ {
+ $parents = array();
+ $page = $this;
+ while ($page instanceof Zend_Navigation_Page) {
+ $parent = $page->getParent();
+ if (is_null($parent)) {
+ break;
+ }
+ $parents[] = $parent;
+ $page = $parent;
+ }
+ if ($reverse) {
+ $parents = array_reverse($parents);
+ }
+
+ return $parents;
+ }
+
+ /**
+ * return boolean
+ */
+ public function findInParents($key, $value)
+ {
+ $parents = $this->getParents();
+ foreach ($parents as $page) {
+ /* @var $page Zend_Navigation_Page */
+
+ if ($page instanceof Zend_Navigation_Page && $page->get($key) == $value) {
+ return $page;
+ }
+ }
+ return false;
+ }
+
+ public function findClosestBy($key, $value)
+ {
+ $page = $this;
+ while (true) {
+ if (!$page instanceof Zend_Navigation_Page) {
+ return false;
+ }
+ if ($page->get($key) == $value) {
+ return $page;
+ }
+ $page = $page->getParent();
+ }
+ }
+
+ public function setAutoUri($datas, $template, $locale = null)
+ {
+ return $this->setUri(self::generateAutoUri($datas, $template, $locale));
+ }
+
+ public static function generateAutoUri($datas, $template, $locale = null)
+ {
+ self::$_autoUrlDatas = CubeIT_Util_Cms::unserialize($datas, $locale);
+
+ $u = preg_replace_callback('|\%([a-z]+)\%|', array('self', '_replaceAutoUrl'), $template);
+ if (!is_null($locale)) {
+ $domain = Bootstrap::getInstance()->getLocaleDomain($locale);
+ if ($domain['domain']) {
+ $res = '';
+ if ($domain['segment']) {
+ $res = '/' . $domain['segment'];
+ }
+ $res .= '/' . $u;
+ return $res;
+ } else {
+ return '/' . $locale . '/' . $u;
+ }
+ } else {
+ return $u;
+ }
+ }
+
+ protected static function _replaceAutoUrl($m)
+ {
+
+ $key = $m[1];
+
+ if (is_object(self::$_autoUrlDatas)) {
+ $url = self::$_autoUrlDatas->$key;
+ } else {
+ $url = self::$_autoUrlDatas[$m[1]];
+ }
+
+ return CubeIT_Text::str2URL($url);
+ }
+
+ public function getHref()
+ {
+ $href = parent::getHref();
+ if (CubeIT_Util_Url::isDistant($href) || strpos($href, 'http') === 0) {
+ return $href;
+ }
+ $domain = $this->getDomain();
+ if (!$domain) {
+ return $href;
+ }
+ return SITE_PROTOCOL . $domain . $href;
+ }
+
+ public function getAbsoluteHref()
+ {
+ $href = $this->getHref();
+ if (CubeIT_Util_Url::isDistant($href) || strpos($href, 'http') === 0) {
+ return $href;
+ }
+ return SITE_PROTOCOL . Bootstrap::getInstance()->getOption('webhost') . $href;
+ }
+
+
+ /**
+ * @param $locale
+ * @return $this
+ */
+ public function getLocaleVariant($locale)
+ {
+ return $this;
+ }
+
+ public function isRoot()
+ {
+ return !$this->hasParent();
+ }
+
+ public function hasParent()
+ {
+ return null !== $this->getParent();
+ }
+
+
+ /**
+ * @return CubeIT_Navigation
+ */
+ public function getRoot()
+ {
+ return $this->getParentAt(0)->getParent();
+ }
}