Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | */ | |
40 | static $_showIcon, $_hideIcon; | |
41 | ||
42 | /** | |
f9e31d7f | 43 | * The array of ids of blocks that will be shown. |
6a488035 TO |
44 | * |
45 | * @var array | |
46 | */ | |
47 | protected $_show; | |
48 | ||
49 | /** | |
f9e31d7f | 50 | * The array of ids of blocks that will be hidden. |
6a488035 TO |
51 | * |
52 | * @var array | |
53 | */ | |
54 | protected $_hide; | |
55 | ||
56 | /** | |
f9e31d7f | 57 | * Class constructor. |
6a488035 | 58 | * |
6a0b768e TO |
59 | * @param array $show |
60 | * Initial value of show array. | |
61 | * @param array $hide | |
62 | * Initial value of hide array. | |
6a488035 | 63 | * |
e0b82b44 | 64 | * @return \CRM_Core_ShowHideBlocks the newly created object |
6a488035 | 65 | */ |
00be9182 | 66 | public function __construct($show = NULL, $hide = NULL) { |
6a488035 TO |
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 | /** | |
f9e31d7f | 83 | * Load icon vars used in hide and show links. |
6a488035 | 84 | */ |
00be9182 | 85 | public static function setIcons() { |
6a488035 TO |
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 | /** | |
f9e31d7f | 94 | * Add the values from this class to the template. |
6a488035 | 95 | */ |
00be9182 | 96 | public function addToTemplate() { |
6a488035 TO |
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 | /** | |
f9e31d7f | 123 | * Add a value to the show array. |
6a488035 | 124 | * |
6a0b768e TO |
125 | * @param string $name |
126 | * Id to be added. | |
6a488035 | 127 | */ |
00be9182 | 128 | public function addShow($name) { |
6a488035 TO |
129 | $this->_show[$name] = 1; |
130 | if (array_key_exists($name, $this->_hide)) { | |
131 | unset($this->_hide[$name]); | |
132 | } | |
133 | } | |
134 | ||
135 | /** | |
f9e31d7f | 136 | * Add a value to the hide array. |
6a488035 | 137 | * |
6a0b768e TO |
138 | * @param string $name |
139 | * Id to be added. | |
6a488035 | 140 | */ |
00be9182 | 141 | public function addHide($name) { |
6a488035 TO |
142 | $this->_hide[$name] = 1; |
143 | if (array_key_exists($name, $this->_show)) { | |
144 | unset($this->_show[$name]); | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
f9e31d7f | 149 | * Create a well formatted html link from the smaller pieces. |
6a488035 | 150 | * |
6a0b768e TO |
151 | * @param string $name |
152 | * Name of the link. | |
6a488035 TO |
153 | * @param string $href |
154 | * @param string $text | |
155 | * @param string $js | |
156 | * | |
a6c01b45 CW |
157 | * @return string |
158 | * the formatted html link | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function linkHtml($name, $href, $text, $js) { |
6a488035 TO |
161 | return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>"; |
162 | } | |
163 | ||
164 | /** | |
f9e31d7f | 165 | * Create links that we can use in the form. |
6a488035 | 166 | * |
6a0b768e TO |
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. | |
fd31fa4c EM |
175 | * |
176 | * @param bool $assign | |
6a488035 | 177 | * |
8eedd10a | 178 | * @return array |
6a488035 | 179 | */ |
00be9182 | 180 | public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) { |
6a488035 TO |
181 | $showCode = "cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();"; |
182 | $hideCode = "cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show(); return false;"; | |
183 | ||
184 | self::setIcons(); | |
353ffa53 | 185 | $values = array(); |
6a488035 TO |
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 | /** | |
f9e31d7f | 198 | * Create html link elements that we can use in the form. |
6a488035 | 199 | * |
6a0b768e TO |
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. | |
6a488035 | 216 | */ |
00be9182 | 217 | public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) { |
6a488035 TO |
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 | } | |
96025800 | 258 | |
6a488035 | 259 | } |