123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- class CatalyzToolbar {
- protected static $instance;
- protected function __construct()
- {
- }
- /**
- *
- * @return CatalyzToolbar
- */
- static function instance()
- {
- if (empty(self::$instance)) {
- self::$instance = new CatalyzToolbar();
- }
- return self::$instance;
- }
- protected function getDefaultActions($page)
- {
- $canAdd = count($page->getValidSubClasses()) > 0;
- $canEdit = true;
- $ContentTree = ContentTree::instance(Catalyz::getPageCulture());
- $ContentTree->build();
- $ContentTree->initializeCurrentNodeById($page->getNodeId());
- $CurrentNode = $ContentTree->getCurrentNode();
- // var_dump(ContentTree::instance());exit;
- $canReorder = count($CurrentNode->children) > 1;
- $canDelete = !empty($CurrentNode->parent);
- $canTranslate = ($page instanceof ICatalyzTranslatable) && count(sfConfig::get('app_translations_available_languages')) > 1;
- if ($page instanceof ICatalyzToolbarActionManager) {
- $canAdd = $page->canAddChild($canAdd);
- $canEdit = $page->canEdit();
- $canReorder = $page->canReorderChildren($canReorder);
- $canDelete = $canDelete && $page->canDelete();
- }
- if ($page instanceof ICatalyzToolbarActionStringManager) {
- $addCaption = $page->getCatalyzToolbarAddCaption();
- $reorderCaption = $page->getCatalyzToolbarReorderCaption();
- } else {
- $addCaption = false;
- $reorderCaption = false;
- }
- if (false == $addCaption) {
- $addCaption = __('Add a page', null, 'catalyz');
- }
- if (false == $reorderCaption) {
- $reorderCaption = __('Reorder', null, 'catalyz');
- }
- $disabledButtons = sfConfig::get('app_catalyz_disabled_action', array());
- $actions = array();
- if ($canEdit && !in_array('edit', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'edit',
- 'caption' => __('Edit', null, 'catalyz'),
- 'url' => sfContext::getInstance()->getController()->genUrl('@node-translation-edit?node_id=' . $page->getNodeId() . '&culture=' . $CurrentNode->getCulture())
- );
- }
- if ($canAdd && !in_array('add', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'add',
- 'caption' => $addCaption,
- 'url' => sfContext::getInstance()->getController()->genUrl('@node-add?parent_id=' . $page->getNodeId())
- );
- }
- if ($canTranslate && !in_array('translations', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'translation',
- 'caption' => __('Language versions', null, 'catalyz'),
- 'url' => sfContext::getInstance()->getController()->genUrl('@node-translations?node_id=' . $page->getNodeId())
- );
- }
- if ($canReorder && !in_array('reorder', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'reorder',
- 'caption' => $reorderCaption,
- 'url' => sfContext::getInstance()->getController()->genUrl('@node-reorder?node_id=' . $page->getNodeId())
- );
- }
- if ($canDelete && !in_array('delete', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'delete',
- 'caption' => __('Delete', null, 'catalyz'),
- 'url' => sfContext::getInstance()->getController()->genUrl('@node-delete?node_id=' . $page->getNodeId())
- );
- }
- if (!in_array('admin', $disabledButtons)) {
- $actions[] = array(
- 'key' => 'admin',
- 'caption' => __('Admin', null, 'catalyz'),
- 'url' => sfContext::getInstance()->getController()->genUrl('@admin?node_id=' . $page->getNodeId())
- );
- }
- // $actions[] = array(
- // 'key' => 'logout',
- // 'caption' => __('X'),
- // 'url' => '/catalyz/logout'
- // );
- return $actions;
- }
- protected $actions = array();
- protected $admin_actions = array();
- public function register($action, $is_admin = false)
- {
- if (!$is_admin) {
- $this->actions[] = $action;
- } else {
- $this->admin_actions[] = $action;
- }
- }
- public function getToolbarActions($page)
- {
- sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_toolbar_actions'));
- $actions = $this->getDefaultActions($page);
- $newActions = $this->actions;
- if ($page instanceof ICatalyzToolbarActionProvider) {
- $newActions = array_merge($newActions, $page->getToolbarActions());
- }
- if (count($newActions) > 0) {
- foreach($newActions as $newAction) {
- $tmp = array();
- $found = false;
- foreach($actions as $existingAction) {
- if (isset($newAction['before']) && $newAction['before'] == $existingAction['key']) {
- $tmp[] = $newAction;
- $found = true;
- }
- $tmp[] = $existingAction;
- if (isset($newAction['after']) && $newAction['after'] == $existingAction['key']) {
- $tmp[] = $newAction;
- $found = true;
- }
- }
- if (!$found) {
- if (!empty($newAction['after'])) {
- $tmp[] = $newAction;
- } else {
- throw new Exception(sprintf('Unable to find action "%s" to add "%s" action near it in Catalyz Toolbar.', $newAction['before'] . $newAction['after'], $newAction['caption']));
- }
- }
- $actions = $tmp;
- }
- }
- return $actions;
- }
- public function getAdminActions($page)
- {
- sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_admin_actions'));
- $return = $this->admin_actions;
- $disabled_actions = sfConfig::get('app_catalyz_disabled_action', array());
- foreach ($return as $k => $action) {
- if (in_array($action['key'], $disabled_actions)) {
- unset($return[$k]);
- }
- }
- return $return;
- }
- protected $enabled = true;
- public function disable()
- {
- $this->enabled = false;
- }
- public function enable()
- {
- $this->enabled = true;
- }
- public function enabled()
- {
- return $this->enabled;
- }
- }
- ?>
|