Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
FileModel.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  class FileModel extends Item
38  {
39  protected $isAudited = false;
40 
41  public function __toString()
42  {
43  if (trim($this->name) == '')
44  {
45  return Zurmo::t('Core', '(Unnamed)');
46  }
47  return $this->name;
48  }
49 
50  public static function getDefaultMetadata()
51  {
52  $metadata = parent::getDefaultMetadata();
53  $metadata[__CLASS__] = array(
54  'members' => array(
55  'name',
56  'size',
57  'type',
58  ),
59  'relations' => array(
60  'fileContent' => array(static::HAS_ONE, 'FileContent', static::NOT_OWNED),
61  ),
62  'rules' => array(
63  array('fileContent', 'required'),
64  array('name', 'required'),
65  array('name', 'type', 'type' => 'string'),
66  array('name', 'length', 'min' => 1, 'max' => 100),
67  array('size', 'required'),
68  array('size', 'type', 'type' => 'integer'),
69  array('type', 'required'),
70  array('type', 'type', 'type' => 'string'),
71  array('type', 'length', 'min' => 1, 'max' => 128),
72  array('type', 'fileTypeValidator'),
73 
74  ),
75  'defaultSortAttribute' => 'name',
76  'noAudit' => array(
77  'fileContent',
78  ),
79  );
80  return $metadata;
81  }
82 
83  public function fileTypeValidator($attribute, $params)
84  {
85  return true;
86  }
87 
88  public static function isTypeDeletable()
89  {
90  return true;
91  }
92 
93  protected static function translatedAttributeLabels($language)
94  {
95  return array_merge(parent::translatedAttributeLabels($language),
96  array(
97  'name' => Zurmo::t('Core', 'Name', array(), null, $language),
98  'size' => Zurmo::t('Core', 'Size', array(), null, $language),
99  'type' => Zurmo::t('Core', 'Type', array(), null, $language),
100  )
101  );
102  }
103 
104  protected function beforeSave()
105  {
106  if (parent::beforeSave())
107  {
108  if ($this->fileContent !== null)
109  {
110  return $this->fileContent->save();
111  }
112  return true;
113  }
114  else
115  {
116  return false;
117  }
118  }
119 
120  protected function deleteRelatedFileContentIfNotRelatedToAnyOtherFileModel()
121  {
122  $searchAttributeData = array();
123  $searchAttributeData['clauses'] = array(
124  1 => array(
125  'attributeName' => 'fileContent',
126  'relatedModelData' => array(
127  'attributeName' => 'id',
128  'operatorType' => 'equals',
129  'value' => $this->fileContent->id,
130  )
131  ),
132  );
133  $searchAttributeData['structure'] = '1';
134  $class = get_class($this);
135  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($class);
136  $where = RedBeanModelDataProvider::makeWhere($class, $searchAttributeData, $joinTablesAdapter);
137  if (static::getCount($joinTablesAdapter, $where, $class) == 1)
138  {
139  return $this->fileContent->delete();
140  }
141  return true;
142  }
143 
144  protected function deleteOwnedRelatedModels($modelClassName)
145  {
146  // THIS IS A HACK. We want to save space by not duplicating fileContent's blob so we have to live with this.
147 
148  // This is to fix the dangling FileContent that remain there when deleting a model that owns files
149  // Example: ModelWithAttachmentTest.testModelWithAttachmentTestItem
150 
151  // we use $this->deleteOwnedRelatedModels so when deleting FileModel directly this gets
152  // invoked anyway under unrestrictedDelete, no need to call same function in beforeDelete of FileModel
153 
154  // When deleting a model that owns Files, this gets invoked as a result of RedbeanModel.2245
155  // We can't change RedBeanModel.2238 and RedBeanModel.2245 to delete() because that would
156  // throw exception, by that point we are deleting an OwnedModel instance which can't be deleted
157  // from outside and hence this fix.
158  if (get_class($this) == $modelClassName)
159  {
160  // get rid of fileContent that belong only to this model before going ahead and trashing it.
161  if (!$this->deleteRelatedFileContentIfNotRelatedToAnyOtherFileModel())
162  {
163  throw new FailedToDeleteModelException("Unable to delete related FileContent");
164  }
165  }
166  parent::deleteOwnedRelatedModels($modelClassName);
167  }
168  }
169 ?>
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
Definition: Item.php:37
Generated on Tue Jul 14 2020 07:10:37
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.