Merge pull request #18963 from samuelsov/nli18n
[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 */
21 class CRM_Core_Selector_Base {
22
23 /**
24 * The sort order which is computed from the columnHeaders
25 *
26 * @var array
27 */
28 protected $_order;
29
30 /**
31 * The permission mask for this selector
32 *
33 * @var string
34 */
35 protected $_permission = NULL;
36
37 /**
38 * The qfKey of the underlying search
39 *
40 * @var string
41 */
42 protected $_key;
43
44 /**
45 * This function gets the attribute for the action that.
46 * it matches.
47 *
48 * @param string $match the action to match against
49 * @param string $attribute the attribute to return ( name, link, title )
50 *
51 * @return string
52 * the attribute that matches the action if any
53 */
54 public function getActionAttribute($match, $attribute = 'name') {
55 $links = &$this->links();
56
57 foreach ($link as $action => $item) {
58 if ($match & $action) {
59 return $item[$attribute];
60 }
61 }
62 return NULL;
63 }
64
65 /**
66 * This is a static virtual function returning reference on links array. Each
67 * inherited class must redefine this function
68 *
69 * links is an array of associative arrays. Each element of the array
70 * has at least 3 fields
71 *
72 * name : the name of the link
73 * url : the URI to be used for this link
74 * qs : the parameters to the above url along with any dynamic substitutions
75 * title : A more descriptive name, typically used in breadcrumbs / navigation
76 */
77 public static function &links() {
78 return NULL;
79 }
80
81 /**
82 * Compose the template file name from the class name.
83 *
84 * @param string $action
85 * The action being performed.
86 *
87 * @return string
88 * template file name
89 */
90 public function getTemplateFileName($action = NULL) {
91 return (str_replace('_', DIRECTORY_SEPARATOR, CRM_Utils_System::getClassName($this)) . ".tpl");
92 }
93
94 /**
95 * Getter for the sorting direction for the fields which will be displayed on the form.
96 *
97 * @param string $action the action being performed
98 *
99 * @return array
100 * the elements that can be sorted along with their properties
101 */
102 public function &getSortOrder($action) {
103 $columnHeaders = &$this->getColumnHeaders(NULL);
104
105 if (!isset($this->_order)) {
106 $this->_order = [];
107 $start = 2;
108 $firstElementNotFound = TRUE;
109 if (!empty($columnHeaders)) {
110 foreach ($columnHeaders as $k => $header) {
111 $header = &$columnHeaders[$k];
112 if (array_key_exists('sort', $header)) {
113 if ($firstElementNotFound && $header['direction'] != CRM_Utils_Sort::DONTCARE) {
114 $this->_order[1] = &$header;
115 $firstElementNotFound = FALSE;
116 }
117 else {
118 $this->_order[$start++] = &$header;
119 }
120 }
121 unset($header);
122 }
123 }
124 }
125 return $this->_order;
126 }
127
128 /**
129 * Setter for permission.
130 *
131 * @var string
132 */
133 public function setPermission($permission) {
134 $this->_permission = $permission;
135 }
136
137 /**
138 * Get the display text in plain language for the search
139 * to display on the results page
140 *
141 * FIXME: the current internationalisation is bad, but should more or less work
142 * on most of "European" languages
143 *
144 * @return array
145 * array of strings
146 */
147 public function getQill() {
148 return NULL;
149 }
150
151 /**
152 * @return null
153 */
154 public function getSummary() {
155 return NULL;
156 }
157
158 /**
159 * @param $key
160 */
161 public function setKey($key) {
162 $this->_key = $key;
163 }
164
165 /**
166 * @return string
167 */
168 public function getKey() {
169 return $this->_key;
170 }
171
172 }