3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Core_ShowHideBlocks
{
36 * The icons prefixed to block show and hide links.
40 public static $_showIcon;
41 public static $_hideIcon;
44 * The array of ids of blocks that will be shown.
51 * The array of ids of blocks that will be hidden.
61 * Initial value of show array.
63 * Initial value of hide array.
65 * @return \CRM_Core_ShowHideBlocks the newly created object
67 public function __construct($show = NULL, $hide = NULL) {
84 * Load icon vars used in hide and show links.
86 public static function setIcons() {
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') . '"/>';
95 * Add the values from this class to the template.
97 public function addToTemplate() {
101 foreach (array_keys($this->_hide
) as $h) {
110 foreach (array_keys($this->_show
) as $s) {
118 $template = CRM_Core_Smarty
::singleton();
119 $template->assign_by_ref('hideBlocks', $hide);
120 $template->assign_by_ref('showBlocks', $show);
124 * Add a value to the show array.
126 * @param string $name
129 public function addShow($name) {
130 $this->_show
[$name] = 1;
131 if (array_key_exists($name, $this->_hide
)) {
132 unset($this->_hide
[$name]);
137 * Add a value to the hide array.
139 * @param string $name
142 public function addHide($name) {
143 $this->_hide
[$name] = 1;
144 if (array_key_exists($name, $this->_show
)) {
145 unset($this->_show
[$name]);
150 * Create a well formatted html link from the smaller pieces.
152 * @param string $name
154 * @param string $href
155 * @param string $text
159 * the formatted html link
161 public static function linkHtml($name, $href, $text, $js) {
162 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
166 * Create links that we can use in the form.
168 * @param CRM_Core_Form $form
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.
177 * @param bool $assign
181 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
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();";
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\"");
191 $form->assign($prefix, $values);
199 * Create html link elements that we can use in the form.
201 * @param CRM_Core_Form $form
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
215 * @param string $hideLink
216 * The hide block string.
218 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
219 $showHidePrefix = str_replace(["]", "["], ["", "_"], $prefix);
220 $showHidePrefix = "id_" . $showHidePrefix;
221 if ($index == $maxIndex) {
222 $showCode = $hideCode = "return false;";
227 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
229 $hideCode = $hideLink;
232 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
236 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
237 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
243 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
244 ['onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode]
246 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
247 ['onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode]
251 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
252 ['onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode]
254 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
255 ['onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode]