Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelElement.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  abstract class ModelElement extends Element implements ElementActionTypeInterface
45  {
46  const MODAL_CONTAINER_PREFIX = 'modalContainer';
47 
48  const CLEAR_LINK_ID = 'clear-link';
49 
53  protected static $moduleId;
54 
59  protected $controllerId = 'default';
60 
64  protected $idAttributeId = 'id';
65 
69  protected static $autoCompleteActionId = 'autoComplete';
70 
74  protected static $modalActionId = 'modalList';
75 
80  protected static $editableActionType = 'ModalList';
81 
86  protected static $nonEditableActionType = 'Details';
87 
88  protected $hideSelectLinkWhenDisabled = true;
89 
90  public function doNotHideSelectLinkWhenDisabled()
91  {
92  $this->hideSelectLinkWhenDisabled = false;
93  }
94 
95  public function setIdAttributeId($idAttributeId)
96  {
97  assert('is_string($idAttributeId)');
98  $this->idAttributeId = $idAttributeId;
99  }
100 
101  protected function renderControlEditable()
102  {
103  assert('$this->model->{$this->attribute} instanceof RedBeanModel');
104  return $this->renderEditableContent();
105  }
106 
113  protected function renderEditableContent()
114  {
115  $cs = Yii::app()->getClientScript();
116  $cs->registerCoreScript('bbq');
117  $cs->registerScriptFile(
118  Yii::app()->getAssetManager()->publish(
119  Yii::getPathOfAlias('application.core.elements.assets')
120  ) . '/Modal.js',
121  CClientScript::POS_END
122  );
123  $content = $this->form->hiddenField($this->model, $this->idAttributeId, $this->getIdInputHtmlOptions());
124  if (!$this->showOnlyHiddenInputForEditable())
125  {
126  $inputContent = $this->renderTextField($this->getIdForHiddenField());
127  $inputContent .= $this->renderSelectLink();
128  $this->wrapHasModelSelectInput($inputContent);
129  $content .= $inputContent;
130  }
131  $content .= $this->renderExtraHtmlContent();
132  return $content;
133  }
134 
135  protected function wrapHasModelSelectInput(& $content)
136  {
137  $content = ZurmoHtml::tag('div', array('class' => 'has-model-select'), $content);
138  }
139 
140  protected function getIdInputHtmlOptions()
141  {
142  $idInputHtmlOptions = array(
143  'name' => $this->getNameForHiddenField(),
144  'id' => $this->getIdForHiddenField(),
145  'disabled' => $this->getDisabledValue(),
146  'value' => $this->getId(),
147  );
148  return $idInputHtmlOptions;
149  }
150 
157  protected function renderTextField($idInputName)
158  {
160  $cClipWidget = new CClipWidget();
161  $cClipWidget->beginClip("ModelElement");
162  // Begin Not Coding Standard
163  $cClipWidget->widget('zii.widgets.jui.CJuiAutoComplete', array(
164  'name' => $this->getNameForTextField(),
165  'id' => $this->getIdForTextField(),
166  'value' => $this->getName(),
167  'source' => Yii::app()->createUrl($this->resolveModuleId() . '/' . $this->getAutoCompleteControllerId()
168  . '/' . static::$autoCompleteActionId, $this->getAutoCompleteUrlParams()),
169  'options' => array(
170  'select' => $this->getOnSelectOptionForAutoComplete($idInputName),
171  'appendTo' => 'js:$("#' . $this->getIdForTextField() . '").parent().parent()',
172  'search' => 'js: function(event, ui)
173  {
174  var context = $("#' . $this->getIdForTextField() . '").parent();
175  $(".model-select-icon", context).fadeOut(100);
176  $(this).makeOrRemoveTogglableSpinner(true, context);
177  }',
178  'create' => 'js: function(event, ui)
179  {
180  var context = $("#' . $this->getIdForTextField() . '");
181  context.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
182  if(item.id == null){
183  item.value = null;
184  };
185  return $("<li>")
186  .append($("<a>").text(item.label))
187  .appendTo(ul);
188  };
189  }',
190  'open' => 'js: function(event, ui)
191  {
192  var context = $("#' . $this->getIdForTextField() . '").parent();
193  $(".model-select-icon", context).fadeIn(250);
194  $(this).makeOrRemoveTogglableSpinner(false, context);
195  }',
196  'close' => 'js: function(event, ui)
197  {
198  var context = $("#' . $this->getIdForTextField() . '").parent();
199  $(".model-select-icon", context).fadeIn(250);
200  $(this).makeOrRemoveTogglableSpinner(false, context);
201  }',
202  'response' => 'js: function(event, ui)
203  {
204  if (ui.content.length < 1)
205  {
206  var context = $("#' . $this->getIdForTextField() . '").parent();
207  $(".model-select-icon", context).fadeIn(250);
208  $(this).makeOrRemoveTogglableSpinner(false, context);
209  }
210  }'
211  ),
212  'htmlOptions' => array_merge(
213  array(
214  'disabled' => $this->getDisabledValue(),
215  'onblur' => 'clearIdFromAutoCompleteField($(this).val(), \'' . $idInputName . '\');'
216  ), $this->resolveAdditionalHtmlOptionsForTextField($idInputName))
217  ));
218  // End Not Coding Standard
219  $cClipWidget->endClip();
220  return $cClipWidget->getController()->clips['ModelElement'];
221  }
222 
226  protected function resolveAdditionalHtmlOptionsForTextField($idInputName)
227  {
228  return array();
229  }
230 
231  protected function getAutoCompleteUrlParams()
232  {
233  return array();
234  }
235 
236  protected function getAutoCompleteControllerId()
237  {
238  return $this->controllerId;
239  }
240 
246  protected function renderSelectLink()
247  {
248  $id = $this->getIdForSelectLink();
249  $content = ZurmoHtml::ajaxLink('<span class="model-select-icon"></span><span class="z-spinner"></span>',
250  Yii::app()->createUrl($this->resolveModuleId() . '/' . $this->getSelectLinkControllerId() . '/'.
251  static::$modalActionId .'/', $this->getSelectLinkUrlParams()),
252  $this->resolveAjaxOptionsForSelectingModel($id),
253  array(
254  'id' => $id,
255  'style' => $this->getSelectLinkStartingStyle(),
256  'namespace' => 'selectLink',
257  )
258  );
259  return $content;
260  }
261 
262  protected function getSelectLinkUrlParams()
263  {
264  return array(
265  'modalTransferInformation' => $this->getModalTransferInformation(),
266  );
267  }
268 
269  protected function resolveAjaxOptionsForSelectingModel($formId)
270  {
271  assert('is_string($formId)');
272  $title = $this->getModalTitleForSelectingModel();
273  return ModalView::getAjaxOptionsForModalLink($title, $this->getModalContainerId());
274  }
275 
276  protected function getModalTitleForSelectingModel()
277  {
278  $module = Yii::app()->getModule(static::$moduleId);
279  $moduleSingularLabel = $module->getModuleLabelByTypeAndLanguage('Singular');
280  return Zurmo::t('Core', '{moduleSingularLabel} Search',
281  array('{moduleSingularLabel}' => $moduleSingularLabel));
282  }
283 
284  protected function getModalContainerId()
285  {
286  return self::MODAL_CONTAINER_PREFIX . '-' . $this->form->id;
287  }
288 
289  protected function getSelectLinkControllerId()
290  {
291  return $this->controllerId;
292  }
293 
298  protected function renderControlNonEditable()
299  {
300  if (is_object($this->model->{$this->attribute}) && $this->model->{$this->attribute}->id > 0)
301  {
302  if ($this->showLinkOnNonEditable())
303  {
304  return $this->makeNonEditableLink();
305  }
306  else
307  {
308  return Yii::app()->format->text($this->model->{$this->attribute});
309  }
310  }
311  }
312 
313  protected function makeNonEditableLink()
314  {
315  return ZurmoHtml::link(Yii::app()->format->text($this->model->{$this->attribute}),
316  $this->makeNonEditableLinkUrl()
317  );
318  }
319 
320  protected function makeNonEditableLinkUrl()
321  {
322  return Yii::app()->createUrl($this->resolveModuleId() . '/' . $this->controllerId .
323  '/details/', array('id' => $this->model->{$this->attribute}->id));
324  }
325 
329  protected function renderLabel()
330  {
331  if ($this->form === null)
332  {
333  return $this->getFormattedAttributeLabel();
334  }
335  $id = $this->getIdForHiddenField();
336  return $this->form->labelEx($this->model, $this->attribute, array('for' => $id));
337  }
338 
339  protected function getIdForHiddenField()
340  {
341  return $this->getEditableInputId($this->attribute, 'id');
342  }
343 
344  protected function getNameForHiddenField()
345  {
346  return $this->getEditableInputName($this->attribute, 'id');
347  }
348 
349  protected function getIdForTextField()
350  {
351  return $this->getEditableInputId($this->attribute, 'name');
352  }
353 
354  protected function getNameForTextField()
355  {
356  return $this->getEditableInputId($this->attribute, 'name');
357  }
358 
359  protected function getIdForSelectLink()
360  {
361  return $this->getEditableInputId($this->attribute, 'SelectLink');
362  }
363 
368  public function getEditableNameIds()
369  {
370  return array(
371  $this->getIdForHiddenField(),
372  $this->getIdForTextField(),
373  $this->getIdForSelectLink()
374  );
375  }
376 
381  protected function getName()
382  {
383  if (is_object($this->model->{$this->attribute}) && $this->model->{$this->attribute}->id > 0)
384  {
385  return $this->model->{$this->attribute};
386  }
387  return '';
388  }
389 
394  protected function getId()
395  {
396  if (is_object($this->model->{$this->attribute}) && $this->model->{$this->attribute}->id > 0)
397  {
398  return $this->model->{$this->attribute}->id;
399  }
400  return '';
401  }
402 
407  protected function getResolvedModel()
408  {
409  return $this->model->{$this->attribute};
410  }
411 
415  protected function getModalTransferInformation()
416  {
417  return array_merge(array(
418  'sourceIdFieldId' => $this->getIdForHiddenField(),
419  'sourceNameFieldId' => $this->getIdForTextField(),
420  'modalId' => $this->getModalContainerId(),
421  ), $this->resolveSourceModelIdForModalTransferInformation());
422  }
423 
424  protected function resolveSourceModelIdForModalTransferInformation()
425  {
426  return array('sourceModelId' => $this->model->id);
427  }
428 
429  protected function getSelectLinkStartingStyle()
430  {
431  if ($this->getDisabledValue() == 'disabled' && $this->hideSelectLinkWhenDisabled)
432  {
433  return 'display:none';
434  }
435  else
436  {
437  return null;
438  }
439  }
440 
444  protected function showLinkOnNonEditable()
445  {
446  if (isset($this->params['noLink']) && $this->params['noLink'])
447  {
448  return false;
449  }
450  return true;
451  }
452 
457  protected function showOnlyHiddenInputForEditable()
458  {
459  if (isset($this->params['onlyHiddenInput']) && $this->params['onlyHiddenInput'])
460  {
461  return true;
462  }
463  return false;
464  }
465 
470  protected function resolveModuleId()
471  {
472  return static::getModuleId();
473  }
474 
475  public static function getModuleId()
476  {
477  return static::$moduleId;
478  }
479 
485  public static function getEditableActionType()
486  {
487  return static::$editableActionType;
488  }
489 
494  public static function getNonEditableActionType()
495  {
496  return static::$nonEditableActionType;
497  }
498 
502  protected function registerScriptForAutoCompleteTextField()
503  {
504  $script = "
505  function clearIdFromAutoCompleteField(value, id)
506  {
507  if (value == '')
508  {
509  $('#' + id).val('');
510  }
511  }
512  ";
513  Yii::app()->clientScript->registerScript(
514  'clearIdFromAutoCompleteField',
515  $script,
516  CClientScript::POS_END
517  );
518  }
519 
525  protected function getOnSelectOptionForAutoComplete($idInputName)
526  {
527  return 'js:function(event, ui){ jQuery("#' . $idInputName . '").val(ui.item["'. $this->resolveOnSelectionOptionAttributeNameForAutoComplete() .'"]).trigger("change");}';
528  }
529 
530  protected function resolveOnSelectionOptionAttributeNameForAutoComplete()
531  {
532  return 'id';
533  }
534 
539  protected function renderExtraHtmlContent()
540  {
541  $content = null;
542  if ($this->renderClearLink())
543  {
544  $content = $this->renderClearLinkContent();
545  }
546  return $content;
547  }
548 
549  protected function renderClearLink()
550  {
551  return false;
552  }
553 
554  protected function renderClearLinkContent()
555  {
556  $this->registerClearLinkScripts();
557  $htmlOptions = $this->resolveClearLinkHtmlOptions();
558  $icon = ZurmoHtml::tag('i', array('class' => 'icon-x'), '');
559  $link = ZurmoHtml::link($icon, '#', $htmlOptions);
560  return $link;
561  }
562 
563  protected function resolveClearLinkHtmlOptions()
564  {
565  $htmlOptions = array('id' => static::CLEAR_LINK_ID, 'class' => 'clear-select');
566  if (intval($this->getId()) <= 0)
567  {
568  $htmlOptions['style'] = 'display:none;';
569  }
570  return $htmlOptions;
571  }
572 
573  protected function registerClearLinkScripts()
574  {
575  $this->registerIdHiddenInputChangeScript();
576  $this->registerClearLinkClickScript();
577  }
578 
579  protected function registerIdHiddenInputChangeScript()
580  {
581  Yii::app()->clientScript->registerScript('idHiddenInputChangeScript', '
582  $("#'. $this->getIdForHiddenField() . '").unbind("change.idHiddenInputChangeScript")
583  .bind("change.idHiddenInputChangeScript", function(event)
584  {
585  if ($("#' . static::CLEAR_LINK_ID .'").is(":hidden"))
586  {
587  $("#' . static::CLEAR_LINK_ID .'").show();
588  }
589  });');
590  }
591 
592  protected function registerClearLinkClickScript()
593  {
594  Yii::app()->clientScript->registerScript('clearLinkClickScript', '
595  $("#' . static::CLEAR_LINK_ID . '").unbind("click.clearLinkClickScript")
596  .bind("click.clearLinkClickScript", function(event)
597  {
598  $("#'. $this->getIdForHiddenField() . '").val("");
599  $("#' . $this->getNameForTextField() . '").val("");
600  $(this).hide();
601  event.preventDefault();
602  });');
603  }
604  }
605 ?>
getOnSelectOptionForAutoComplete($idInputName)
static $moduleId
static $nonEditableActionType
registerScriptForAutoCompleteTextField()
static $autoCompleteActionId
showOnlyHiddenInputForEditable()
renderTextField($idInputName)
static $editableActionType
static $modalActionId
Generated on Tue Jul 14 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.