PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Utils/Sort.php

https://github.com/michaelmcandrew/ste
PHP | 293 lines | 100 code | 41 blank | 152 comment | 23 complexity | a88b1d6071cb77f9e45f81b39b4803fe MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. /**
  35. *
  36. * Base class to provide generic sort functionality. Note that some ideas
  37. * have been borrowed from the drupal tablesort.inc code. Also note that
  38. * since the Pager and Sort class are similar, do match the function names
  39. * if introducing additional functionality
  40. *
  41. * @package CRM
  42. * @copyright CiviCRM LLC (c) 2004-2011
  43. * $Id$
  44. *
  45. */
  46. class CRM_Utils_Sort {
  47. /**
  48. * constants to determine what direction each variable
  49. * is to be sorted
  50. *
  51. * @var int
  52. */
  53. const
  54. ASCENDING = 1,
  55. DESCENDING = 2,
  56. DONTCARE = 4,
  57. /**
  58. * the name for the sort GET/POST param
  59. *
  60. * @var string
  61. */
  62. SORT_ID = 'crmSID',
  63. SORT_DIRECTION = 'crmSortDirection',
  64. SORT_ORDER = 'crmSortOrder';
  65. /**
  66. * name of the sort function. Used to isolate session variables
  67. * @var string
  68. */
  69. protected $_name;
  70. /**
  71. * array of variables that influence the query
  72. *
  73. * @var array
  74. */
  75. public $_vars;
  76. /**
  77. * the newly formulated base url to be used as links
  78. * for various table elements
  79. *
  80. * @var string
  81. */
  82. protected $_link;
  83. /**
  84. * what's the name of the sort variable in a REQUEST
  85. *
  86. * @var string
  87. */
  88. protected $_urlVar;
  89. /**
  90. * What variable are we currently sorting on
  91. *
  92. * @var string
  93. */
  94. protected $_currentSortID;
  95. /**
  96. * What direction are we sorting on
  97. *
  98. * @var string
  99. */
  100. protected $_currentSortDirection;
  101. /**
  102. * The output generated for the current form
  103. *
  104. * @var array
  105. */
  106. public $_response;
  107. /**
  108. * The constructor takes an assoc array
  109. * key names of variable (which should be the same as the column name)
  110. * value: ascending or descending
  111. *
  112. * @param mixed $vars - assoc array as described above
  113. * @param string $defaultSortOrder - order to sort
  114. *
  115. * @return void
  116. * @access public
  117. */
  118. function __construct( &$vars, $defaultSortOrder = null ) {
  119. $this->_vars = array( );
  120. $this->_response = array();
  121. foreach ( $vars as $weight => $value ) {
  122. $this->_vars[$weight] = array(
  123. 'name' => $value['sort'],
  124. 'direction' => CRM_Utils_Array::value( 'direction', $value ),
  125. 'title' => $value['name'],
  126. );
  127. }
  128. $this->_currentSortID = 1;
  129. if ( isset( $this->_vars[$this->_currentSortID] ) ) {
  130. $this->_currentSortDirection = $this->_vars[$this->_currentSortID]['direction'];
  131. }
  132. $this->_urlVar = self::SORT_ID;
  133. $this->_link = CRM_Utils_System::makeURL( $this->_urlVar );
  134. $this->initialize( $defaultSortOrder );
  135. }
  136. /**
  137. * Function returns the string for the order by clause
  138. *
  139. * @return string the order by clause
  140. * @access public
  141. */
  142. function orderBy( ) {
  143. if ( ! CRM_Utils_Array::value( $this->_currentSortID, $this->_vars ) ) {
  144. return '';
  145. }
  146. if ( $this->_vars[$this->_currentSortID]['direction'] == self::ASCENDING ||
  147. $this->_vars[$this->_currentSortID]['direction'] == self::DONTCARE ) {
  148. $this->_vars[$this->_currentSortID]['name'] = str_replace( ' ', '_', $this->_vars[$this->_currentSortID]['name'] );
  149. return $this->_vars[$this->_currentSortID]['name'] . ' asc';
  150. } else {
  151. $this->_vars[$this->_currentSortID]['name'] = str_replace( ' ', '_', $this->_vars[$this->_currentSortID]['name'] );
  152. return $this->_vars[$this->_currentSortID]['name'] . ' desc';
  153. }
  154. }
  155. /**
  156. * create the sortID string to be used in the GET param
  157. *
  158. * @param int $index the field index
  159. * @param int $dir the direction of the sort
  160. *
  161. * @return string the string to append to the url
  162. * @static
  163. * @access public
  164. */
  165. static function sortIDValue( $index, $dir ) {
  166. return ( $dir == self::DESCENDING ) ? $index . '_d' : $index . '_u';
  167. }
  168. /**
  169. * init the sort ID values in the object
  170. *
  171. * @param string $defaultSortOrder the sort order to use by default
  172. *
  173. * @return returns null if $url- (sort url) is not found
  174. * @access public
  175. */
  176. function initSortID( $defaultSortOrder ) {
  177. $url = CRM_Utils_Array::value( self::SORT_ID, $_GET, $defaultSortOrder );
  178. if ( empty( $url ) ) {
  179. return;
  180. }
  181. list( $current, $direction ) = explode( '_', $url );
  182. // if current is wierd and does not exist in the vars array, skip
  183. if ( ! array_key_exists( $current, $this->_vars ) ) {
  184. return;
  185. }
  186. if ( $direction == 'u' ) {
  187. $direction = self::ASCENDING;
  188. } else if ( $direction == 'd' ) {
  189. $direction = self::DESCENDING;
  190. } else {
  191. $direction = self::DONTCARE;
  192. }
  193. $this->_currentSortID = $current;
  194. $this->_currentSortDirection = $direction;
  195. $this->_vars[$current]['direction'] = $direction;
  196. }
  197. /**
  198. * init the object
  199. *
  200. * @param string $defaultSortOrder the sort order to use by default
  201. *
  202. * @return void
  203. * @access public
  204. */
  205. function initialize( $defaultSortOrder ) {
  206. $this->initSortID( $defaultSortOrder );
  207. $this->_response = array( );
  208. $current = $this->_currentSortID;
  209. foreach ( $this->_vars as $index => $item ) {
  210. $name = $item['name'];
  211. $this->_response[$name] = array();
  212. $newDirection = ( $item['direction'] == self::ASCENDING ) ? self::DESCENDING : self::ASCENDING;
  213. if ( $current == $index ) {
  214. if ( $item['direction'] == self::ASCENDING ) {
  215. $class = 'sorting_asc';
  216. } else {
  217. $class = 'sorting_desc';
  218. }
  219. } else {
  220. $class = 'sorting';
  221. }
  222. $this->_response[$name]['link'] = '<a href="' . $this->_link . $this->sortIDValue( $index, $newDirection ) . '" class="' . $class . '">' . $item['title'] . '</a>';
  223. }
  224. }
  225. /**
  226. * getter for currentSortID
  227. *
  228. * @return int returns of the current sort id
  229. * @acccess public
  230. */
  231. function getCurrentSortID( ) {
  232. return $this->_currentSortID;
  233. }
  234. /**
  235. * getter for currentSortDirection
  236. *
  237. * @return int returns of the current sort direction
  238. * @acccess public
  239. */
  240. function getCurrentSortDirection( ) {
  241. return $this->_currentSortDirection;
  242. }
  243. /**
  244. * Universal callback function for sorting by weight
  245. *
  246. * @return array of items sorted by weight
  247. * @access public
  248. */
  249. static function cmpFunc( $a, $b ) {
  250. return ( $a['weight'] <= $b['weight'] ) ? -1 : 1;
  251. }
  252. }