CatalyzToolbar.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. class CatalyzToolbar {
  3. protected static $instance;
  4. protected function __construct()
  5. {
  6. }
  7. /**
  8. *
  9. * @return CatalyzToolbar
  10. */
  11. static function instance()
  12. {
  13. if (empty(self::$instance)) {
  14. self::$instance = new CatalyzToolbar();
  15. }
  16. return self::$instance;
  17. }
  18. protected function getDefaultActions($page)
  19. {
  20. $canAdd = count($page->getValidSubClasses()) > 0;
  21. $canEdit = true;
  22. $ContentTree = ContentTree::instance(Catalyz::getPageCulture());
  23. $ContentTree->build();
  24. $ContentTree->initializeCurrentNodeById($page->getNodeId());
  25. $CurrentNode = $ContentTree->getCurrentNode();
  26. // var_dump(ContentTree::instance());exit;
  27. $canReorder = count($CurrentNode->children) > 1;
  28. $canDelete = !empty($CurrentNode->parent);
  29. $canTranslate = ($page instanceof ICatalyzTranslatable) && count(sfConfig::get('app_translations_available_languages')) > 1;
  30. if ($page instanceof ICatalyzToolbarActionManager) {
  31. $canAdd = $page->canAddChild($canAdd);
  32. $canEdit = $page->canEdit();
  33. $canReorder = $page->canReorderChildren($canReorder);
  34. $canDelete = $canDelete && $page->canDelete();
  35. }
  36. if ($page instanceof ICatalyzToolbarActionStringManager) {
  37. $addCaption = $page->getCatalyzToolbarAddCaption();
  38. $reorderCaption = $page->getCatalyzToolbarReorderCaption();
  39. } else {
  40. $addCaption = false;
  41. $reorderCaption = false;
  42. }
  43. if (false == $addCaption) {
  44. $addCaption = __('Add a page', null, 'catalyz');
  45. }
  46. if (false == $reorderCaption) {
  47. $reorderCaption = __('Reorder', null, 'catalyz');
  48. }
  49. $disabledButtons = sfConfig::get('app_catalyz_disabled_action', array());
  50. $actions = array();
  51. if ($canEdit && !in_array('edit', $disabledButtons)) {
  52. $actions[] = array(
  53. 'key' => 'edit',
  54. 'caption' => __('Edit', null, 'catalyz'),
  55. 'url' => sfContext::getInstance()->getController()->genUrl('@node-translation-edit?node_id=' . $page->getNodeId() . '&culture=' . $CurrentNode->getCulture())
  56. );
  57. }
  58. if ($canAdd && !in_array('add', $disabledButtons)) {
  59. $actions[] = array(
  60. 'key' => 'add',
  61. 'caption' => $addCaption,
  62. 'url' => sfContext::getInstance()->getController()->genUrl('@node-add?parent_id=' . $page->getNodeId())
  63. );
  64. }
  65. if ($canTranslate && !in_array('translations', $disabledButtons)) {
  66. $actions[] = array(
  67. 'key' => 'translation',
  68. 'caption' => __('Language versions', null, 'catalyz'),
  69. 'url' => sfContext::getInstance()->getController()->genUrl('@node-translations?node_id=' . $page->getNodeId())
  70. );
  71. }
  72. if ($canReorder && !in_array('reorder', $disabledButtons)) {
  73. $actions[] = array(
  74. 'key' => 'reorder',
  75. 'caption' => $reorderCaption,
  76. 'url' => sfContext::getInstance()->getController()->genUrl('@node-reorder?node_id=' . $page->getNodeId())
  77. );
  78. }
  79. if ($canDelete && !in_array('delete', $disabledButtons)) {
  80. $actions[] = array(
  81. 'key' => 'delete',
  82. 'caption' => __('Delete', null, 'catalyz'),
  83. 'url' => sfContext::getInstance()->getController()->genUrl('@node-delete?node_id=' . $page->getNodeId())
  84. );
  85. }
  86. if (!in_array('admin', $disabledButtons)) {
  87. $actions[] = array(
  88. 'key' => 'admin',
  89. 'caption' => __('Admin', null, 'catalyz'),
  90. 'url' => sfContext::getInstance()->getController()->genUrl('@admin?node_id=' . $page->getNodeId())
  91. );
  92. }
  93. // $actions[] = array(
  94. // 'key' => 'logout',
  95. // 'caption' => __('X'),
  96. // 'url' => '/catalyz/logout'
  97. // );
  98. return $actions;
  99. }
  100. protected $actions = array();
  101. protected $admin_actions = array();
  102. public function register($action, $is_admin = false)
  103. {
  104. if (!$is_admin) {
  105. $this->actions[] = $action;
  106. } else {
  107. $this->admin_actions[] = $action;
  108. }
  109. }
  110. public function getToolbarActions($page)
  111. {
  112. sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_toolbar_actions'));
  113. $actions = $this->getDefaultActions($page);
  114. $newActions = $this->actions;
  115. if ($page instanceof ICatalyzToolbarActionProvider) {
  116. $newActions = array_merge($newActions, $page->getToolbarActions());
  117. }
  118. if (count($newActions) > 0) {
  119. foreach($newActions as $newAction) {
  120. $tmp = array();
  121. $found = false;
  122. foreach($actions as $existingAction) {
  123. if (isset($newAction['before']) && $newAction['before'] == $existingAction['key']) {
  124. $tmp[] = $newAction;
  125. $found = true;
  126. }
  127. $tmp[] = $existingAction;
  128. if (isset($newAction['after']) && $newAction['after'] == $existingAction['key']) {
  129. $tmp[] = $newAction;
  130. $found = true;
  131. }
  132. }
  133. if (!$found) {
  134. if (!empty($newAction['after'])) {
  135. $tmp[] = $newAction;
  136. } else {
  137. throw new Exception(sprintf('Unable to find action "%s" to add "%s" action near it in Catalyz Toolbar.', $newAction['before'] . $newAction['after'], $newAction['caption']));
  138. }
  139. }
  140. $actions = $tmp;
  141. }
  142. }
  143. return $actions;
  144. }
  145. public function getAdminActions($page)
  146. {
  147. sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_admin_actions'));
  148. $return = $this->admin_actions;
  149. $disabled_actions = sfConfig::get('app_catalyz_disabled_action', array());
  150. foreach ($return as $k => $action) {
  151. if (in_array($action['key'], $disabled_actions)) {
  152. unset($return[$k]);
  153. }
  154. }
  155. return $return;
  156. }
  157. protected $enabled = true;
  158. public function disable()
  159. {
  160. $this->enabled = false;
  161. }
  162. public function enable()
  163. {
  164. $this->enabled = true;
  165. }
  166. public function enabled()
  167. {
  168. return $this->enabled;
  169. }
  170. }
  171. ?>