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