Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Core_ShowHideBlocks { | |
18 | ||
19 | /** | |
20 | * The icons prefixed to block show and hide links. | |
21 | * | |
22 | * @var string | |
23 | */ | |
518fa0ee SL |
24 | public static $_showIcon; |
25 | public static $_hideIcon; | |
6a488035 TO |
26 | |
27 | /** | |
f9e31d7f | 28 | * The array of ids of blocks that will be shown. |
6a488035 TO |
29 | * |
30 | * @var array | |
31 | */ | |
32 | protected $_show; | |
33 | ||
34 | /** | |
f9e31d7f | 35 | * The array of ids of blocks that will be hidden. |
6a488035 TO |
36 | * |
37 | * @var array | |
38 | */ | |
39 | protected $_hide; | |
40 | ||
41 | /** | |
f9e31d7f | 42 | * Class constructor. |
6a488035 | 43 | * |
6a0b768e TO |
44 | * @param array $show |
45 | * Initial value of show array. | |
46 | * @param array $hide | |
47 | * Initial value of hide array. | |
6a488035 | 48 | * |
e0b82b44 | 49 | * @return \CRM_Core_ShowHideBlocks the newly created object |
6a488035 | 50 | */ |
00be9182 | 51 | public function __construct($show = NULL, $hide = NULL) { |
6a488035 TO |
52 | if (!empty($show)) { |
53 | $this->_show = $show; | |
54 | } | |
55 | else { | |
be2fb01f | 56 | $this->_show = []; |
6a488035 TO |
57 | } |
58 | ||
59 | if (!empty($hide)) { | |
60 | $this->_hide = $hide; | |
61 | } | |
62 | else { | |
be2fb01f | 63 | $this->_hide = []; |
6a488035 TO |
64 | } |
65 | } | |
66 | ||
67 | /** | |
f9e31d7f | 68 | * Load icon vars used in hide and show links. |
6a488035 | 69 | */ |
00be9182 | 70 | public static function setIcons() { |
6a488035 TO |
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 | /** | |
f9e31d7f | 79 | * Add the values from this class to the template. |
6a488035 | 80 | */ |
00be9182 | 81 | public function addToTemplate() { |
6a488035 TO |
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 | /** | |
f9e31d7f | 108 | * Add a value to the show array. |
6a488035 | 109 | * |
6a0b768e TO |
110 | * @param string $name |
111 | * Id to be added. | |
6a488035 | 112 | */ |
00be9182 | 113 | public function addShow($name) { |
6a488035 TO |
114 | $this->_show[$name] = 1; |
115 | if (array_key_exists($name, $this->_hide)) { | |
116 | unset($this->_hide[$name]); | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
f9e31d7f | 121 | * Add a value to the hide array. |
6a488035 | 122 | * |
6a0b768e TO |
123 | * @param string $name |
124 | * Id to be added. | |
6a488035 | 125 | */ |
00be9182 | 126 | public function addHide($name) { |
6a488035 TO |
127 | $this->_hide[$name] = 1; |
128 | if (array_key_exists($name, $this->_show)) { | |
129 | unset($this->_show[$name]); | |
130 | } | |
131 | } | |
132 | ||
133 | /** | |
f9e31d7f | 134 | * Create a well formatted html link from the smaller pieces. |
6a488035 | 135 | * |
6a0b768e TO |
136 | * @param string $name |
137 | * Name of the link. | |
6a488035 TO |
138 | * @param string $href |
139 | * @param string $text | |
140 | * @param string $js | |
141 | * | |
a6c01b45 CW |
142 | * @return string |
143 | * the formatted html link | |
6a488035 | 144 | */ |
00be9182 | 145 | public static function linkHtml($name, $href, $text, $js) { |
6a488035 TO |
146 | return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>"; |
147 | } | |
148 | ||
149 | /** | |
f9e31d7f | 150 | * Create links that we can use in the form. |
6a488035 | 151 | * |
6a0b768e TO |
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. | |
fd31fa4c EM |
160 | * |
161 | * @param bool $assign | |
6a488035 | 162 | * |
8eedd10a | 163 | * @return array |
6a488035 | 164 | */ |
00be9182 | 165 | public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) { |
3f8fccc6 CW |
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();"; | |
6a488035 TO |
168 | |
169 | self::setIcons(); | |
be2fb01f | 170 | $values = []; |
6a488035 TO |
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 | /** | |
f9e31d7f | 183 | * Create html link elements that we can use in the form. |
6a488035 | 184 | * |
6a0b768e TO |
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. | |
6a488035 | 201 | */ |
00be9182 | 202 | public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) { |
be2fb01f | 203 | $showHidePrefix = str_replace(["]", "["], ["", "_"], $prefix); |
6a488035 TO |
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, | |
be2fb01f | 228 | ['onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode] |
6a488035 TO |
229 | ); |
230 | $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText, | |
be2fb01f | 231 | ['onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode] |
6a488035 TO |
232 | ); |
233 | } | |
234 | else { | |
235 | $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self::$_showIcon . $showLinkText, | |
be2fb01f | 236 | ['onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode] |
6a488035 TO |
237 | ); |
238 | $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self::$_hideIcon . $hideLinkText, | |
be2fb01f | 239 | ['onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode] |
6a488035 TO |
240 | ); |
241 | } | |
242 | } | |
96025800 | 243 | |
6a488035 | 244 | } |