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