All Data Structures Functions Variables Pages
ImageFileModel.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  class ImageFileModel extends FileModel
42  {
43  const DEFAULT_THUMBNAIL_HEIGHT = 30;
44  const DEFAULT_THUMBNAIL_WIDTH = 65;
45  const THUMB_FILE_NAME_PREFIX = 'thumb_';
46  const FILE_NAME_SEPARATOR = '_';
47 
48  public static function getDefaultMetadata()
49  {
50  $metadata = parent::getDefaultMetadata();
51  $metadata[__CLASS__] = array(
52  'members' => array(
53  'isShared',
54  'width',
55  'height',
56  'inactive'
57  ),
58  'rules' => array(
59  array('isShared', 'boolean'),
60  array('isShared', 'default', 'value' => false),
61  array('width', 'type', 'type' => 'integer'),
62  array('height', 'type', 'type' => 'integer'),
63  array('inactive', 'boolean'),
64  array('inactive', 'default', 'value' => false),
65  ),
66  );
67  return $metadata;
68  }
69 
70  protected static function translatedAttributeLabels($language)
71  {
72  return array_merge(parent::translatedAttributeLabels($language),
73  array(
74  'isShared' => Zurmo::t('ZurmoModule', 'Shared', array(), null, $language),
75  'width' => Zurmo::t('Core', 'Width', array(), null, $language),
76  'height' => Zurmo::t('Core', 'Height', array(), null, $language),
77  'inactive' => Zurmo::t('ZurmoModule', 'Inactive', array(), null, $language),
78  )
79  );
80  }
81 
88  public static function getByFileName($fileName)
89  {
90  return static::getById(static::getIdByFileName($fileName));
91  }
92 
99  protected static function getIdByFileName($fileName)
100  {
101  $matches = array();
102  $pattern = '/^(\d+)' . static::FILE_NAME_SEPARATOR . '/'; // Not Coding Standard
103  preg_match($pattern, $fileName, $matches);
104  if (count($matches) == 2)
105  {
106  return (int) $matches[1];
107  }
108  else
109  {
110  throw new NotFoundException();
111  }
112  }
113 
119  public function getImageCachePath($shouldGetThumbnail = false)
120  {
121  if ($shouldGetThumbnail)
122  {
123  return static::getPathToCachedFiles() . static::THUMB_FILE_NAME_PREFIX . $this->getImageCacheFileName();
124  }
125  return static::getPathToCachedFiles() . $this->getImageCacheFileName();
126  }
127 
134  public static function getImageCachePathByFileName($fileName, $shouldReturnThumbnail)
135  {
136  assert('is_string($fileName)');
137  assert('is_bool($shouldReturnThumbnail)');
138  if ($shouldReturnThumbnail)
139  {
140  $fileName = static::THUMB_FILE_NAME_PREFIX . $fileName;
141  }
142  return static::getPathToCachedFiles() . $fileName;
143  }
144 
149  protected static function getPathToCachedFiles()
150  {
151  return Yii::getPathOfAlias('application.runtime.uploads') . DIRECTORY_SEPARATOR;
152  }
153 
158  public function getImageCacheFileName()
159  {
160  return $this->id . static::FILE_NAME_SEPARATOR . $this->name;
161  }
162 
167  public function createImageCache($shouldCreateThumbnail = false)
168  {
169  $this->createCacheDirIfNotExists();
170  if (!$this->isImageCached($shouldCreateThumbnail))
171  {
172  $this->cacheImage($shouldCreateThumbnail);
173  }
174  }
175 
179  protected function createCacheDirIfNotExists()
180  {
181  if (!is_dir(Yii::getPathOfAlias('application.runtime.uploads')))
182  {
183  mkdir(Yii::getPathOfAlias('application.runtime.uploads'), 0755, true); // set recursive flag and permissions 0755
184  }
185  }
186 
192  protected function isImageCached($checkThumbnail = false)
193  {
194  $imageCachePath = $this->getImageCachePath($checkThumbnail);
195  return file_exists($imageCachePath);
196  }
197 
202  protected function cacheImage($shouldCacheThumbnail)
203  {
204  $imageCachePath = $this->getImageCachePath($shouldCacheThumbnail);
205  if ($shouldCacheThumbnail)
206  {
207  $newWidth = static::DEFAULT_THUMBNAIL_WIDTH;
208  $newHeight = static::DEFAULT_THUMBNAIL_HEIGHT;
209  WideImage::load($this->fileContent->content)->resize($newWidth, $newHeight)->saveToFile($imageCachePath);
210  }
211  else
212  {
213  file_put_contents($imageCachePath, $this->fileContent->content);
214  }
215  }
216 
217  public function fileTypeValidator($attribute, $params)
218  {
219  if ($this->type == 'image/png' ||
220  $this->type == 'image/jpg' ||
221  $this->type == 'image/gif' ||
222  $this->type == 'image/jpeg')
223  {
224  return true;
225  }
226  else
227  {
228  $this->addError($attribute, Zurmo::t('ZurmoModule', 'File type is not valid.'));
229  return false;
230  }
231  }
232 
233  public static function getModuleClassName()
234  {
235  return 'ImagesModule';
236  }
237 
238  public function toggle($attribute)
239  {
240  if ($this->isToggleable($attribute))
241  {
242  $this->{$attribute} = !$this->{$attribute};
243  $this->save();
244  }
245  else
246  {
247  throw new NotSupportedException();
248  }
249  }
250 
251  public function isToggleable($attribute)
252  {
253  if (Yii::app()->user->userModel->isSame($this->createdByUser) && $attribute == 'isShared')
254  {
255  return true;
256  }
257  return false;
258  }
259 
260  public function canDelete()
261  {
262  if (Yii::app()->user->userModel->isSame($this->createdByUser))
263  {
264  return true;
265  }
266  return false;
267  }
268 
269  public function isEditableByCurrentUser()
270  {
271  return (Yii::app()->user->userModel->isSame($this->createdByUser) || $this->isShared);
272  }
273  }
274 ?>
static getImageCachePathByFileName($fileName, $shouldReturnThumbnail)
isImageCached($checkThumbnail=false)
static getIdByFileName($fileName)
getImageCachePath($shouldGetThumbnail=false)
static getPathToCachedFiles()
createImageCache($shouldCreateThumbnail=false)
static getByFileName($fileName)
cacheImage($shouldCacheThumbnail)
addError($attributeName, $errorMessage)
Generated on Thu May 28 2020 07:10:35