/sites/all/modules/contrib/civicrm/CRM/Utils/PagerAToZ.php

https://gitlab.com/virtualrealms/d7civicrm · PHP · 200 lines · 108 code · 15 blank · 77 comment · 12 complexity · 396406eef4341eb0d959a48bfe93639d MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  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. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2019
  30. */
  31. /**
  32. * This class is for displaying alphabetical bar
  33. */
  34. class CRM_Utils_PagerAToZ {
  35. /**
  36. * Returns the alphabetic array for sorting by character.
  37. *
  38. * @param array $query
  39. * The query object.
  40. * @param string $sortByCharacter
  41. * The character that we are potentially sorting on.
  42. *
  43. * @param bool $isDAO
  44. *
  45. * @return string
  46. * The html formatted string
  47. */
  48. public static function getAToZBar(&$query, $sortByCharacter, $isDAO = FALSE) {
  49. $AToZBar = self::createLinks($query, $sortByCharacter, $isDAO);
  50. return $AToZBar;
  51. }
  52. /**
  53. * Return the all the static characters.
  54. *
  55. * @return array
  56. * is an array of static characters
  57. */
  58. public static function getStaticCharacters() {
  59. $staticAlphabets = [
  60. 'A',
  61. 'B',
  62. 'C',
  63. 'D',
  64. 'E',
  65. 'F',
  66. 'G',
  67. 'H',
  68. 'I',
  69. 'J',
  70. 'K',
  71. 'L',
  72. 'M',
  73. 'N',
  74. 'O',
  75. 'P',
  76. 'Q',
  77. 'R',
  78. 'S',
  79. 'T',
  80. 'U',
  81. 'V',
  82. 'W',
  83. 'X',
  84. 'Y',
  85. 'Z',
  86. ];
  87. return $staticAlphabets;
  88. }
  89. /**
  90. * Return the all the dynamic characters.
  91. *
  92. * @param $query
  93. * @param $isDAO
  94. *
  95. * @return array
  96. * is an array of dynamic characters
  97. */
  98. public static function getDynamicCharacters(&$query, $isDAO) {
  99. if ($isDAO) {
  100. $result = $query;
  101. }
  102. else {
  103. $result = $query->alphabetQuery();
  104. }
  105. if (!$result) {
  106. return NULL;
  107. }
  108. $dynamicAlphabets = [];
  109. while ($result->fetch()) {
  110. $dynamicAlphabets[] = strtoupper($result->sort_name);
  111. }
  112. return $dynamicAlphabets;
  113. }
  114. /**
  115. * Create the links.
  116. *
  117. * @param array $query
  118. * The form values for search.
  119. * @param string $sortByCharacter
  120. * The character that we are potentially sorting on.
  121. *
  122. * @param $isDAO
  123. *
  124. * @return array
  125. * with links
  126. */
  127. public static function createLinks(&$query, $sortByCharacter, $isDAO) {
  128. $AToZBar = self::getStaticCharacters();
  129. $dynamicAlphabets = self::getDynamicCharacters($query, $isDAO);
  130. if (!$dynamicAlphabets) {
  131. return NULL;
  132. }
  133. $AToZBar = array_merge($AToZBar, $dynamicAlphabets);
  134. sort($AToZBar, SORT_STRING);
  135. $AToZBar = array_unique($AToZBar);
  136. // get the current path
  137. $path = CRM_Utils_System::currentPath();
  138. $qfKey = NULL;
  139. if (isset($query->_formValues)) {
  140. $qfKey = CRM_Utils_Array::value('qfKey', $query->_formValues);
  141. }
  142. if (empty($qfKey)) {
  143. // CRM-20943 Can only pass variables by reference and also cannot use $this so using $empty setting to NULL which is default.
  144. $emptyVariable = NULL;
  145. $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $emptyVariable, FALSE, NULL, $_REQUEST);
  146. }
  147. $aToZBar = [];
  148. foreach ($AToZBar as $key => $link) {
  149. if ($link === NULL) {
  150. continue;
  151. }
  152. $element = [];
  153. if (in_array($link, $dynamicAlphabets)) {
  154. $klass = '';
  155. if ($link == $sortByCharacter) {
  156. $element['class'] = "active";
  157. $klass = 'class="active"';
  158. }
  159. $url = CRM_Utils_System::url($path, "force=1&qfKey=$qfKey&sortByCharacter=");
  160. // we do it this way since we want the url to be encoded but not the link character
  161. // since that seems to mess up drupal utf-8 encoding etc
  162. $url .= urlencode($link);
  163. $element['item'] = sprintf('<a href="%s" %s>%s</a>',
  164. $url,
  165. $klass,
  166. $link
  167. );
  168. }
  169. else {
  170. $element['item'] = $link;
  171. }
  172. $aToZBar[] = $element;
  173. }
  174. $url = sprintf(
  175. '<a href="%s">%s</a>',
  176. CRM_Utils_System::url(
  177. $path,
  178. "force=1&qfKey=$qfKey&sortByCharacter=all"
  179. ),
  180. ts('All')
  181. );
  182. $aToZBar[] = ['item' => $url];
  183. return $aToZBar;
  184. }
  185. }