Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanOneToManyRelatedModels.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  $basePath = Yii::app()->getBasePath();
38  require_once("$basePath/../../redbean/rb.php");
39 
45  {
49  protected $relatedModel;
50 
54  protected $relatedModelClassName;
55 
59  protected $relatedAttributeName;
60 
66  protected $deferredUnrelatedModels = array();
67 
71  protected $owns;
72 
79  protected $linkType;
80 
84  protected $linkName;
85 
90  protected $opposingRelationName;
91 
92  protected $opposingRelationNameRetrieved = false;
93 
99  public function __construct(RedBeanModel $relatedModel, RedBean_OODBBean $bean, $modelClassName, $relatedModelClassName,
100  $relatedAttributeName,
101  $owns, $linkType, $linkName = null)
102  {
103  assert('is_string($modelClassName)');
104  assert('is_string($relatedModelClassName)');
105  assert('is_string($relatedAttributeName)');
106  assert('$modelClassName != ""');
107  assert('$relatedModelClassName != ""');
108  assert('$relatedAttributeName != ""');
109  assert('is_bool($owns)');
110  assert('is_int($linkType)');
111  assert('is_string($linkName) || $linkName == null');
112  assert('($linkType == RedBeanModel::LINK_TYPE_ASSUMPTIVE && $linkName == null) ||
113  ($linkType != RedBeanModel::LINK_TYPE_ASSUMPTIVE && $linkName != null)');
114  $this->rewind();
115  $this->relatedModel = $relatedModel;
116  $this->modelClassName = $modelClassName;
117  $this->relatedModelClassName = $relatedModelClassName;
118  $this->relatedAttributeName = $relatedAttributeName;
119  $this->owns = $owns;
120  $this->linkType = $linkType;
121  $this->linkName = $linkName;
122  $this->constructRelatedBeansAndModels($modelClassName, $bean);
123  }
124 
125  public function getModelClassName()
126  {
127  return $this->modelClassName;
128  }
129 
135  private function constructRelatedBeansAndModels($modelClassName, $sqlOrBean = '')
136  {
137  assert('is_string($sqlOrBean) || $sqlOrBean instanceof RedBean_OODBBean');
138  $tableName = $modelClassName::getTableName();
139  if (is_string($sqlOrBean))
140  {
141  $this->relatedBeansAndModels = array_values($beans = ZurmoRedBean::find($tableName, $sqlOrBean));
142  }
143  else
144  {
145  assert('$sqlOrBean instanceof RedBean_OODBBean');
146  $this->bean = $sqlOrBean;
147  try
148  {
149  if ($this->bean->id > 0)
150  {
151  if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC)
152  {
153  $value = array();
154  $values['id'] = $this->bean->id;
155  $values['type'] = $this->bean->getMeta('type');
156 
157  $this->relatedBeansAndModels = array_values(ZurmoRedBean::find( $tableName,
158  strtolower($this->linkName) . '_id = :id AND ' .
159  strtolower($this->linkName) . '_type = :type',
160  $values));
161  }
162  else
163  {
164  $relatedIds = ZurmoRedBeanLinkManager::getKeys($this->bean, $tableName,
165  $this->resolveLinkNameForCasing());
166  $this->relatedBeansAndModels = array_values(ZurmoRedBean::batch($tableName, $relatedIds));
167  }
168  }
169  else
170  {
171  $this->relatedBeansAndModels = array();
172  }
173  }
174  catch (RedBean_Exception_SQL $e)
175  {
176  // SQLSTATE[42S02]: Base table or view not found...
177  // SQLSTATE[42S22]: Column not found...
178  if (!in_array($e->getSQLState(), array('42S02', '42S22')))
179  {
180  throw $e;
181  }
182  $this->relatedBeansAndModels = array();
183  }
184  }
185  }
186 
187  public function save($runValidation = true)
188  {
189  if (!parent::save($runValidation))
190  {
191  return false;
192  }
193  foreach ($this->deferredRelateBeans as $bean)
194  {
195  if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC)
196  {
197  if ($this->bean->id == null)
198  {
199  ZurmoRedBean::store($this->bean);
200  }
201  $polyIdFieldName = strtolower($this->linkName) . '_id';
202  $polyTypeFieldName = strtolower($this->linkName) . '_type';
203  $bean->$polyTypeFieldName = $this->bean->getMeta('type');
204  $bean->$polyIdFieldName = $this->bean->id;
205  }
206  else
207  {
208  ZurmoRedBeanLinkManager::link($bean, $this->bean, $this->resolveLinkNameForCasing());
209  }
210  ZurmoRedBean::store($bean);
211  }
212  $this->deferredRelateBeans = array();
213  $relatedModelClassName = $this->relatedModelClassName;
214  $tableName = $relatedModelClassName::getTableName();
215  foreach ($this->deferredUnrelateBeans as $bean)
216  {
217  if (!$this->owns)
218  {
219  if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC)
220  {
221  throw new NotSupportedException("Polymorphic relations can not be NOT_OWNED");
222  }
223  ZurmoRedBeanLinkManager::breakLink($bean, $tableName, $this->resolveLinkNameForCasing());
224  ZurmoRedBean::store($bean);
225  }
226  else
227  {
228  ZurmoRedBean::trash($bean);
229  }
230  }
231  $this->deferredUnrelatedBeans = array();
232  foreach ($this->deferredUnrelatedModels as $model)
233  {
234  $event = new CModelEvent($model);
235  $model->onRedBeanOneToManyRelatedModelsChange($event);
236  }
237  $this->deferredUnrelatedModels = array();
238  return true;
239  }
240 
241  protected function resolveLinkNameForCasing()
242  {
243  if ($this->linkName != null)
244  {
245  return strtolower($this->linkName);
246  }
247  }
248 
252  public function getStringifiedData()
253  {
254  $data = null;
255  foreach ($this as $containedModel)
256  {
257  if ($containedModel->id > 0)
258  {
259  $data[] = strval($containedModel);
260  }
261  }
262  return $data;
263  }
264 
271  public function add(RedBeanModel $model)
272  {
273  parent::add($model);
274  if (get_class($model) != get_class($this->relatedModel) &&
275  (null != $opposingRelationName = $this->getOpposingRelationName($model)))
276  {
277  $model->$opposingRelationName = $this->relatedModel;
278  }
279  }
280 
287  public function removeByIndex($i)
288  {
289  $model = $this->getByIndex($i);
290  parent::removeByIndex($i);
291  if ((get_class($model) != get_class($this->relatedModel) &&
292  null != $opposingRelationName = $this->getOpposingRelationName($model))
293  )
294  {
295  $model->$opposingRelationName = null;
296  $this->deferredUnrelatedModels[] = $model;
297  }
298  }
299 
300  protected function getOpposingRelationName($model)
301  {
302  if (!$this->opposingRelationNameRetrieved)
303  {
304  if (null !== $opposingRelationName = RedBeanModel::
305  getHasManyOpposingRelationName($model, $this->relatedModelClassName,
306  $this->relatedAttributeName))
307  {
308  $this->opposingRelationName = $opposingRelationName;
309  }
310  $this->opposingRelationNameRetrieved = true;
311  }
312  return $this->opposingRelationName;
313  }
314  }
315 ?>
Generated on Wed Jul 1 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.