Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanMutableRelatedModels.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 
46  {
47  protected $modified = false;
48 
54  protected $deferredRelateBeans = array();
55 
61  protected $deferredUnrelateBeans = array();
62 
63  public function contains(RedBeanModel $model)
64  {
65  // Just adds self consistency checking.
66  $contains = parent::contains($model);
67  return $contains;
68  }
69 
73  public function add(RedBeanModel $model)
74  {
75  assert("\$model instanceof {$this->modelClassName}");
76  assert('!$this->contains($model)');
77  assert('($oldCount = $this->count()) > -1'); // To save the value only when asserts are enabled.
78  $bean = $model->getClassBean($this->modelClassName);
79  $this->deferredRelateBeans[] = $bean;
80  if (in_array($bean, $this->deferredUnrelateBeans))
81  {
82  self::array_remove_object($this->deferredUnrelateBeans, $bean);
83  }
84  $this->relatedBeansAndModels[] = $model;
85  $this->modified = true;
86  assert('$this->count() == $oldCount + 1');
87  assert('$this->contains($model)');
88  }
89 
93  public function remove(RedBeanModel $model)
94  {
95  assert("\$model instanceof {$this->modelClassName}");
96  assert('$this->contains($model)');
97  assert('($oldCount = $this->count()) > -1'); // To save the value only when asserts are enabled.
98  for ($i = 0; $i < $this->count(); $i++)
99  {
100  if ($this->getByIndex($i)->isSame($model))
101  {
102  $this->removeByIndex($i);
103  }
104  }
105  assert('$this->count() == $oldCount - 1');
106  assert('!$this->contains($model)');
107  }
108 
112  public function removeByIndex($i)
113  {
114  assert('is_int($i)');
115  assert('$i >= 0');
116  assert('$i < $this->count()');
117  assert('($oldCount = $this->count()) > -1'); // To save the value only when asserts are enabled.
118  $model = $this[$i];
119  $bean = $model->getClassBean($this->modelClassName);
120  $this->deferredUnrelateBeans[] = $bean;
121  if (in_array($bean, $this->deferredUnrelateBeans))
122  {
123  self::array_remove_object($this->deferredRelateBeans, $bean);
124  }
125  unset($this->relatedBeansAndModels[$i]);
126  $this->relatedBeansAndModels = array_values($this->relatedBeansAndModels);
127  $this->modified = true;
128  assert('$this->count() == $oldCount - 1');
129  }
130 
131  protected static function array_remove_object(&$array, $object)
132  {
133  // For anything other than an object this idiotic
134  // method is not needed, unset will do the job.
135  assert('gettype($object) == "object"');
136  // This probably calls for a comment. All I want to do is remove the element
137  // from the array that contains an $object. The comment I'd like to make is
138  // that there must be a less silly way than this surely!??
139  for ($i = 0; $i < count($array); $i++)
140  {
141  if ($array[$i] == $object)
142  {
143  unset($array[$i]);
144  $array = array_values($array);
145  break;
146  }
147  }
148  }
149 
153  public function removeAll()
154  {
155  $oldCount = $this->count();
156  while ($this->count() > 0)
157  {
158  $this->removeByIndex(0);
159  }
160  $this->modified = $oldCount > 0;
161  assert('$this->count() == 0');
162  }
163 
168  public function getErrors($attributeNameOrNames = null)
169  {
170  $allErrors = array();
171  for ($i = 0; $i < $this->count(); $i++)
172  {
173  $errors = $this[$i]->getErrors($attributeNameOrNames);
174  if (count($errors) > 0)
175  {
176  $allErrors[$i] = $errors;
177  }
178  }
179  return $allErrors;
180  }
181 
182  public function save($runValidation = true)
183  {
184  $saved = parent::save($runValidation);
185  if ($saved)
186  {
187  $this->modified = false;
188  }
189  return $saved;
190  }
191 
197  public function isModified()
198  {
199  return $this->modified || parent::isModified();
200  }
201  }
202 ?>
Generated on Sat Jul 4 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.