All Data Structures Functions Variables Pages
ZurmoActiveForm.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 
37  class ZurmoActiveForm extends CActiveForm
38  {
44  public $modelClassNameForError;
45 
51  public $bindAsLive = false;
52 
53  public static function makeErrorsSummaryId($id)
54  {
55  assert('is_string($id)');
56  return $id . '_es_';
57  }
58 
69  public static function makeErrorsDataAndResolveForOwnedModelAttributes($model)
70  {
71  assert('$model instanceof RedBeanModel || $model instanceof CModel');
72  $errorData = array();
73  foreach ($model->getErrors() as $attribute => $errors)
74  {
75  if ($model::isRelation($attribute) && $model::isOwnedRelation($attribute) &&
76  in_array($model::getRelationModelClassName($attribute), array('Address', 'Email', 'CurrencyValue')))
77  {
78  foreach ($errors as $relatedAttribute => $relatedErrors)
79  {
80  $errorData[ZurmoHtml::activeId($model, $attribute . '[' . $relatedAttribute . ']')] = $relatedErrors;
81  }
82  }
83  else
84  {
85  $errorData[ZurmoHtml::activeId($model, $attribute)] = $errors;
86  }
87  }
88  return $errorData;
89  }
90 
91  public static function makeErrorsDataAndResolveForOwnedModelAttributesAndHasOneRelations($model)
92  {
93  assert('$model instanceof RedBeanModel || $model instanceof CModel');
94  $errorData = array();
95  foreach ($model->getErrors() as $attribute => $errors)
96  {
97  if ($model::isRelation($attribute) && $model::isOwnedRelation($attribute) &&
98  in_array($model::getRelationModelClassName($attribute), array('Address', 'Email', 'CurrencyValue')))
99  {
100  foreach ($errors as $relatedAttribute => $relatedErrors)
101  {
102  $errorData[ZurmoHtml::activeId($model, $attribute . '[' . $relatedAttribute . ']')] = $relatedErrors;
103  }
104  }
105  elseif ($model::isRelation($attribute) &&
106  in_array($model::getRelationModelClassName($attribute), array('Account', 'Contact', 'Opportunity', 'Project')))
107  {
108  $errorData[ZurmoHtml::activeId($model, $attribute)] = array($model->getError($attribute));
109  }
110  else
111  {
112  $errorData[ZurmoHtml::activeId($model, $attribute)] = $errors;
113  }
114  }
115  return $errorData;
116  }
117 
124  public function renderAddAttributeErrorSettingsScript($formId)
125  {
126  assert('is_string($formId)');
127  $attributes = $this->getAttributes();
128  $encodedErrorAttributes = CJSON::encode(array_values($attributes));
129  $script = "
130  var settings = $('#" . $formId . "').data('settings');
131  $.each(" . $encodedErrorAttributes . ", function(i)
132  {
133  var newId = this.id;
134  var alreadyInArray = false;
135  $.each(settings.attributes, function (i)
136  {
137  if (newId == this.id)
138  {
139  alreadyInArray = true;
140  }
141  });
142  if (alreadyInArray == false)
143  {
144  settings.attributes.push(this);
145  }
146  });
147  $('#" . $formId . "').data('settings', settings);
148  ";
149  Yii::app()->getClientScript()->registerScript('AddAttributeErrorSettingsScript' . $formId, $script);
150  }
151 
159  public function error($model, $attribute, $htmlOptions = array(), $enableAjaxValidation = true, $enableClientValidation = true, $id = null)
160  {
161  if (!$this->enableAjaxValidation)
162  {
163  $enableAjaxValidation = false;
164  }
165  if (!$this->enableClientValidation)
166  {
167  $enableClientValidation = false;
168  }
169  if (!isset($htmlOptions['class']))
170  {
171  $htmlOptions['class'] = $this->errorMessageCssClass;
172  }
173  if (!$enableAjaxValidation && !$enableClientValidation)
174  {
175  return CHtml::error($model, $attribute, $htmlOptions);
176  }
177  if ($id == null)
178  {
179  $id = $this->resolveId($model, $attribute);
180  }
181  $inputID = isset($htmlOptions['inputID']) ? $htmlOptions['inputID'] : $id;
182  unset($htmlOptions['inputID']);
183  if (!isset($htmlOptions['id']))
184  {
185  $htmlOptions['id'] = $inputID . '_em_';
186  }
187  $option = array(
188  'id' => $id,
189  'inputID' => $inputID,
190  'errorID' => $htmlOptions['id'],
191  'model' => $this->resolveModelClassNameForError($model),
192  'name' => $attribute,
193  'enableAjaxValidation' => $enableAjaxValidation,
194  'summary' => true,
195  );
196  $optionNames = array(
197  'validationDelay',
198  'validateOnChange',
199  'validateOnType',
200  'hideErrorMessage',
201  'inputContainer',
202  'errorCssClass',
203  'successCssClass',
204  'validatingCssClass',
205  'beforeValidateAttribute',
206  'afterValidateAttribute',
207  );
208  foreach ($optionNames as $name)
209  {
210  if (isset($htmlOptions[$name]))
211  {
212  $option[$name] = $htmlOptions[$name];
213  unset($htmlOptions[$name]);
214  }
215  }
216  if ($model instanceof CActiveRecord && !$model->isNewRecord)
217  {
218  $option['status'] = 1;
219  }
220  if ($enableClientValidation)
221  {
222  $validators = isset($htmlOptions['clientValidation']) ? array($htmlOptions['clientValidation']) : array();
223  $attributeName = $attribute;
224  if (($pos = strrpos($attribute, ']')) !== false && $pos !== strlen($attribute) - 1) // e.g. [a]name
225  {
226  $attributeName = substr($attribute, $pos + 1);
227  }
228  foreach ($model->getValidators($attributeName) as $validator)
229  {
230  if ($validator->enableClientValidation)
231  {
232  if (($js = $validator->clientValidateAttribute($model, $attributeName)) != '')
233  {
234  $validators[] = $js;
235  }
236  }
237  }
238  if ($validators !== array())
239  {
240  $option['clientValidation'] = new CJavaScriptExpression("function(value, messages, attribute) {\n" .
241  implode("\n", $validators) . "\n}");
242  }
243  }
244  $html = CHtml::error($model, $attribute, $htmlOptions);
245  if ($html === '')
246  {
247  if (isset($htmlOptions['style']))
248  {
249  $htmlOptions['style'] = rtrim($htmlOptions['style'], ';') . ';display:none';
250  }
251  else
252  {
253  $htmlOptions['style'] = 'display:none';
254  }
255  $html = CHtml::tag('div', $htmlOptions, '');
256  }
257  $this->attributes[$inputID] = $option;
258  return $html;
259  }
260 
266  public function errorSummary($models, $header = null, $footer = null, $htmlOptions = array())
267  {
268  if (!$this->enableAjaxValidation && !$this->enableClientValidation)
269  {
270  return ZurmoHtml::errorSummary($models, $header, $footer, $htmlOptions);
271  }
272  if (!isset($htmlOptions['id']))
273  {
274  $htmlOptions['id'] = static::makeErrorsSummaryId($this->id);
275  }
276  $html = ZurmoHtml::errorSummary($models, $header, $footer, $htmlOptions);
277  if ($html === '')
278  {
279  if ($header === null)
280  {
281  $header = '<p>' . Zurmo::t('yii', 'Please fix the following input errors:') . '</p>';
282  }
283  if (!isset($htmlOptions['class']))
284  {
285  $htmlOptions['class'] = ZurmoHtml::$errorSummaryCss;
286  }
287  if (isset($htmlOptions['style']))
288  {
289  $htmlOptions['style'] = rtrim($htmlOptions['style'], ';') . ';display:none';
290  }
291  else
292  {
293  $htmlOptions['style'] = 'display:none';
294  }
295  $html = ZurmoHtml::tag('div', $htmlOptions, $header . "\n<ul><li>dummy</li></ul>" . $footer);
296  }
297 
298  $this->summaryID = $htmlOptions['id'];
299  return $html;
300  }
301 
305  public function run()
306  {
307  if (is_array($this->focus))
308  {
309  $this->focus="#" . ZurmoHtml::activeId($this->focus[0], $this->focus[1]);
310  }
311  echo ZurmoHtml::endForm();
312  $cs = Yii::app()->clientScript;
313  $cs->registerScriptFile(
314  Yii::app()->getAssetManager()->publish(
315  Yii::getPathOfAlias('application.core.views.assets')
316  ) . '/FormUtils.js',
317  CClientScript::POS_END
318  );
319  if (!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))
320  {
321  if ($this->focus !== null)
322  {
323  $cs->registerCoreScript('jquery');
324  $cs->registerScript('CActiveForm#focus', "
325  if (!window.location.hash)
326  { $('" . $this->focus . "').focus(); }
327  ");
328  }
329  return;
330  }
331  $options = $this->clientOptions;
332  if (isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))
333  {
334  $options['validationUrl'] = ZurmoHtml::normalizeUrl($this->clientOptions['validationUrl']);
335  }
336 
337  $options['attributes'] = array_values($this->attributes);
338  if ($this->summaryID !== null)
339  {
340  $options['summaryID'] = $this->summaryID;
341  }
342  if ($this->focus !== null)
343  {
344  $options['focus'] = $this->focus;
345  }
346 
347  $options = CJavaScript::encode($options);
348  //Not registering via coreScript because it does not properly register when using ajax non-minified
349  //on home page myList config view. Needs a better solution
350  $cs->registerScriptFile(
351  Yii::app()->getAssetManager()->publish(
352  Yii::getPathOfAlias('system.web.js.source')
353  ) . '/jquery.yii.js',
354  CClientScript::POS_END
355  );
356  $cs->registerScriptFile(
357  Yii::app()->getAssetManager()->publish(
358  Yii::getPathOfAlias('system.web.js.source')
359  ) . '/jquery.yiiactiveform.js',
360  CClientScript::POS_END
361  );
362  $id = $this->id;
363  if ($this->bindAsLive)
364  {
365  $cs->registerScript(__CLASS__. '#' . $id, "\$('#$id').live('focus', function(e)
366  {
367  if ($(this).data('settings') == undefined)
368  {
369  $(this).yiiactiveform($options);
370  }
371  });
372  ");
373  }
374  else
375  {
376  $cs->registerScript(__CLASS__. '#' . $id, "\$('#$id').yiiactiveform($options);");
377  }
378  }
379 
380  public function getAttributes()
381  {
382  return $this->attributes;
383  }
384 
389  public function radioButtonList($model, $attribute, $data, $htmlOptions = array(), $dataSelectOptions = array(),
390  $dataHtmlOptions = array())
391  {
392  return ZurmoHtml::activeRadioButtonList($model, $attribute, $data, $htmlOptions, $dataSelectOptions,
393  $dataHtmlOptions);
394  }
395 
401  public function checkBox($model, $attribute, $htmlOptions = array())
402  {
403  return ZurmoHtml::activeCheckBox($model, $attribute, $htmlOptions);
404  }
405 
410  public function checkBoxList($model, $attribute, $data, $htmlOptions = array())
411  {
412  return ZurmoHtml::activeCheckBoxList($model, $attribute, $data, $htmlOptions);
413  }
414 
419  public function dropDownList($model, $attribute, $data, $htmlOptions = array())
420  {
421  return ZurmoHtml::activeDropDownList($model, $attribute, $data, $htmlOptions);
422  }
423 
424  protected function resolveId($model, $attribute)
425  {
426  return CHtml::activeId($model, $attribute);
427  }
428 
429  protected function resolveModelClassNameForError($model)
430  {
431  if ($this->modelClassNameForError != null)
432  {
433  return $this->modelClassNameForError;
434  }
435  return get_class($model);
436  }
437  }
438 ?>
static activeDropDownList($model, $attribute, $data, $htmlOptions=array())
Definition: ZurmoHtml.php:598
dropDownList($model, $attribute, $data, $htmlOptions=array())
checkBoxList($model, $attribute, $data, $htmlOptions=array())
renderAddAttributeErrorSettingsScript($formId)
static makeErrorsDataAndResolveForOwnedModelAttributes($model)
radioButtonList($model, $attribute, $data, $htmlOptions=array(), $dataSelectOptions=array(), $dataHtmlOptions=array())
errorSummary($models, $header=null, $footer=null, $htmlOptions=array())
static errorSummary($model, $header=null, $footer=null, $htmlOptions=array())
Definition: ZurmoHtml.php:57
error($model, $attribute, $htmlOptions=array(), $enableAjaxValidation=true, $enableClientValidation=true, $id=null)
checkBox($model, $attribute, $htmlOptions=array())
static activeCheckBoxList($model, $attribute, $data, $htmlOptions=array())
Definition: ZurmoHtml.php:493
static activeRadioButtonList($model, $attribute, $data, $htmlOptions=array(), $dataSelectOptions=array(), $dataHtmlOptions=array())
Definition: ZurmoHtml.php:126
Generated on Tue Feb 25 2020 07:10:29