3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Core_ShowHideBlocks
{
20 * The icons prefixed to block show and hide links.
24 public static $_showIcon;
25 public static $_hideIcon;
28 * The array of ids of blocks that will be shown.
35 * The array of ids of blocks that will be hidden.
45 * Initial value of show array.
47 * Initial value of hide array.
49 * @return \CRM_Core_ShowHideBlocks the newly created object
51 public function __construct($show = NULL, $hide = NULL) {
68 * Load icon vars used in hide and show links.
70 public static function setIcons() {
71 if (!isset(self
::$_showIcon)) {
72 $config = CRM_Core_Config
::singleton();
73 self
::$_showIcon = '<img src="' . $config->resourceBase
. 'i/TreePlus.gif" class="action-icon" alt="' . ts('show field or section') . '"/>';
74 self
::$_hideIcon = '<img src="' . $config->resourceBase
. 'i/TreeMinus.gif" class="action-icon" alt="' . ts('hide field or section') . '"/>';
79 * Add the values from this class to the template.
81 public function addToTemplate() {
85 foreach (array_keys($this->_hide
) as $h) {
94 foreach (array_keys($this->_show
) as $s) {
102 $template = CRM_Core_Smarty
::singleton();
103 $template->assign_by_ref('hideBlocks', $hide);
104 $template->assign_by_ref('showBlocks', $show);
108 * Add a value to the show array.
110 * @param string $name
113 public function addShow($name) {
114 $this->_show
[$name] = 1;
115 if (array_key_exists($name, $this->_hide
)) {
116 unset($this->_hide
[$name]);
121 * Add a value to the hide array.
123 * @param string $name
126 public function addHide($name) {
127 $this->_hide
[$name] = 1;
128 if (array_key_exists($name, $this->_show
)) {
129 unset($this->_show
[$name]);
134 * Create a well formatted html link from the smaller pieces.
136 * @param string $name
138 * @param string $href
139 * @param string $text
143 * the formatted html link
145 public static function linkHtml($name, $href, $text, $js) {
146 return '<a name="' . $name . '" id="' . $name . '" href="' . $href . '" ' . $js . ">$text</a>";
150 * Create links that we can use in the form.
152 * @param CRM_Core_Form $form
154 * @param string $prefix
155 * The attribute that we are referencing.
156 * @param string $showLinkText
157 * The text to be shown for the show link.
158 * @param string $hideLinkText
159 * The text to be shown for the hide link.
161 * @param bool $assign
165 public static function links(&$form, $prefix, $showLinkText, $hideLinkText, $assign = TRUE) {
166 $showCode = "if(event.preventDefault) event.preventDefault(); else event.returnValue = false; cj('#id_{$prefix}').show(); cj('#id_{$prefix}_show').hide();";
167 $hideCode = "if(event.preventDefault) event.preventDefault(); else event.returnValue = false; cj('#id_{$prefix}').hide(); cj('#id_{$prefix}_show').show();";
171 $values['show'] = self
::linkHtml("${prefix}_show", "#${prefix}_hide", self
::$_showIcon . $showLinkText, "onclick=\"$showCode\"");
172 $values['hide'] = self
::linkHtml("${prefix}_hide", "#${prefix}", self
::$_hideIcon . $hideLinkText, "onclick=\"$hideCode\"");
175 $form->assign($prefix, $values);
183 * Create html link elements that we can use in the form.
185 * @param CRM_Core_Form $form
188 * The current index of the element being processed.
189 * @param int $maxIndex
190 * The max number of elements that will be processed.
191 * @param string $prefix
192 * The attribute that we are referencing.
193 * @param string $showLinkText
194 * The text to be shown for the show link.
195 * @param string $hideLinkText
196 * The text to be shown for the hide link.
197 * @param string $elementType
199 * @param string $hideLink
200 * The hide block string.
202 public function linksForArray(&$form, $index, $maxIndex, $prefix, $showLinkText, $hideLinkText, $elementType = NULL, $hideLink = NULL) {
203 $showHidePrefix = str_replace(["]", "["], ["", "_"], $prefix);
204 $showHidePrefix = "id_" . $showHidePrefix;
205 if ($index == $maxIndex) {
206 $showCode = $hideCode = "return false;";
211 $showCode = "cj('#${prefix}_${next}_show').show(); return false;";
213 $hideCode = $hideLink;
216 $hideCode = "cj('#${prefix}_${next}_show, #${prefix}_${next}').hide(); return false;";
220 $showCode = "cj('#{$showHidePrefix}_{$next}_show').show(); return false;";
221 $hideCode = "cj('#{$showHidePrefix}_{$next}_show, #{$showHidePrefix}_{$next}').hide(); return false;";
227 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
228 ['onclick' => "cj('#${prefix}_${index}_show').hide(); cj('#${prefix}_${index}').show();" . $showCode]
230 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
231 ['onclick' => "cj('#${prefix}_${index}').hide(); cj('#${prefix}_${index}_show').show();" . $hideCode]
235 $form->addElement('link', "${prefix}[${index}][show]", NULL, "#${prefix}_${index}", self
::$_showIcon . $showLinkText,
236 ['onclick' => "cj('#{$showHidePrefix}_{$index}_show').hide(); cj('#{$showHidePrefix}_{$index}').show();" . $showCode]
238 $form->addElement('link', "${prefix}[${index}][hide]", NULL, "#${prefix}_${index}", self
::$_hideIcon . $hideLinkText,
239 ['onclick' => "cj('#{$showHidePrefix}_{$index}').hide(); cj('#{$showHidePrefix}_{$index}_show').show();" . $hideCode]