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