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