Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
EditableDropDownCollectionElement.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
44  {
45  protected $collectionCountData;
46 
47  protected function renderEditable()
48  {
49  return $this->renderControlEditable();
50  }
51 
52  protected function renderControlEditable()
53  {
54  assert('in_array("CollectionAttributeFormInterface", class_implements($this->model))');
55  assert('$this->model->{$this->attribute} == null || is_array($this->model->{$this->attribute})');
56  $cClipWidget = new CClipWidget();
57  $cClipWidget->beginClip("EditableDropDownSortable");
58  $cClipWidget->widget('application.core.widgets.JuiSortable', array(
59  'itemTemplate' => $this->renderItemTemplate(),
60  'items' => $this->getItems(),
61  'options' => array(
62  'placeholder' => 'ui-state-highlight',
63  'stop' => 'js:function(event, ui){' .
64  $this->renderScriptCallToRebuildSelectInputFromInputs() . '}',
65  ),
66  'baseInputNameForSortableCollection' => $this->resolveInputNamePrefix() . '[' . $this->attribute . ']',
67  'htmlOptions' =>
68  array(
69  'id' => $this->attribute . '_ul',
70  'class' => 'sortable',
71  )
72  ));
73  $cClipWidget->endClip();
74  $this->registerScripts();
75  $content = $this->renderError();
76  $content .= $this->renderMultipleAttributesUsingCollectionContent();
77  $content .= $this->renderRemovalNoticeContent();
78  $content .= '<div id="sortable-editable-dropdown-collection">';
79  $content .= $cClipWidget->getController()->clips['EditableDropDownSortable'];
80  $content .= '</div>';
81  $content .= $this->renderAddInputAndAddButton();
82 
83  return $content;
84  }
85 
86  protected function getItems()
87  {
88  $items = array();
89  $dropDownCountData = $this->getCollectionCountData();
90  foreach ($this->getDropDownArray() as $order => $name)
91  {
92  if (isset($dropDownCountData[$name]))
93  {
94  $removalContent = null;
95  }
96  else
97  {
98  $removalContent = $this->renderRemoveLink();
99  }
100  $items[$order] = array('{content}' => $name,
101  '{removalContent}' => $removalContent,
102  '{dataLengthPlusOne}' => $order + 1);
103 
104  $activeLanguagesData = $this->getActiveLanguagesData();
105  $labelsAttributeName = $this->getLabelsAttributeName();
106  $labelsData = $this->model->$labelsAttributeName;
107  foreach ($activeLanguagesData as $language => $notUsed)
108  {
109  if ($labelsAttributeName !== null &&
110  isset($labelsData[$language]) &&
111  isset($labelsData[$language][$order]))
112  {
113  $items[$order]['{' . $language . 'Label}'] = $labelsData[$language][$order];
114  }
115  else
116  {
117  $items[$order]['{' . $language . 'Label}'] = Zurmo::t('CustomField', $name, array(), null, $language);;
118  }
119  }
120  }
121  return $items;
122  }
123 
124  protected function canAllItemsBeRemoved()
125  {
126  $dropDownCountData = $this->getCollectionCountData();
127  foreach ($this->getDropDownArray() as $order => $name)
128  {
129  if (isset($dropDownCountData[$name]))
130  {
131  return false;
132  }
133  }
134  return true;
135  }
136 
137  protected function getCollectionCountData()
138  {
139  if ($this->collectionCountData == null)
140  {
141  $this->collectionCountData = $this->model->getCollectionCountData();
142  }
143  return $this->collectionCountData;
144  }
145 
146  protected function renderItemTemplate()
147  {
148  $activeLanguagesData = $this->getActiveLanguagesData();
149  $baseLanguage = $this->getBaseLanguage();
150  return '<li class="ui-state-default" id="editableDropDown_{id}">
151  <span class="ui-icon ui-icon-arrowthick-2-n-s">&#160;</span>
152  <div class="has-lang-label">
153  <input name = "' . $this->getNameForInputField() .
154  '" id = "' . $this->getIdForInputField('{id}') .
155  '" type = "text" value = "{content}" size="50"/>
156  <input name = "' . $this->getNameForExistingValueHiddenField() . '" type = "hidden" value = "{content}"/>
157  ' . static::renderLanguageLabelHtmlContent($activeLanguagesData[$baseLanguage]['name']) .
158  '</div>{removalContent}' .
159  $this->renderSortableLanguageLabelInputsForAddingNewValuesJuiSortableContent() .
160  '</li>';
161  }
162 
163  protected function renderRemoveLink()
164  {
165  return ZurmoHtml::link( ZurmoHtml::tag('span', array(), Zurmo::t('Core', 'Remove')), '#',
166  array('class' => 'remove-sortable-item-link'));
167  }
168 
169  protected function renderAddInputAndAddButton()
170  {
171  $activeLanguagesData = $this->getActiveLanguagesData();
172  $baseLanguage = $this->getBaseLanguage();
173  $content = '<table id="picklist-values">';
174  $content .= '<colgroup><col style="width:50%" />';
175  $content .= '</colgroup>';
176  $content .= '<tbody>';
177  $content .= '<tr><td>';
178  $content .= '<div class="has-lang-label">' . ZurmoHtml::textField( $this->attribute . '_AddInput', '', array('size' => 50));
179  $content .= static::renderLanguageLabelHtmlContent($activeLanguagesData[$baseLanguage]['name']);
180  $content .= '</div>';
181  $content .= '<div id="' . $this->attribute . '_AddInput_em_" class="errorMessage" style="display:none"></div>';
182  $content .= '</td></tr>';
183  $content .= '<tr><td>';
184  $content .= ZurmoHtml::link(ZurmoHtml::wrapLabel(Zurmo::t('Core', 'Add Item')), '#', array('id' => $this->attribute . '_AddInputButton'));
185  $content .= '</td></tr>';
186  $content .= '</tbody>';
187  $content .= '</table>';
188  return $content;
189  }
190 
191  protected function registerScripts()
192  {
193  $supportedLanguagesData = $this->getActiveLanguagesData();
194  $baseLanguage = $this->getBaseLanguage();
195  Yii::app()->clientScript->registerScriptFile(
196  Yii::app()->getAssetManager()->publish(
197  Yii::getPathOfAlias('application.core.elements.assets')
198  ) . '/SelectInputUtils.js',
199  CClientScript::POS_END
200  );
201  $inputIdPrefix = $this->getInputIdPrefix();
202  Yii::app()->clientScript->registerScript('editableDropDown', "
203  " . $this->renderItemsOnChangeScript() . "
204  $('.remove-sortable-item-link').live('click', function()
205  {
206  $(this).parent().remove();
207  " . $this->renderScriptCallToRebuildSelectInputFromInputs() . "
208  return false;
209  }
210  );
211  $('#" . $this->attribute . "_AddInputButton').click(function()
212  {
213  if ($('#" . $this->attribute . "_AddInput').val()=='')
214  {
215  $('#" . $this->attribute . "_AddInput').addClass($.fn.yiiactiveform.getSettings('#edit-form').errorCssClass);
216  $('#" . $this->attribute . "_AddInput_em_').html('" . Zurmo::t('Core', 'You must enter a value') . "').show();
217  return false;
218  }
219  else
220  {
221  $('#" . $this->attribute . "_AddInput').removeClass($.fn.yiiactiveform.getSettings('#edit-form').errorCssClass);
222  $('#" . $this->attribute . "_AddInput_em_').html('').hide();
223  }
224  var currenInputCollectionLength = $('input[name=\"" . $this->getNameForInputField() . "\"]').length;
225  var newLi = $('<li class=\"ui-state-default\" id=\"{id}\">' +
226  '" . $this->renderRemoveLink() . "' +
227  '<span class=\"ui-icon ui-icon-arrowthick-2-n-s\">&#160;</span>' +
228  '<div class=\"has-lang-label\">' +
229  '<input name=\"" . $this->getNameForInputField() . "\" id=\"" . $inputIdPrefix .
230  "' + ($('input[name=\"" . $this->getNameForInputField() . "\"]').length + 1) +'\" type=\"text\" value=\"' +
231  $('#" . $this->attribute . "_AddInput').val()
232  + '\" size=\"50\"/>" . static::renderLanguageLabelHtmlContent($supportedLanguagesData[$baseLanguage]['name']) . "</div>' +
233  '<input name=\"" . $this->getNameForExistingValueHiddenField() . "\" type=\"hidden\" value=\"' +
234  $('#" . $this->attribute . "_AddInput').val() + '\" />" . "' +
235  '" . $this->renderSortableLanguageLabelInputsForAddingNewValuesJavaScriptContent(). "' +
236  '</li>');
237  newLi.appendTo($('#" . $this->attribute . "_ul'));
238  $('#" . $this->attribute . "_AddInput').val('');
239  $('#" . $inputIdPrefix . "' + (currenInputCollectionLength + 1)).change(function()
240  {
241  " . $this->renderScriptCallToRebuildSelectInputFromInputs() . "
242  }
243  );
244  " . $this->renderScriptCallToRebuildSelectInputFromInputs() . "
245  $('.has-lang-label', newLi).each(
246  function()
247  {
248  $(this).resolvePaddingForHasLangLabel( $(this) );
249  }
250  );
251  return false;
252  }
253  );
254  ");
255  }
256 
257  protected function renderSortableLanguageLabelInputsForAddingNewValuesJavaScriptContent()
258  {
259  $activeLanguagesData = $this->getActiveLanguagesData();
260  $baseLanguage = $this->getBaseLanguage();
261  $content = null;
262  foreach ($activeLanguagesData as $language => $languageData)
263  {
264  if ($language != $baseLanguage)
265  {
266  $content .= "<div class=\"has-lang-label\"><input name=\"" . $this->getNameForLabelInputField($language) . "\" id=\"" . $this->getLabelInputIdPrefix($language) .
267  "' + ($('input[name=\"" . $this->getNameForLabelInputField($language) . "\"]').length + 1) +'\"" .
268  " type=\"text\" value=\"' + $('#" . $this->attribute . "_AddInput').val() + '\" size=\"50\"/>";
269  $content .= static::renderLanguageLabelHtmlContent($languageData['name']) . "</div>";
270  }
271  }
272  return $content;
273  }
274 
275  protected function renderSortableLanguageLabelInputsForAddingNewValuesJuiSortableContent()
276  {
277  $activeLanguagesData = $this->getActiveLanguagesData();
278  $baseLanguage = $this->getBaseLanguage();
279  $content = null;
280  foreach ($activeLanguagesData as $language => $languageData)
281  {
282  if ($language != $baseLanguage)
283  {
284  $content .= "<div class=\"has-lang-label\"><input name=\"" . $this->getNameForLabelInputField($language) . "\" id=\"" . $this->getLabelInputIdPrefix($language) .
285  "{dataLengthPlusOne}\"" .
286  " type=\"text\" value=\"{" . $language . "Label}\" size=\"50\"/>";
287  $content .= static::renderLanguageLabelHtmlContent($languageData['name']) . "</div>";
288  }
289  }
290  return $content;
291  }
292 
293  protected function renderScriptCallToRebuildSelectInputFromInputs()
294  {
295  assert('$this->getSpecificValueFromDropDownAttributeName() != null');
296  return "rebuildSelectInputFromInputs(
297  '" . $this->resolveInputIdPrefix() . "_" .
298  $this->getSpecificValueFromDropDownAttributeName() . "', '" .
299  $this->getNameForInputField() . "');";
300  }
301 
302  protected function renderItemsOnChangeScript()
303  {
304  $content = null;
305  foreach ($this->getDropDownArray() as $key => $item)
306  {
307  $content .= "$('#" . $this->getIdForInputField($key) . "').change(function()
308  {
309  " . $this->renderScriptCallToRebuildSelectInputFromInputs() . "
310  }
311  );";
312  }
313  return $content;
314  }
315 
316  protected function renderControlNonEditable()
317  {
318  return Yii::app()->format->text($this->model->{$this->attribute});
319  }
320 
321  protected function getSpecificValueFromDropDownAttributeName()
322  {
323  if (isset($this->params['specificValueFromDropDownAttributeName']))
324  {
325  return $this->params['specificValueFromDropDownAttributeName'];
326  }
327  return null;
328  }
329 
333  protected function getBaseLanguage()
334  {
335  if (isset($this->params['baseLanguage']))
336  {
337  return $this->params['baseLanguage'];
338  }
339  throw new NotSupportedException();
340  }
341 
345  protected function getActiveLanguagesData()
346  {
347  if (isset($this->params['activeLanguagesData']))
348  {
349  if (count($this->params['activeLanguagesData']) == 0)
350  {
351  throw new NotSupportedException();
352  }
353  return $this->params['activeLanguagesData'];
354  }
355  throw new NotSupportedException();
356  }
357 
361  protected function getLabelsAttributeName()
362  {
363  if (isset($this->params['labelsAttributeName']))
364  {
365  return $this->params['labelsAttributeName'];
366  }
367  throw new NotSupportedException();
368  }
369 
370  protected function getIdForInputField($suffix)
371  {
372  return $this->resolveInputIdPrefix() . '_' . $this->attribute . '_'. $suffix;
373  }
374 
375  protected function getNameForInputField()
376  {
377  return $this->resolveInputNamePrefix() . '[' . $this->attribute . '][]';
378  }
379 
380  protected function getNameForLabelInputField($language)
381  {
382  assert('is_string($language)');
383  return $this->resolveInputNamePrefix() . '[' . $this->getLabelsAttributeName() . '][' . $language . '][]';
384  }
385 
386  protected function getNameForExistingValueHiddenField()
387  {
388  return $this->resolveInputNamePrefix() . '[' . $this->attribute . 'ExistingValues][]';
389  }
390 
391  protected function getInputIdPrefix()
392  {
393  return $this->resolveInputIdPrefix() . '_' . $this->attribute . '_';
394  }
395 
396  protected function getLabelInputIdPrefix($language)
397  {
398  assert('is_string($language)');
399  return $this->resolveInputIdPrefix() . '_' . $this->getLabelsAttributeName() . '_' . $language . '_';
400  }
401 
402  protected function getDropDownArray()
403  {
404  $dropDownArray = $this->model->{$this->attribute};
405  if ($dropDownArray == null)
406  {
407  return array();
408  }
409  return $dropDownArray;
410  }
411 
412  protected function renderMultipleAttributesUsingCollectionContent()
413  {
414  $content = '';
415  $modelLabelAttributeLabelsData = $this->model->getModelPluralNameAndAttributeLabelsThatUseCollectionData();
416  if (count($modelLabelAttributeLabelsData) > 1)
417  {
418  $message = Zurmo::t('Core', 'This pick-list is used by more than one module.');
419  foreach ($modelLabelAttributeLabelsData as $modelLabel => $attributeLabel)
420  {
421  $message .= '<br/>' . $modelLabel . '&#160;-&#160;' . $attributeLabel;
422  }
423  $content .= HtmlNotifyUtil::renderHighlightBoxByMessage($message);
424  }
425  return $content;
426  }
427 
428  protected function renderRemovalNoticeContent()
429  {
430  if ($this->canAllItemsBeRemoved())
431  {
432  return;
433  }
434  $message = Zurmo::t('Core', 'Some values cannot be removed because they are currently in use. Try changing the records that use them first.');
435  $content = HtmlNotifyUtil::renderHighlightBoxByMessage($message);
436  return $content;
437  }
438 
439  protected static function renderLanguageLabelHtmlContent($label)
440  {
441  return ZurmoHtml::tag('span', array(), $label);
442  }
443  }
444 ?>
static wrapLabel($label, $class= 'z-label')
Definition: ZurmoHtml.php:656
static renderHighlightBoxByMessage($message)
renderError()
Definition: Element.php:139
Generated on Sat Jul 11 2020 07:10:25
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.