All Data Structures Functions Variables Pages
DerivedAttributeMetadata.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  abstract class DerivedAttributeMetadata extends RedBeanModel
42  {
47  public static function getByNameAndModelClassName($name, $modelClassName)
48  {
49  assert('is_string($name)');
50  assert('$name != ""');
51  assert('is_string($modelClassName)');
52  assert('$modelClassName != ""');
53  assert('get_called_class() != "DerivedAttributeMetadata"');
54  $derivedAttirbuteMetadataTableName = DerivedAttributeMetadata::getTableName();
55  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
56  $joinTablesAdapter->addFromTableAndGetAliasName($derivedAttirbuteMetadataTableName,
57  "{$derivedAttirbuteMetadataTableName}_id");
58  $where = "$derivedAttirbuteMetadataTableName.name = '$name' and ";
59  $where .= "$derivedAttirbuteMetadataTableName.modelclassname = '$modelClassName'";
60  $models = static::getSubset($joinTablesAdapter, null, null, $where);
61  if (count($models) == 0 || count($models) > 1)
62  {
63  throw new NotFoundException();
64  }
65  return $models[0];
66  }
67 
72  public static function getAllByModelClassName($modelClassName)
73  {
74  assert('$modelClassName != ""');
75  $derivedAttirbuteMetadataTableName = DerivedAttributeMetadata::getTableName();
76  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
77  $joinTablesAdapter->addFromTableAndGetAliasName($derivedAttirbuteMetadataTableName,
78  "{$derivedAttirbuteMetadataTableName}_id");
79  $where = "$derivedAttirbuteMetadataTableName.modelclassname = '$modelClassName'";
80  $models = static::getSubset($joinTablesAdapter, null, null, $where);
81  if (count($models) == 0)
82  {
83  return array();
84  }
85  return $models;
86  }
87 
88  public static function getDefaultMetadata()
89  {
90  $metadata = parent::getDefaultMetadata();
91  $metadata[__CLASS__] = array(
92  'members' => array(
93  'name',
94  'modelClassName',
95  'serializedMetadata',
96  ),
97  'rules' => array(
98  array('name', 'required'),
99  array('name', 'validateUniqueNameByModelClassName'),
100  array('name', 'type', 'type' => 'string'),
101  array('name', 'length', 'max' => 64),
102  array('name', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/', // Not Coding Standard
103  'message' => Zurmo::t('Core', 'Name must not contain spaces or special characters'),
104  ),
105  array('name', 'match', 'pattern' => '/^[a-z]/', // Not Coding Standard
106  'message' => Zurmo::t('Core', 'First character must be a lower case letter'),
107  ),
108  array('modelClassName', 'required'),
109  array('modelClassName', 'match', 'pattern' => '/[A-Z]([a-zA-Z]*[a-z]|[a-z]?)/',
110  'message' => 'Model Class Name must be PascalCase.'),
111  array('modelClassName', 'type', 'type' => 'string'),
112  array('modelClassName', 'length', 'max' => 64),
113  array('serializedMetadata', 'required'),
114  array('serializedMetadata', 'type', 'type' => 'string'),
115  array('serializedMetadata', 'validateSerializedMetadata', 'on' => 'nonAutoBuild'),
116  )
117  );
118  return $metadata;
119  }
120 
121  public function validateSerializedMetadata($attribute, $params)
122  {
123  if ($this->$attribute != null)
124  {
125  $unserializedData = unserialize($this->serializedMetadata);
126  if (!isset($unserializedData['attributeLabels']))
127  {
128  $message = Zurmo::t('Core', 'Missing the attribute labels.');
129  $this->addError('name', $message);
130  }
131  }
132  }
133 
134  public function validateUniqueNameByModelClassName($attribute, $params)
135  {
136  assert('$attribute == "name"');
137  if ($this->$attribute != null)
138  {
140  $sql = 'select id from ' . $tableName . " where name = '{$this->$attribute}' and ";
141  $sql .= "modelclassname = '" . $this->modelClassName . "'";
142  $rows = ZurmoRedBean::getAll($sql);
143  if (count($rows) == 0 || count($rows) == 1 && $rows[0]['id'] == $this->getClassId('DerivedAttributeMetadata'))
144  {
145  return;
146  }
147  $message = Zurmo::t('Core', '{attribute} "{value}" is already in use.',
148  array('{attribute}' => $attribute, '{name}' => $this->$attribute));
149  $this->addError('name', $message);
150  }
151  }
152 
153  public function getLabelByLanguage($language)
154  {
155  assert('is_string($language)');
156  $unserializedData = unserialize($this->serializedMetadata);
157  if (isset($unserializedData['attributeLabels']) && isset($unserializedData['attributeLabels'][$language]))
158  {
159  return $unserializedData['attributeLabels'][$language];
160  }
161  return $this->name;
162  }
163  }
164 ?>
getClassId($modelClassName)
static getAllByModelClassName($modelClassName)
addError($attributeName, $errorMessage)
static getTableName()
static getByNameAndModelClassName($name, $modelClassName)
Generated on Tue Jun 2 2020 07:10:26