All Data Structures Functions Variables Pages
ModelAutoCompleteUtil.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 
46  {
55  public static function getByPartialName($modelClassName, $partialName, $pageSize, $autoCompleteOptions = null)
56  {
57  assert('is_string($modelClassName)');
58  assert('is_string($partialName)');
59  assert('is_int($pageSize)');
60  if ($modelClassName == 'User')
61  {
62  return ModelAutoCompleteUtil::getUserResults($partialName, $pageSize, $autoCompleteOptions);
63  }
64  elseif ($modelClassName == 'Contact')
65  {
66  throw new NotSupportedException();
67  }
68  elseif ($modelClassName == 'Person')
69  {
70  throw new NotImplementedException();
71  }
72  else
73  {
74  return ModelAutoCompleteUtil::getGenericResults($modelClassName, $partialName, $pageSize, $autoCompleteOptions);
75  }
76  }
77 
86  public static function getGlobalSearchResultsByPartialTerm($partialTerm, $pageSize, User $user, $scopeData = null)
87  {
88  assert('is_string($partialTerm)');
89  assert('is_int($pageSize)');
90  assert('$user->id > 0');
91  assert('$scopeData == null || is_array($scopeData)');
92  $modelClassNamesAndSearchAttributeData = static::makeModelClassNamesAndSearchAttributeData($partialTerm, $user, $scopeData);
93  if (empty($modelClassNamesAndSearchAttributeData))
94  {
95  return array(static::makeNoResultsFoundResultsData());
96  }
97  $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData,
98  array('pagination' => array('pageSize' => $pageSize)));
99  $data = $dataProvider->getData();
100  if (empty($data))
101  {
102  return array(static::makeNoResultsFoundResultsData());
103  }
104  $autoCompleteResults = array();
105  foreach ($data as $model)
106  {
107  $autoCompleteResults[] = static::makeModelResultsData($model);
108  }
109  return $autoCompleteResults;
110  }
111 
119  public static function getCustomFieldDataByPartialName($customFieldDataName, $partialName)
120  {
121  assert('is_string($customFieldDataName)');
122  assert('is_string($partialName)');
123  $customFieldData = CustomFieldData::getByName($customFieldDataName);
124  $dataAndLabels = CustomFieldDataUtil::
125  getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language);
126  $autoCompleteResults = array();
127  foreach ($dataAndLabels as $data => $label)
128  {
129  if (stripos($label, $partialName) !== false)
130  {
131  $autoCompleteResults[] = array(
132  'id' => $data,
133  'name' => $label,
134  );
135  }
136  }
137  return $autoCompleteResults;
138  }
139 
140  protected static function makeNoResultsFoundResultsData()
141  {
142  return array('href' => '', 'label' => Zurmo::t('Core', 'No Results Found'), 'iconClass' => '');
143  }
144 
145  protected static function makeModelResultsData(RedBeanModel $model)
146  {
147  $moduleClassName = ModelStateUtil::resolveModuleClassNameByStateOfModel($model);
148  $route = Yii::app()->createUrl($moduleClassName::getDirectoryName()
149  . '/default/details/', array('id' => $model->id));
150  return array('href' => $route,
151  'label' => strval($model),
152  'iconClass' => 'autocomplete-icon-' . $moduleClassName);
153  }
154 
155  protected static function makeModelClassNamesAndSearchAttributeData($partialTerm, User $user, $scopeData)
156  {
157  assert('is_string($partialTerm)');
158  assert('$user->id > 0');
159  assert('$scopeData == null || is_array($scopeData)');
160  $modelClassNamesAndSearchAttributeData = array();
161  $modules = Module::getModuleObjects();
162  foreach ($modules as $module)
163  {
164  $globalSearchFormClassName = $module::getGlobalSearchFormClassName();
166  $globalSearchFormClassName != null &&
167  RightsUtil::canUserAccessModule(get_class($module), $user) &&
168  ($scopeData == null || in_array($module->getName(), $scopeData)))
169  {
170  $modelClassName = $module::getPrimaryModelName();
171  $searchAttributes = MixedTermSearchUtil::
173  $partialTerm);
174  if (!empty($searchAttributes))
175  {
176  $model = new $modelClassName(false);
177  assert('$model instanceof RedBeanModel');
178  $searchForm = new $globalSearchFormClassName($model);
179  assert('$searchForm instanceof SearchForm');
180  $metadataAdapter = new SearchDataProviderMetadataAdapter(
181  $searchForm, $user->id, $searchAttributes);
182  $metadata = $metadataAdapter->getAdaptedMetadata(false);
183  $stateMetadataAdapterClassName = $module::getStateMetadataAdapterClassName();
184  if ($stateMetadataAdapterClassName != null)
185  {
186  $stateMetadataAdapter = new $stateMetadataAdapterClassName($metadata);
187  $metadata = $stateMetadataAdapter->getAdaptedDataProviderMetadata();
188  }
189  $modelClassNamesAndSearchAttributeData[$globalSearchFormClassName] =
190  array($modelClassName => $metadata);
191  }
192  }
193  }
194  return $modelClassNamesAndSearchAttributeData;
195  }
196 
197  protected static function getGenericResults($modelClassName, $partialName, $pageSize, $autoCompleteOptions)
198  {
199  $autoCompleteResults = array();
200  $joinTablesAdapter = null;
201  static::sanitizeSearchTerm($partialName);
202  $where = "name like lower('{$partialName}%')";
203  static::handleAutoCompleteOptions($joinTablesAdapter, $where, $autoCompleteOptions);
204  $models = $modelClassName::getSubset($joinTablesAdapter, null, $pageSize, $where, 'name');
205  foreach ($models as $model)
206  {
207  $autoCompleteResults[] = array(
208  'id' => $model->id,
209  'value' => strval($model),
210  'label' => strval($model)
211  );
212  }
213  return $autoCompleteResults;
214  }
215 
216  protected static function getUserResults($partialName, $pageSize, $autoCompleteOptions = null)
217  {
218  $autoCompleteResults = array();
219  $users = UserSearch::getUsersByPartialFullName($partialName, $pageSize, $autoCompleteOptions);
220  foreach ($users as $user)
221  {
222  $autoCompleteResults[] = array(
223  'id' => $user->id,
224  'value' => strval($user),
225  'label' => strval($user) .' (' . $user->username . ')'
226  );
227  }
228  return $autoCompleteResults;
229  }
230  }
231 ?>
static resolveModuleClassNameByStateOfModel(RedBeanModel $model)
static resolveIfModuleShouldBeGloballySearched(Module $module)
static getDataIndexedByDataAndTranslatedLabelsByLanguage(CustomFieldData $customFieldData, $language)
static getGlobalSearchAttributeByModuleAndPartialTerm(Module $module, $partialTerm)
Definition: User.php:37
static getGlobalSearchResultsByPartialTerm($partialTerm, $pageSize, User $user, $scopeData=null)
static getModuleObjects()
Definition: Module.php:51
static getByName($name, $shouldCache=true)
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static getByPartialName($modelClassName, $partialName, $pageSize, $autoCompleteOptions=null)
static getUsersByPartialFullName($partialName, $pageSize, $autoCompleteOptions=null)
Definition: UserSearch.php:47
static getCustomFieldDataByPartialName($customFieldDataName, $partialName)
Generated on Fri Jun 5 2020 07:10:37