PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pkp/classes/controllers/grid/CategoryGridHandler.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 252 lines | 101 code | 38 blank | 113 comment | 9 complexity | 9a40907750f29ce4bb8173bb622d75a6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/controllers/grid/CategoryGridHandler.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 GridHandler
  9. * @ingroup controllers_grid
  10. *
  11. * @brief Class defining basic operations for handling HTML grids.
  12. */
  13. // import grid classes
  14. import('controllers.grid.GridHandler');
  15. import('controllers.grid.GridCategoryRow');
  16. // empty category constant
  17. define('GRID_CATEGORY_NONE', 'NONE');
  18. class CategoryGridHandler extends GridHandler {
  19. /**
  20. * Constructor.
  21. */
  22. function CategoryGridHandler() {
  23. parent::GridHandler();
  24. }
  25. //
  26. // Getters/Setters
  27. //
  28. //
  29. // Overridden methods from PKPHandler
  30. //
  31. /**
  32. * @see PKPHandler::getRemoteOperations()
  33. */
  34. function getRemoteOperations() {
  35. return array_merge(parent::getRemoteOperations(), array('fetchCategory'));
  36. }
  37. /**
  38. * @see PKPHandler::initialize()
  39. * @param $request PKPRequest
  40. */
  41. function initialize(&$request) {
  42. parent::initialize($request);
  43. }
  44. //
  45. // Public handler methods
  46. //
  47. /**
  48. * Render the entire grid controller and send
  49. * it to the client.
  50. * @return string the grid HTML
  51. */
  52. function fetchGrid($args, &$request) {
  53. // Prepare the template to render the grid
  54. $templateMgr =& TemplateManager::getManager();
  55. $templateMgr->assign_by_ref('grid', $this);
  56. // Add columns to the view
  57. $columns =& $this->getColumns();
  58. $templateMgr->assign_by_ref('columns', $columns);
  59. $templateMgr->assign('numColumns', count($columns));
  60. // Render the body elements (category groupings + rows inside a <tbody>)
  61. $gridBodyParts = $this->_renderCategoriesInternally($request);
  62. $templateMgr->assign_by_ref('gridBodyParts', $gridBodyParts);
  63. // Let the view render the grid
  64. return $templateMgr->fetch($this->getTemplate());
  65. }
  66. /**
  67. * Render a row and send it to the client.
  68. * @return string the row HTML
  69. */
  70. function fetchRow(&$args, &$request) {
  71. // Instantiate the requested row
  72. $row =& $this->getRequestedRow($request, $args);
  73. // Render the requested row
  74. return $this->_renderRowInternally($request, $row);
  75. }
  76. /**
  77. * Render a cell and send it to the client
  78. * @return string the row HTML
  79. */
  80. function fetchCell(&$args, &$request) {
  81. // Check the requested column
  82. if(!isset($args['columnId'])) fatalError('Missing column id!');
  83. if(!$this->hasColumn($args['columnId'])) fatalError('Invalid column id!');
  84. $column =& $this->getColumn($args['columnId']);
  85. // Instantiate the requested row
  86. $row =& $this->getRequestedRow($request, $args);
  87. // Render the cell
  88. return $this->_renderCellInternally($request, $row, $column);
  89. }
  90. //
  91. // Protected methods to be overridden/used by subclasses
  92. //
  93. /**
  94. * Get a new instance of a category grid row. May be
  95. * overridden by subclasses if they want to
  96. * provide a custom row definition.
  97. * @return CategoryGridRow
  98. */
  99. function &getCategoryRowInstance() {
  100. //provide a sensible default category row definition
  101. $row = new GridCategoryRow();
  102. return $row;
  103. }
  104. /**
  105. * Tries to identify the data element in the grids
  106. * data source that corresponds to the requested row id.
  107. * Raises a fatal error if such an element cannot be
  108. * found.
  109. * @param $request PKPRequest
  110. * @param $args array
  111. * @return GridRow the requested grid row, already
  112. * configured with id and data.
  113. */
  114. function &getRequestedRow($request, $args) {
  115. // Instantiate a new row
  116. $row =& $this->getRowInstance();
  117. $row->setGridId($this->getId());
  118. // Try to retrieve a row id from $args if it is present
  119. if(!isset($args['rowId'])) fatalError('Missing row id!');
  120. $rowId = $args['rowId'];
  121. $row->setId($rowId);
  122. // Retrieve row data for the requested row id
  123. $dataElement = $this->getRowDataElement($rowId);
  124. if (is_null($dataElement)) fatalError('Invalid row id!');
  125. $row->setData($dataElement);
  126. // Initialize the row
  127. $row->initialize($request);
  128. return $row;
  129. }
  130. /**
  131. * Retrieve a single data element from the grid's data
  132. * source corresponding to the given row id. If none is
  133. * found then return null.
  134. * @param $rowId
  135. * @return mixed
  136. */
  137. function &getRowDataElement($rowId) {
  138. $elementIterator =& $this->getData();
  139. if (is_a($elementIterator, 'DAOResultFactory')) {
  140. $dataArray =& $elementIterator->toAssociativeArray('id');
  141. } else {
  142. $dataArray =& $elementIterator->toArray();
  143. }
  144. if (!isset($dataArray[$rowId])) {
  145. $nullVar = null;
  146. return $nullVar;
  147. } else {
  148. return $dataArray[$rowId];
  149. }
  150. }
  151. //
  152. // Private helper methods
  153. //
  154. /**
  155. * Render all the categories internally
  156. * @param $request
  157. */
  158. function _renderCategoriesInternally(&$request) {
  159. // Iterate through the rows and render them according
  160. // to the row definition.
  161. $elementIterator =& $this->_getSortedElements();
  162. $renderedCategories = array();
  163. $iterator = 1;
  164. while (!$elementIterator->eof()) {
  165. // Instantiate a new row
  166. $categoryRow =& $this->getCategoryRowInstance();
  167. $categoryRow->setGridId($this->getId());
  168. // Use the element key as the row id
  169. list($key, $element) = $elementIterator->nextWithKey();
  170. $categoryRow->setId($key);
  171. $categoryRow->setData($element);
  172. // Initialize the row before we render it
  173. $categoryRow->initialize($request);
  174. // Render the row
  175. $renderedCategories[] = $this->_renderCategoryInternally($request, $categoryRow, $iterator);
  176. unset($element);
  177. $iterator = $iterator < 5 ? $iterator+1 : $iterator = 1;
  178. }
  179. return $renderedCategories;
  180. }
  181. /**
  182. * Optionally render a category row and render its data. If no category data given, render the rows only
  183. * @param PKPRequest $request
  184. * @param GridCategoryRow $categoryRow
  185. * @return String HTML for all the rows (including category)
  186. */
  187. function _renderCategoryInternally(&$request, &$categoryRow, $iterator = null) {
  188. $templateMgr =& TemplateManager::getManager();
  189. $categoryDataElement =& $categoryRow->getData();
  190. $rowData =& $this->getCategoryData($categoryDataElement);
  191. // Render the data rows
  192. $renderedRows = $this->_renderRowsInternally($request, $rowData);
  193. $templateMgr->assign_by_ref('rows', $renderedRows);
  194. $columns =& $this->getColumns();
  195. $templateMgr->assign('numColumns', count($columns));
  196. $templateMgr->assign('iterator', $iterator);
  197. $templateMgr->assign_by_ref('categoryRow', $categoryRow);
  198. $renderedCategoryRow = $templateMgr->fetch($categoryRow->getTemplate());
  199. $templateMgr->assign_by_ref('renderedCategoryRow', $renderedCategoryRow);
  200. return $templateMgr->fetch('controllers/grid/gridBodyPartWithCategory.tpl');
  201. }
  202. /**
  203. * Given a category name and a data element, return an id that identifies this category
  204. * To be used for sorting data elements into category buckets
  205. * @param Data Object $element
  206. * @param String $category
  207. * return mixed int/string
  208. */
  209. function getCategoryIdFromElement(&$element, $category) {
  210. // Should be overriden by subclasses
  211. return GRID_CATEGORY_NONE;
  212. }
  213. }
  214. ?>