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