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