19609636abfe7403d363d2b98007276263d3491c
[civicrm-core.git] / CRM / Core / Selector / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * A simple base class for objects that need to implement the selector api
31 * interface. This class provides common functionality with regard to actions
32 * and display names
33 *
34 * @package CRM
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * $Id$
37 *
38 */
39 class CRM_Core_Selector_Base {
40
41 /**
42 * The sort order which is computed from the columnHeaders
43 *
44 * @var array
45 */
46 protected $_order;
47
48 /**
49 * The permission mask for this selector
50 *
51 * @var string
52 */
53 protected $_permission = NULL;
54
55 /**
56 * The qfKey of the underlying search
57 *
58 * @var string
59 */
60 protected $_key;
61
62 /**
63 * This function gets the attribute for the action that
64 * it matches.
65 *
66 * @param string match the action to match against
67 * @param string attribute the attribute to return ( name, link, title )
68 *
69 * @return string the attribute that matches the action if any
70 *
71 *
72 */
73 public function getActionAttribute($match, $attribute = 'name') {
74 $links = &$this->links();
75
76 foreach ($link as $action => $item) {
77 if ($match & $action) {
78 return $item[$attribute];
79 }
80 }
81 return NULL;
82 }
83
84 /**
85 * This is a static virtual function returning reference on links array. Each
86 * inherited class must redefine this function
87 *
88 * links is an array of associative arrays. Each element of the array
89 * has at least 3 fields
90 *
91 * name : the name of the link
92 * url : the URI to be used for this link
93 * qs : the parameters to the above url along with any dynamic substitutions
94 * title : A more descriptive name, typically used in breadcrumbs / navigation
95 */
96 public static function &links() {
97 return NULL;
98 }
99
100 /**
101 * Compose the template file name from the class name
102 *
103 * @param string $action the action being performed
104 *
105 * @return string template file name
106 */
107 public function getTemplateFileName($action = NULL) {
108 return (str_replace('_', DIRECTORY_SEPARATOR, CRM_Utils_System::getClassName($this)) . ".tpl");
109 }
110
111 /**
112 * Getter for the sorting direction for the fields which will be displayed on the form.
113 *
114 * @param string action the action being performed
115 *
116 * @return array the elements that can be sorted along with their properties
117 */
118 public function &getSortOrder($action) {
119 $columnHeaders = &$this->getColumnHeaders(NULL);
120
121 if (!isset($this->_order)) {
122 $this->_order = array();
123 $start = 2;
124 $firstElementNotFound = TRUE;
125 if (!empty($columnHeaders)) {
126 foreach ($columnHeaders as $k => $header) {
127 $header = &$columnHeaders[$k];
128 if (array_key_exists('sort', $header)) {
129 if ($firstElementNotFound && $header['direction'] != CRM_Utils_Sort::DONTCARE) {
130 $this->_order[1] = &$header;
131 $firstElementNotFound = FALSE;
132 }
133 else {
134 $this->_order[$start++] = &$header;
135 }
136 }
137 unset($header);
138 }
139 }
140 if ($firstElementNotFound) {
141 // CRM_Core_Error::fatal( "Could not find a valid sort directional element" );
142 }
143 }
144 return $this->_order;
145 }
146
147 /**
148 * Setter for permission
149 *
150 * @var string
151 */
152 public function setPermission($permission) {
153 $this->_permission = $permission;
154 }
155
156 /**
157 * Get the display text in plain language for the search
158 * to display on the results page
159 *
160 * @return string
161 */
162 public function getQill() {
163 return NULL;
164 }
165
166 /**
167 * @return null
168 */
169 public function getSummary() {
170 return NULL;
171 }
172
173 /**
174 * @param $key
175 */
176 public function setKey($key) {
177 $this->_key = $key;
178 }
179
180 /**
181 * @return string
182 */
183 public function getKey() {
184 return $this->_key;
185 }
186 }