/libraries/koowa/controller/behavior/commandable.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 185 lines · 98 code · 22 blank · 65 comment · 16 complexity · 4b4e584e878d6d52b6cd635b0c06bd43 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: commandable.php 4266 2011-10-08 23:57:41Z johanjanssens $
  4. * @category Koowa
  5. * @package Koowa_Controller
  6. * @subpackage Command
  7. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. * @link http://www.nooku.org
  10. */
  11. /**
  12. * Commandable Controller Behavior Class
  13. *
  14. * @author Johan Janssens <johan@nooku.org>
  15. * @category Koowa
  16. * @package Koowa_Controller
  17. * @subpackage Behavior
  18. */
  19. class KControllerBehaviorCommandable extends KControllerBehaviorAbstract
  20. {
  21. /**
  22. * Toolbar object or identifier (com://APP/COMPONENT.model.NAME)
  23. *
  24. * @var string|object
  25. */
  26. protected $_toolbar;
  27. /**
  28. * Constructor
  29. *
  30. * @param object An optional KConfig object with configuration options.
  31. */
  32. public function __construct(KConfig $config)
  33. {
  34. parent::__construct($config);
  35. $this->_toolbar = $config->toolbar;
  36. }
  37. /**
  38. * Initializes the default configuration for the object
  39. *
  40. * Called from {@link __construct()} as a first step of object instantiation.
  41. *
  42. * @param object An optional KConfig object with configuration options.
  43. * @return void
  44. */
  45. protected function _initialize(KConfig $config)
  46. {
  47. $config->append(array(
  48. 'toolbar' => null,
  49. ));
  50. parent::_initialize($config);
  51. }
  52. /**
  53. * Get the view object attached to the controller
  54. *
  55. * @throws KControllerException if the view cannot be found.
  56. * @return KControllerToolbarAbstract
  57. */
  58. public function getToolbar()
  59. {
  60. if(!$this->_toolbar instanceof KControllerToolbarAbstract)
  61. {
  62. //Make sure we have a view identifier
  63. if(!($this->_toolbar instanceof KServiceIdentifier)) {
  64. $this->setToolbar($this->_toolbar);
  65. }
  66. $config = array(
  67. 'controller' => $this->getMixer()
  68. );
  69. $this->_toolbar = $this->getService($this->_toolbar, $config);
  70. }
  71. return $this->_toolbar;
  72. }
  73. /**
  74. * Method to set a toolbar object attached to the controller
  75. *
  76. * @param mixed An object that implements KObjectServiceable, KServiceIdentifier object
  77. * or valid identifier string
  78. * @throws KControllerBehaviorException If the identifier is not a view identifier
  79. * @return KControllerToolbarAbstract
  80. */
  81. public function setToolbar($toolbar)
  82. {
  83. if(!($toolbar instanceof KControllerToolbarAbstract))
  84. {
  85. if(is_string($toolbar) && strpos($toolbar, '.') === false )
  86. {
  87. $identifier = clone $this->getIdentifier();
  88. $identifier->path = array('controller', 'toolbar');
  89. $identifier->name = $toolbar;
  90. }
  91. else $identifier = $this->getIdentifier($toolbar);
  92. if($identifier->path[1] != 'toolbar') {
  93. throw new KControllerBehaviorException('Identifier: '.$identifier.' is not a toolbar identifier');
  94. }
  95. $toolbar = $identifier;
  96. }
  97. $this->_toolbar = $toolbar;
  98. return $this;
  99. }
  100. /**
  101. * Add default toolbar commands
  102. * .
  103. * @param KCommandContext A command context object
  104. */
  105. protected function _beforeGet(KCommandContext $context)
  106. {
  107. if(!$this->_toolbar) {
  108. $this->setToolbar($this->getView()->getName());
  109. }
  110. }
  111. /**
  112. * Add default toolbar commands and set the toolbar title
  113. * .
  114. * @param KCommandContext A command context object
  115. */
  116. protected function _afterRead(KCommandContext $context)
  117. {
  118. if($this->_toolbar)
  119. {
  120. $name = ucfirst($context->caller->getIdentifier()->name);
  121. if($this->getModel()->getState()->isUnique())
  122. {
  123. $saveable = $this->canEdit();
  124. $title = 'Edit '.$name;
  125. }
  126. else
  127. {
  128. $saveable = $this->canAdd();
  129. $title = 'New '.$name;
  130. }
  131. if($saveable)
  132. {
  133. $this->getToolbar()
  134. ->setTitle($title)
  135. ->addCommand('save')
  136. ->addCommand('apply');
  137. }
  138. $this->getToolbar()->addCommand('cancel', array('attribs' => array('data-novalidate' => 'novalidate')));
  139. }
  140. }
  141. /**
  142. * Add default toolbar commands
  143. * .
  144. * @param KCommandContext A command context object
  145. */
  146. protected function _afterBrowse(KCommandContext $context)
  147. {
  148. if($this->_toolbar)
  149. {
  150. if($this->canAdd())
  151. {
  152. $identifier = $context->caller->getIdentifier();
  153. $config = array('attribs' => array(
  154. 'href' => JRoute::_( 'index.php?option=com_'.$identifier->package.'&view='.$identifier->name)
  155. ));
  156. $this->getToolbar()->addCommand('new', $config);
  157. }
  158. if($this->canDelete()) {
  159. $this->getToolbar()->addCommand('delete');
  160. }
  161. }
  162. }
  163. }