Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
NewContextImageFileForm.inc.php
1 <?php
2 
16 import('lib.pkp.controllers.tab.settings.form.SettingsFileUploadForm');
17 
19 
24  function NewContextImageFileForm($imageSettingName) {
25  parent::SettingsFileUploadForm('controllers/tab/settings/form/newImageFileUploadForm.tpl');
26  $this->setFileSettingName($imageSettingName);
27  }
28 
29 
30  //
31  // Extend methods from SettingsFileUploadForm.
32  //
36  function fetch($request) {
37  $params = array('fileType' => 'image');
38  return parent::fetch($request, $params);
39  }
40 
41 
42  //
43  // Extend methods from Form.
44  //
48  function getLocaleFieldNames() {
49  return array('imageAltText');
50  }
51 
55  function initData($request) {
56  $context = $request->getContext();
57  $fileSettingName = $this->getFileSettingName();
58 
59  $image = $context->getSetting($fileSettingName);
60  $imageAltText = array();
61 
63  foreach ($supportedLocales as $key => $locale) {
64  $imageAltText[$key] = $image[$key]['altText'];
65  }
66 
67  $this->setData('imageAltText', $imageAltText);
68  }
69 
73  function readInputData() {
74  $this->readUserVars(array('imageAltText'));
75 
76  parent::readInputData();
77  }
78 
83  function execute($request) {
84  $temporaryFile = $this->fetchTemporaryFile($request);
85 
86  import('classes.file.PublicFileManager');
87  $publicFileManager = new PublicFileManager();
88 
89  if (is_a($temporaryFile, 'TemporaryFile')) {
90  $type = $temporaryFile->getFileType();
91  $extension = $publicFileManager->getImageExtension($type);
92  if (!$extension) {
93  return false;
94  }
95  $locale = AppLocale::getLocale();
96  $context = $request->getContext();
97 
98  $uploadName = $this->getFileSettingName() . '_' . $locale . $extension;
99  if($publicFileManager->copyContextFile($context->getAssocType(), $context->getId(), $temporaryFile->getFilePath(), $uploadName)) {
100 
101  // Get image dimensions
102  $filePath = $publicFileManager->getContextFilesPath($context->getAssocType(), $context->getId());
103  list($width, $height) = getimagesize($filePath . '/' . $uploadName);
104 
105  $value = $context->getSetting($this->getFileSettingName());
106  $imageAltText = $this->getData('imageAltText');
107 
108  $value[$locale] = array(
109  'name' => $temporaryFile->getOriginalFileName(),
110  'uploadName' => $uploadName,
111  'width' => $width,
112  'height' => $height,
113  'dateUploaded' => Core::getCurrentDate(),
114  'altText' => $imageAltText[$locale]
115  );
116 
117  $settingsDao = $context->getSettingsDAO();
118  $settingsDao->updateSetting($context->getId(), $this->getFileSettingName(), $value, 'object', true);
119 
120  // Clean up the temporary file.
121  $this->removeTemporaryFile($request);
122 
123  return true;
124  }
125  }
126  return false;
127  }
128 }
129 
130 ?>
setData($key, $value)
Definition: Form.inc.php:219
Wrapper class for uploading files to a site/press&#39; public directory.
readUserVars($vars)
Definition: Form.inc.php:364
static getSupportedLocales()
Base class for a settings upload file form.
getData($key)
Definition: Form.inc.php:210
static getLocale()
NewContextImageFileForm($imageSettingName)
$supportedLocales
Definition: Form.inc.php:83
Form for upload an image.
static getCurrentDate($ts=null)
Definition: Core.inc.php:95