All Data Structures Functions Variables Pages
FileModelUtil.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 
42  {
49  public static function makeByFilePathAndName($filePath, $fileName)
50  {
51  assert('is_string($filePath) && $filePath !=""');
52  assert('is_string($fileName) && $fileName !=""');
53  $contents = file_get_contents($filePath);
54  if ($contents === false)
55  {
56  return false;
57  }
58  $fileContent = new FileContent();
59  $fileContent->content = $contents;
60  $file = new FileModel();
61  $file->fileContent = $fileContent;
62  $file->name = $fileName;
63  $file->type = ZurmoFileHelper::getMimeType($filePath);
64  $file->size = filesize($filePath);
65  if (!$file->save())
66  {
67  return false;
68  }
69  return $file;
70  }
71 
77  public static function makeByUploadedFile($uploadedFile)
78  {
79  assert('$uploadedFile instanceof CUploadedFile');
80  $fileContent = new FileContent();
81  $fileContent->content = file_get_contents($uploadedFile->getTempName());
82  $file = new FileModel();
83  $file->fileContent = $fileContent;
84  $file->name = $uploadedFile->getName();
85  $file->type = $uploadedFile->getType();
86  $file->size = $uploadedFile->getSize();
87  if (!$file->save())
88  {
89  throw new FailedFileUploadException(Zurmo::t('ZurmoModule', 'File failed to upload. The file is empty.'));
90  }
91  return $file;
92  }
93 
99  public static function resolveModelsHasManyFilesFromPost(& $model, $relationName, $postDataVariableName)
100  {
101  assert('$model instanceof RedBeanModel');
102  assert('$model::isRelation($relationName)');
103  $relationModelClassName = $model->getRelationModelClassName($relationName);
104  assert('$relationModelClassName == "FileModel" || is_subclass_of($relationModelClassName, "FileModel")');
105  if (isset($_POST[$postDataVariableName]))
106  {
107  $newFileModelsIndexedById = array();
108  foreach ($_POST[$postDataVariableName] as $notUsed => $fileModelId)
109  {
110  $fileModel = $relationModelClassName::getById((int)$fileModelId);
111  $newFileModelsIndexedById[$fileModel->id] = $fileModel;
112  }
113  if ($model->{$relationName}->count() > 0)
114  {
115  $fileModelsToRemove = array();
116  foreach ($model->{$relationName} as $index => $existingFileModel)
117  {
118  if (!isset($newFileModelsIndexedById[$existingFileModel->id]))
119  {
120  $fileModelsToRemove[] = $existingFileModel;
121  }
122  else
123  {
124  unset($newFileModelsIndexedById[$existingFileModel->id]);
125  }
126  }
127  foreach ($fileModelsToRemove as $fileModelToRemove)
128  {
129  $model->{$relationName}->remove($fileModelToRemove);
130  }
131  }
132  //Now add missing fileModels
133  foreach ($newFileModelsIndexedById as $fileModel)
134  {
135  $model->{$relationName}->add($fileModel);
136  }
137  }
138  }
139 
146  public static function makeByExistingFileModelId($fileModelId, $sharedContent = true)
147  {
148  assert('is_int($fileModelId) || (is_string($fileModelId) && !empty($fileModelId))');
149  $existingFileModel = FileModel::getById($fileModelId);
150  return static::makeByFileModel($existingFileModel, $sharedContent);
151  }
152 
159  public static function makeByFileModel($existingFileModel, $sharedContent = true)
160  {
161  $file = new FileModel();
162  ZurmoCopyModelUtil::copy($existingFileModel, $file);
163  if (!$sharedContent)
164  {
165  $fileContent = new FileContent();
166  $fileContent->content = $existingFileModel->fileContent->content;
167  $file->fileContent = $fileContent;
168  }
169  if (!$file->save())
170  {
171  return false;
172  }
173  return $file;
174  }
175  }
176 ?>
static copy(RedBeanModel $model, RedBeanModel $copyToModel)
static makeByFileModel($existingFileModel, $sharedContent=true)
static makeByUploadedFile($uploadedFile)
static makeByFilePathAndName($filePath, $fileName)
static getMimeType($file, $magicFile=null, $checkExtension=true)
static resolveModelsHasManyFilesFromPost(&$model, $relationName, $postDataVariableName)
static getById($id, $modelClassName=null)
static makeByExistingFileModelId($fileModelId, $sharedContent=true)
Generated on Thu May 28 2020 07:10:36