PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/backoffice/services/ContextMenuService.class.php

http://pagizer-cms.googlecode.com/
PHP | 250 lines | 169 code | 30 blank | 51 comment | 31 complexity | 43fdd574c6a4298c4580e53287fe3e6d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of the Pagizer package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) 2010 Advisa (http://www.advisa.fr)
  9. * @author Pagizer Core Team <team@pagizer.org>
  10. * @package Pagizer
  11. * @subpackage backoffice
  12. */
  13. /**
  14. * Service to display the context menu
  15. */
  16. class m_backoffice_services_ContextMenuService extends f_core_Service
  17. {
  18. private static $instance;
  19. /**
  20. * @return m_backoffice_services_MenuService
  21. */
  22. private function __construct()
  23. {
  24. return $this;
  25. }
  26. /**
  27. * Get singleton instance
  28. *
  29. * @return m_backoffice_services_MenuService
  30. */
  31. public static function getInstance()
  32. {
  33. if(self::$instance === null)
  34. {
  35. self::$instance = new self();
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * Get context menu items associated to a document by his id
  41. * @param $document
  42. * @return unknown_type
  43. */
  44. public function getContextMenuItems(f_document_BaseDocument $document)
  45. {
  46. $elements = $this->getDocumentContextMenuItems($document);
  47. if(is_null($elements))
  48. {
  49. return array();
  50. }
  51. else
  52. {
  53. return $this->getItemsArrayFromXmlElements($document, $elements);
  54. }
  55. }
  56. public function getRightClickItems(f_document_BaseDocument $document)
  57. {
  58. if($document->getDocumentModel() == "modules_backoffice/rootNode")
  59. {
  60. return null;
  61. }
  62. else
  63. {
  64. $items = array();
  65. if($document->isLocalized())
  66. {
  67. $original = $document->getVoDocument();
  68. if($original->getDocumentModel() == "modules_website/website")
  69. {
  70. $rootNode = $original;
  71. }
  72. else
  73. {
  74. $rootNodes = f_relation_ManagerNew::getInstance($original)->getParents()->withModel("website/website")->withLang($original->getLang())->retrieveDocuments();
  75. $rootNode = end($rootNodes);
  76. }
  77. if($rootNode == false)
  78. {
  79. return null;
  80. }
  81. foreach($rootNode->getAllLangs() as $lang)
  82. {
  83. if($lang != $document->getLang())
  84. {
  85. $translatedDocument = $document->getTranslation($lang);
  86. if($translatedDocument->isTranslated())
  87. {
  88. $item["label"] = $lang." / ".$translatedDocument->getLabel();
  89. $item["icon"] = $translatedDocument->getIcon();
  90. $item["href"] = $translatedDocument->getEditUrl($lang);
  91. }
  92. else
  93. {
  94. $item["label"] = $lang." / ".$document->getLabel();
  95. $item["icon"] = "message_add.png";
  96. $item["href"] = $translatedDocument->getEditUrl($lang);
  97. }
  98. $items[] = $item;
  99. }
  100. }
  101. if(method_exists($original, "getWebsiteId"))
  102. {
  103. $item["label"] = "Ajouter une langue au site";
  104. $item["icon"] = "message_add.png";
  105. $item["href"] = f_core_Routing::getUrl("translateWebsite", array("docId" => $original->getWebsiteId()));
  106. $item["class"] = "nyroModal";
  107. $items[] = $item;
  108. }
  109. else if($document->getDocumentModel() == "modules_website/website")
  110. {
  111. $item["label"] = "Ajouter une langue au site";
  112. $item["icon"] = "message_add.png";
  113. $item["href"] = f_core_Routing::getUrl("translateWebsite", array("docId" => $document->getUniqueId()));
  114. $item["class"] = "nyroModal";
  115. $items[] = $item;
  116. }
  117. }
  118. if(count($items) > 0)
  119. {
  120. return $items;
  121. }
  122. return null;
  123. }
  124. }
  125. /**
  126. * Get the context actions menu for a given document
  127. *
  128. * @param integer $documentId
  129. * @return string
  130. */
  131. private function getDocumentContextMenuItems(f_document_BaseDocument $document)
  132. {
  133. $docModel = substr($document->getDocumentModel(),8);
  134. list($module, $documentName) = explode("/", $docModel);
  135. if($document->getModel() == 'modules_website/website')
  136. {
  137. $module = $document->getConfigModule();
  138. }
  139. else
  140. {
  141. if($module == "backoffice")
  142. {
  143. $module = $document->getModule();
  144. }
  145. }
  146. return $this->getXmlElements($module, $documentName);
  147. }
  148. /**
  149. * Get the context actions menu for a given root node
  150. *
  151. * @param $rootNode m_backoffice_documents_RootNode
  152. * @return unknown_type
  153. */
  154. private function getRootNodeContextMenuItems(m_backoffice_documents_RootNode $rootNode)
  155. {
  156. return $this->getXmlElements($rootNode->getModule(), "rootNode");
  157. }
  158. /**
  159. * Get XML items declared for a given module and model
  160. * @param $module
  161. * @param $documentModel
  162. * @return null|array f_xml_XmlElement
  163. */
  164. private function getXmlElements($module, $documentModel)
  165. {
  166. $xmlFile = MODULES_DIR.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."contextMenu".DIRECTORY_SEPARATOR.$documentModel.".xml";
  167. if(is_file($xmlFile))
  168. {
  169. $elements = f_xml_Xpath::getInstance($xmlFile)->getElementsFromXpath("item");
  170. if(is_array($elements) && count($elements) > 0)
  171. {
  172. return $elements;
  173. }
  174. }
  175. return null;
  176. }
  177. /**
  178. * Retrieve a formatted array from xml elements
  179. * @param $document
  180. * @param $xmlElements
  181. * @return array
  182. */
  183. private function getItemsArrayFromXmlElements($document, $xmlElements)
  184. {
  185. $items = array();
  186. if($document->getModel() == 'modules_website/website')
  187. {
  188. $module = $document->getConfigModule();
  189. }
  190. else
  191. {
  192. $docModel = substr($document->getDocumentModel(),8);
  193. list($module, $documentName) = explode("/", $docModel);
  194. if($module == "backoffice")
  195. {
  196. $module = $document->getModule();
  197. }
  198. }
  199. foreach($xmlElements as $xmlElement)
  200. {
  201. $right = $xmlElement->getAttributeValue('right');
  202. $item["icon"] = $xmlElement->getAttributeValue('icon');
  203. $item["label"] = $xmlElement->getAttributeValue('label');
  204. $item["type"] = $xmlElement->getAttributeValue('type');
  205. $item["href"] = f_core_Routing::getUrl($xmlElement->getAttributeValue('route'), array("docLang" => strtolower($document->getLang()), "docId" => $document->getUniqueId(), "configModule" => $module));
  206. $item["tooltip"] = $xmlElement->getAttributeValue('tooltip');
  207. if(!is_null($right))
  208. {
  209. $userContext = f_context_User::getInstance();
  210. $user = $userContext->getUser();
  211. if(!$userContext->hasRight($module.'_'.$right) || !$user->hasDocRight($document, $module, strtolower($right)))
  212. {
  213. continue;
  214. }
  215. }
  216. $items[] = $item;
  217. }
  218. return $items;
  219. }
  220. }