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