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