All Data Structures Functions Variables Pages
AutoCompleteTextElement.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  // TODO: @Shoaibi/@Jason: Low: This should be refactored and used everywhere instead of manually creating clip.
38  abstract class AutoCompleteTextElement extends TextElement
39  {
40  abstract protected function getWidgetValue();
41 
42  abstract protected function getSource();
43 
44  abstract protected function getOptions();
45 
50  protected function renderControlEditable()
51  {
52  $cClipWidget = new CClipWidget();
53  $clipId = $this->getWidgetClipName();
54  $cClipWidget->beginClip($clipId);
55  $cClipWidget->widget('zii.widgets.jui.CJuiAutoComplete', array(
56  'name' => $this->getWidgetName(),
57  'id' => $this->getWidgetId(),
58  'value' => $this->getWidgetValue(),
59  'source' => $this->getSource(),
60  'options' => $this->getOptions(),
61  'htmlOptions' => $this->getHtmlOptions(),
62 
63  ));
64  $cClipWidget->endClip();
65  $content = $cClipWidget->getController()->clips[$clipId];
66  $content .= $this->renderSelectLink();
67  return $content;
68  }
69 
70  protected function getWidgetId()
71  {
72  return $this->getEditableInputId();
73  }
74 
75  protected function getWidgetClipName()
76  {
77  return get_class($this);
78  }
79 
80  protected function getWidgetName()
81  {
82  return $this->getEditableInputName();
83  }
84 
85  protected function renderSelectLink()
86  {
87  if (!$this->shouldRenderSelectLink())
88  {
89  return null;
90  }
91  $cs = Yii::app()->getClientScript();
92  $cs->registerCoreScript('bbq');
93  $cs->registerScriptFile(
94  Yii::app()->getAssetManager()->publish(
95  Yii::getPathOfAlias('application.core.elements.assets')
96  ) . '/Modal.js',
97  CClientScript::POS_END
98  );
99  $this->registerSelectLinkScripts();
100  $content = ZurmoHtml::openTag('div', array('class' => 'has-model-select'));
101  $content .= ZurmoHtml::hiddenField($this->getNameForHiddenSelectLinkField());
102  $content .= ZurmoHtml::ajaxLink('<span class="model-select-icon"></span>',
103  Yii::app()->createUrl($this->getSourceUrlForSelectLink(), $this->getSelectLinkUrlParams()),
104  $this->resolveAjaxOptionsForSelectingModel(),
105  array('id' => $this->getWidgetId() . '-select-link')
106  );
107  $content .= ZurmoHtml::closeTag('div');
108  return $content;
109  }
110 
111  protected function getSourceUrlForSelectLink()
112  {
113  throw new NotImplementedException();
114  }
115 
116  protected function getSelectLinkUrlParams()
117  {
118  return array(
119  'modalTransferInformation' => $this->getModalTransferInformation(),
120  );
121  }
122 
123  protected function getModalTransferInformation()
124  {
125  return array(
126  'sourceIdFieldId' => $this->getIdForHiddenSelectLinkField(),
127  'sourceNameFieldId' => $this->getWidgetId(),
128  'modalId' => $this->getModalContainerId(),
129  );
130  }
131 
132  protected function getIdForHiddenSelectLinkField()
133  {
134  return ZurmoHtml::getIdByName($this->getNameForHiddenSelectLinkField());
135  }
136 
137  protected function getNameForHiddenSelectLinkField()
138  {
139  return $this->getEditableInputName(null, 'transfer');
140  }
141 
142  protected function resolveAjaxOptionsForSelectingModel()
143  {
144  $title = $this->getModalTitleForSelectingModel();
145  return ModalView::getAjaxOptionsForModalLink($title, $this->getModalContainerId());
146  }
147 
148  protected function getModalContainerId()
149  {
150  return 'modalContainer';
151  }
152 
153  protected function getModalTitleForSelectingModel()
154  {
155  throw new NotImplementedException();
156  }
157 
158  protected function registerSelectLinkScripts()
159  {
160  $scriptName = $this->getWidgetId() . '-transfer-script';
161  if (Yii::app()->clientScript->isScriptRegistered($scriptName))
162  {
163  return;
164  }
165  else
166  {
167  $selectLinkId = $this->getWidgetId() . '-select-link';
168  // Begin Not Coding Standard
169  Yii::app()->clientScript->registerScript($scriptName, "
170  $('#{$selectLinkId}').off();
171  $('#{$this->getIdForHiddenSelectLinkField()}').change(function(event){
172  {$this->getAfterChangeSelectIdScript()}
173  });
174  ");
175  // End Not Coding Standard
176  }
177  }
178 
179  protected function getAfterChangeSelectIdScript()
180  {
181  throw new NotImplementedException();
182  }
183 
184  protected function shouldRenderSelectLink()
185  {
186  if (isset($this->params['shouldRenderSelectLink']))
187  {
188  return $this->params['shouldRenderSelectLink'];
189  }
190  return $this->shouldRenderSelectLinkDefault();
191  }
192 
193  protected function shouldRenderSelectLinkDefault()
194  {
195  return false;
196  }
197  }
198 ?>
static ajaxLink($text, $url, $ajaxOptions=array(), $htmlOptions=array())
Definition: ZurmoHtml.php:328
getEditableInputName($attributeName=null, $relationAttributeName=null)
Definition: Element.php:303
getEditableInputId($attributeName=null, $relationAttributeName=null)
Definition: Element.php:277
Generated on Tue Apr 7 2020 07:10:43