Merge pull request #13197 from seamuslee001/lab_core_442
[civicrm-core.git] / CRM / Core / ShowHideBlocks.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33 class CRM_Core_ShowHideBlocks {
34
35 /**
36 * The icons prefixed to block show and hide links.
37 *
38 * @var string
39 */
40 static $_showIcon, $_hideIcon;
41
42 /**
43 * The array of ids of blocks that will be shown.
44 *
45 * @var array
46 */
47 protected $_show;
48
49 /**
50 * The array of ids of blocks that will be hidden.
51 *
52 * @var array
53 */
54 protected $_hide;
55
56 /**
57 * Class constructor.
58 *
59 * @param array $show
60 * Initial value of show array.
61 * @param array $hide
62 * Initial value of hide array.
63 *
64 * @return \CRM_Core_ShowHideBlocks the newly created object
65 */
66 public function __construct($show = NULL, $hide = NULL) {
67 if (!empty($show)) {
68 $this->_show = $show;
69 }
70 else {
71 $this->_show = array();
72 }
73
74 if (!empty($hide)) {
75 $this->_hide = $hide;
76 }
77 else {
78 $this->_hide = array();
79 }
80 }
81
82 /**
83 * Load icon vars used in hide and show links.
84 */
85 public static function setIcons() {
86 if (!isset(self::$_showIcon)) {
87 $config = CRM_Core_Config::singleton();
88 self::$_showIcon = '<img src="' . $config->resourceBase . 'i/TreePlus.gif" class="action-icon" alt="' . ts('show field or section') . '"/>';
89 self::$_hideIcon = '<img src="' . $config->resourceBase . 'i/TreeMinus.gif" class="action-icon" alt="' . ts('hide field or section') . '"/>';
90 }
91 }
92
93 /**
94 * Add the values from this class to the template.
95 */
96 public function addToTemplate() {
97 $hide = $show = '';
98
99 $first = TRUE;
100 foreach (array_keys($this->_hide) as $h) {
101 if (!$first) {
102 $hide .= ',';
103 }
104 $hide .= "'$h'";
105 $first = FALSE;
106 }
107
108 $first = TRUE;
109 foreach (array_keys($this->_show) as $s) {
110 if (!$first) {
111 $show .= ',';
112 }
113 $show .= "'$s'";
114 $first = FALSE;
115 }
116
117 $template = CRM_Core_Smarty::singleton();
118 $template->assign_by_ref('hideBlocks', $hide);
119 $template->assign_by_ref('showBlocks', $show);
120 }
121
122 /**
123 * Add a value to the show array.
124 *
125 * @param string $name
126 * Id to be added.
127 */
128 public function addShow($name) {
129 $this->_show[$name] = 1;
130 if (array_key_exists($name, $this->_hide)) {
131 unset($this->_hide[$name]);
132 }
133 }
134
135 /**
136 * Add a value to the hide array.
137 *
138 * @param string $name
139 * Id to be added.
140 */
141 public function addHide($name) {
142 $this->_hide[$name] = 1;
143 if (array_key_exists($name, $this->_show)) {
144 unset($this->_show[$name]);
145 }
146 }
147
148 /**
149 * Create a well formatted html link from the smaller pieces.
150 *
151 * @param string $name
152 * Name of the link.
153 * @param string $href
154 * @param string $text
155 * @param string $js
156 *
157 * @return string
158 * the formatted html link
159 */
160 public static function linkHtml($name, $href, $text, $js) {
161 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
162 }
163
164 /**
165 * Create links that we can use in the form.
166 *
167 * @param CRM_Core_Form $form
168 * The form object.
169 * @param string $prefix
170 * The attribute that we are referencing.
171 * @param string $showLinkText
172 * The text to be shown for the show link.
173 * @param string $hideLinkText
174 * The text to be shown for the hide link.
175 *
176 * @param bool $assign
177 *
178 * @return array
179 */
180 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
181 $showCode = "if(event.preventDefault) event.preventDefault(); else event.returnValue = false; cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();";
182 $hideCode = "if(event.preventDefault) event.preventDefault(); else event.returnValue = false; cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show();";
183
184 self::setIcons();
185 $values = array();
186 $values['show'] = self::linkHtml("${prefix}_show", "#${prefix}_hide", self::$_showIcon . $showLinkText, "onclick=\"$showCode\"");
187 $values['hide'] = self::linkHtml("${prefix}_hide", "#${prefix}", self::$_hideIcon . $hideLinkText, "onclick=\"$hideCode\"");
188
189 if ($assign) {
190 $form->assign($prefix, $values);
191 }
192 else {
193 return $values;
194 }
195 }
196
197 /**
198 * Create html link elements that we can use in the form.
199 *
200 * @param CRM_Core_Form $form
201 * The form object.
202 * @param int $index
203 * The current index of the element being processed.
204 * @param int $maxIndex
205 * The max number of elements that will be processed.
206 * @param string $prefix
207 * The attribute that we are referencing.
208 * @param string $showLinkText
209 * The text to be shown for the show link.
210 * @param string $hideLinkText
211 * The text to be shown for the hide link.
212 * @param string $elementType
213 * The set the class.
214 * @param string $hideLink
215 * The hide block string.
216 */
217 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
218 $showHidePrefix = str_replace(array("]", "["), array("", "_"), $prefix);
219 $showHidePrefix = "id_" . $showHidePrefix;
220 if ($index == $maxIndex) {
221 $showCode = $hideCode = "return false;";
222 }
223 else {
224 $next = $index + 1;
225 if ($elementType) {
226 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
227 if ($hideLink) {
228 $hideCode = $hideLink;
229 }
230 else {
231 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
232 }
233 }
234 else {
235 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
236 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
237 }
238 }
239
240 self::setIcons();
241 if ($elementType) {
242 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self::$_showIcon . $showLinkText,
243 array('onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode)
244 );
245 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText,
246 array('onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode)
247 );
248 }
249 else {
250 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self::$_showIcon . $showLinkText,
251 array('onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode)
252 );
253 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText,
254 array('onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode)
255 );
256 }
257 }
258
259 }