PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pkp/classes/controllers/listbuilder/ListbuilderHandler.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 263 lines | 104 code | 43 blank | 116 comment | 3 complexity | 479e61a459d28ef974800424eeb3140d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/controllers/listbuilder/ListbuilderHandler.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class Listbuilder
  9. * @ingroup controllers_listbuilder
  10. *
  11. * @brief Class defining basic operations for handling Listbuilder UI elements
  12. */
  13. import('controllers.grid.GridHandler');
  14. import('controllers.listbuilder.ListbuilderGridRow');
  15. define('LISTBUILDER_SOURCE_TYPE_TEXT', 0);
  16. define('LISTBUILDER_SOURCE_TYPE_SELECT', 1);
  17. define('LISTBUILDER_SOURCE_TYPE_BOUND', 2);
  18. // FIXME: Rather than inheriting from grid handler, common base
  19. // functionality might better be factored into a common base handler
  20. // class and then both, GridHandler and ListbuilderHandler should
  21. // inherit from the common base class. The shared concept of grids
  22. // and list builders is that both seem to work with element lists. Maybe
  23. // ElementListHandler would be a good name then for a common base
  24. // class? I'm not a 100% sure about this but it'll become obvious
  25. // once you try. If there's considerable amounts of code in both
  26. // the base class and the re-factored grid handler then you know
  27. // you're on the right track.
  28. class ListbuilderHandler extends GridHandler {
  29. /** @var string The label associated with the primary source to be added to the list **/
  30. var $_sourceTitle;
  31. /** @var integer Definition of the type of source **/
  32. var $_sourceType;
  33. /** @var array The current collection of items in the list **/
  34. var $_items;
  35. /** @var string The title of the item collection **/
  36. var $_listTitle;
  37. /** @var array Array of optional attributes **/
  38. var $_attributeNames;
  39. /** @var array Array of strings containing possible items that are stored in the source list */
  40. var $_possibleItems;
  41. /**
  42. * Constructor.
  43. */
  44. function ListbuilderHandler() {
  45. parent::GridHandler();
  46. }
  47. function getRemoteOperations() {
  48. return array('fetch', 'addItem', 'deleteItems');
  49. }
  50. /**
  51. * Get the listbuilder template
  52. * @return string
  53. */
  54. function getTemplate() {
  55. if (is_null($this->_template)) {
  56. $this->setTemplate('controllers/listbuilder/listbuilder.tpl');
  57. }
  58. return $this->_template;
  59. }
  60. /**
  61. * Set the title for the source (left side of the listbuilder)
  62. * FIXME: AFAIK doxygen needs the $ to correctly parse variable names
  63. * I've corrected this throughout the code but leave this as a marker
  64. * for you.
  65. * @param $sourceTitle string
  66. */
  67. function setSourceTitle($sourceTitle) {
  68. $this->_sourceTitle = $sourceTitle;
  69. }
  70. /**
  71. * Get the title for the source (left side of the listbuilder)
  72. * @return string
  73. */
  74. function getSourceTitle() {
  75. return $this->_sourceTitle;
  76. }
  77. /**
  78. * Set the type of source (Free text input, select from list, autocomplete)
  79. * @param $sourceType int
  80. */
  81. function setSourceType($sourceType) {
  82. $this->_sourceType = $sourceType;
  83. }
  84. /**
  85. * Get the type of source (Free text input, select from list, autocomplete)
  86. * @return int
  87. */
  88. function getSourceType() {
  89. return $this->_sourceType;
  90. }
  91. /**
  92. * Set the ListbuilderItem associated with this class
  93. * @param $items array
  94. */
  95. function setItems($items) {
  96. $this->_items = $items;
  97. }
  98. /**
  99. * Return all ListbuilderItems
  100. * @return array
  101. */
  102. function getItems() {
  103. return $this->_items;
  104. }
  105. /**
  106. * Return a ListbuilderItem by ID
  107. * @return ListbuilderItem
  108. */
  109. function getItem($itemId) {
  110. return $this->_items[$itemId];
  111. }
  112. /**
  113. * Remove a ListbuilderItem by ID
  114. * @param $itemId mixed
  115. */
  116. function removeItem($itemId) {
  117. unset($this->_items[$itemId]);
  118. }
  119. /**
  120. * Set the localized label for the list (right side of the listbuilder)
  121. * @param $listTitle string
  122. */
  123. function setListTitle($listTitle) {
  124. $this->_listTitle = $listTitle;
  125. }
  126. /**
  127. * Get the localized label for the list (right side of the listbuilder)
  128. * @return string
  129. */
  130. function getListTitle() {
  131. return $this->_listTitle;
  132. }
  133. /**
  134. * Set the localized labels for each attribute
  135. * @param $attributeNames array
  136. */
  137. function setAttributeNames($attributeNames) {
  138. $this->_attributeNames = $attributeNames;
  139. }
  140. /**
  141. * Get the localized labels for each attribute
  142. * @return array
  143. */
  144. function getAttributeNames() {
  145. return $this->_attributeNames;
  146. }
  147. /**
  148. * Build a list of <option>'s based on the input (can be array or one list item)
  149. * @param $itemName string
  150. * @param $attributeNames string
  151. */
  152. function _buildListItemHTML($itemId = null, $itemName = null, $attributeNames = null) {
  153. $templateMgr =& TemplateManager::getManager();
  154. $templateMgr->assign('itemId', $itemId);
  155. $templateMgr->assign('itemName', $itemName);
  156. if (isset($attributeNames)) {
  157. if (is_array($attributeNames)) $attributeNames = implode(', ', $attributeNames);
  158. $templateMgr->assign('attributeNames', $attributeNames);
  159. }
  160. return $templateMgr->fetch('controllers/listbuilder/listbuilderItem.tpl');
  161. }
  162. /**
  163. * Display the Listbuilder
  164. */
  165. function fetch(&$args, &$request) {
  166. // FIXME: User validation
  167. $templateMgr =& TemplateManager::getManager();
  168. $this->setupTemplate();
  169. $router =& $request->getRouter();
  170. $templateMgr->assign('addUrl', $router->url($request, array(), null, 'addItem'));
  171. $templateMgr->assign('deleteUrl', $router->url($request, array(), null, 'deleteItems'));
  172. // Translate modal submit/cancel buttons
  173. $okButton = __('common.ok');
  174. $warning = __('common.warning');
  175. $templateMgr->assign('localizedButtons', "$okButton, $warning");
  176. // initialize to create the columns
  177. $columns =& $this->getColumns();
  178. $templateMgr->assign_by_ref('columns', $columns);
  179. $templateMgr->assign('numColumns', count($columns));
  180. // Render the rows
  181. $rows = $this->_renderRowsInternally($request);
  182. $templateMgr->assign_by_ref('rows', $rows);
  183. $templateMgr->assign('listbuilder', $this);
  184. echo $templateMgr->fetch($this->getTemplate());
  185. }
  186. //
  187. // Overridden methods from GridHandler
  188. //
  189. /**
  190. * @see GridHandler::getRowInstance()
  191. * @return CitationGridRow
  192. */
  193. function &getRowInstance() {
  194. // Return a citation row
  195. $row = new ListbuilderGridRow();
  196. return $row;
  197. }
  198. /**
  199. * Handle adding an item to the list
  200. * NB: sub-classes must implement this method.
  201. */
  202. function addItem(&$args, &$request) {
  203. assert(false);
  204. }
  205. /**
  206. * Handle deleting items from the list
  207. * NB: sub-classes must implement this method.
  208. */
  209. function deleteItems(&$args, &$request) {
  210. assert(false);
  211. }
  212. /**
  213. * @see PKPHandler::setupTemplate()
  214. */
  215. function setupTemplate() {
  216. parent::setupTemplate();
  217. AppLocale::requireComponents(array(LOCALE_COMPONENT_APPLICATION_COMMON, LOCALE_COMPONENT_OMP_MANAGER, LOCALE_COMPONENT_PKP_MANAGER));
  218. }
  219. }
  220. ?>