Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelToExportAdapter.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 
41  {
47  const DELIMITER = ' - ';
48 
49  public static function getLabelFromTwoAttributeStrings($stringOne, $stringTwo)
50  {
51  return $stringOne . ModelToExportAdapter::DELIMITER . $stringTwo;
52  }
53 
59  public function getData()
60  {
61  $data = array();
62  $data[] = $this->model->id;
63  $retrievableAttributes = static::resolveExportableAttributesByModel($this->model);
64  foreach ($this->model->getAttributes($retrievableAttributes) as $attributeName => $notUsed)
65  {
66  if ((null !== $adapterClassName = $this->getRedBeanModelAttributeValueToExportValueAdapterClassName($attributeName)) &&
67  !($this->model->isRelation($attributeName) && $this->model->getRelationType($attributeName) !=
69  {
70  //Non-relation attribute
71  $adapter = new $adapterClassName($this->model, $attributeName);
72  $adapter->resolveData($data);
73  }
74  elseif ($this->isHasOneVariationOwnedRelation($attributeName))
75  {
76  //Owned relation attribute
77  if ($this->model->{$attributeName}->id > 0)
78  {
79  $util = new ModelToExportAdapter($this->model->{$attributeName});
80  $relatedData = $util->getData();
81  foreach ($relatedData as $relatedDataAttribute => $relatedDataValue)
82  {
83  if (strtolower($relatedDataAttribute) != 'id')
84  {
85  $data[] = $relatedDataValue;
86  }
87  }
88  }
89  else
90  {
91  $data = array_merge($data, $this->getAllAttributesDataAsNull($attributeName));
92  }
93  }
94  //We do not want to list properties from CustomFieldData objects
95  //This is also the case for related models, not only for custom fields
96  elseif ($this->model->isRelation($attributeName) &&
97  $this->isHasOneVariationNotOwnedRelation($attributeName))
98  {
99  //Non-owned relation
100  if ($this->model->{$attributeName}->id > 0)
101  {
102  $data[] = strval($this->model->{$attributeName});
103  }
104  else
105  {
106  $data[] = null;
107  }
108  }
109  }
110  return $data;
111  }
112 
117  public function getHeaderData()
118  {
119  $data = array();
120  $data[] = $this->resolveIdLabelToTitleCaseForExport($this->model->getAttributeLabel('id'));
121  $retrievableAttributes = static::resolveExportableAttributesByModel($this->model);
122  foreach ($this->model->getAttributes($retrievableAttributes) as $attributeName => $notUsed)
123  {
124  if ((null !== $adapterClassName = $this->getRedBeanModelAttributeValueToExportValueAdapterClassName($attributeName)) &&
125  !($this->model->isRelation($attributeName) && $this->model->getRelationType($attributeName) !=
127  {
128  //Non-relation attribute
129  $adapter = new $adapterClassName($this->model, $attributeName);
130  $adapter->resolveHeaderData($data);
131  }
132  elseif ($this->isHasOneVariationOwnedRelation($attributeName))
133  {
134  //Owned relation attribute
135  if ($this->model->{$attributeName}->id > 0)
136  {
137  $util = new ModelToExportAdapter($this->model->{$attributeName});
138  $relatedData = $util->getData();
139  foreach ($relatedData as $relatedDataAttribute => $notUsed)
140  {
141  if (strtolower($relatedDataAttribute) != 'id')
142  {
143  $exportAttributeName = static::getLabelFromTwoAttributeStrings(
144  $this->model->getAttributeLabel($attributeName), $relatedDataAttribute);
145  $data[] = $exportAttributeName;
146  }
147  }
148  }
149  else
150  {
151  $data = array_merge($data, $this->getAllAtttributesDataAsLabels($attributeName));
152  }
153  }
154  //We do not want to list properties from CustomFieldData objects
155  //This is also the case for related models, not only for custom fields
156  elseif ($this->model->isRelation($attributeName) &&
157  $this->isHasOneVariationNotOwnedRelation($attributeName))
158  {
159  //Non-owned relation
160  if ($this->model->{$attributeName}->id > 0)
161  {
162  $label = static::getLabelFromTwoAttributeStrings(
163  $this->model->getAttributeLabel($attributeName), Zurmo::t('Core', 'Name'));
164  $data[] = $label;
165  }
166  else
167  {
168  $data[] = $this->model->getAttributeLabel($attributeName);
169  }
170  }
171  }
172  return $data;
173  }
174 
180  protected static function resolveExportableAttributesByModel($model)
181  {
182  $retrievableAttributeNames = array();
183  $metadata = $model->getMetadata();
184  foreach ($model->attributeNames() as $name)
185  {
186  if (isset($metadata[get_class($model)]['noExport']) && in_array($name, $metadata[get_class($model)]['noExport']))
187  {
188  continue;
189  }
190  try
191  {
192  $value = $model->{$name};
193  $retrievableAttributeNames[] = $name;
194  }
195  catch (Exception $e)
196  {
197  }
198  }
199  return $retrievableAttributeNames;
200  }
201 
207  {
208  assert('is_string($attributeName)');
209  $type = ModelAttributeToMixedArrayTypeUtil::getType($this->model, $attributeName);
210  $adapterClassName = $type . 'RedBeanModelAttributeValueToExportValueAdapter';
211  if ($type != null && @class_exists($adapterClassName))
212  {
213  return $adapterClassName;
214  }
215  }
216 
221  protected function isHasOneVariationOwnedRelation($attributeName)
222  {
223  assert('is_string($attributeName)');
224  if ($this->model->isOwnedRelation($attributeName) &&
225  ($this->model->getRelationType($attributeName) == RedBeanModel::HAS_ONE ||
226  $this->model->getRelationType($attributeName) == RedBeanModel::HAS_MANY_BELONGS_TO))
227  {
228  return true;
229  }
230  return false;
231  }
232 
237  protected function isHasOneVariationNotOwnedRelation($attributeName)
238  {
239  assert('is_string($attributeName)');
240  if (!$this->model->isOwnedRelation($attributeName) &&
241  ($this->model->getRelationType($attributeName) == RedBeanModel::HAS_ONE ||
242  $this->model->getRelationType($attributeName) == RedBeanModel::HAS_MANY_BELONGS_TO))
243  {
244  return true;
245  }
246  return false;
247  }
248 
249  protected function resolveIdLabelToTitleCaseForExport($id)
250  {
251  return mb_convert_case($id, MB_CASE_TITLE, "UTF-8");
252  }
253 
258  protected function getAllAttributesDataAsNull($attributeName)
259  {
260  $data = array();
261  $metadata = $this->model->{$attributeName}->getMetadata();
262  foreach ($metadata[get_class($this->model->{$attributeName})]['members'] as $memberName)
263  {
264  $data[] = null;
265  }
266  return $data;
267  }
268 
273  protected function getAllAtttributesDataAsLabels($attributeName)
274  {
275  $data = array();
276  $metadata = $this->model->{$attributeName}->getMetadata();
277  foreach ($metadata[get_class($this->model->{$attributeName})]['members'] as $memberName)
278  {
279  $label = static::getLabelFromTwoAttributeStrings(
280  $this->model->getAttributeLabel($attributeName),
281  $this->model->{$attributeName}->getAttributeLabel($memberName));
282  $data[] = $label;
283  }
284  return $data;
285  }
286  }
287 ?>
const HAS_ONE
Definition: BeanModel.php:64
getAllAtttributesDataAsLabels($attributeName)
isHasOneVariationOwnedRelation($attributeName)
isHasOneVariationNotOwnedRelation($attributeName)
const HAS_MANY_BELONGS_TO
Definition: BeanModel.php:58
getAllAttributesDataAsNull($attributeName)
getRedBeanModelAttributeValueToExportValueAdapterClassName($attributeName)
static resolveExportableAttributesByModel($model)
Generated on Wed Sep 30 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.