XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
ImageUploadForm.class.php
1<?php
10
11if (!defined('XOOPS_ROOT_PATH')) {
12 exit();
13}
14
15require_once XOOPS_ROOT_PATH . '/core/XCube_ActionForm.class.php';
16require_once XOOPS_MODULE_PATH . '/legacy/class/Legacy_Validator.class.php';
17
19{
20 public $mOldFileName = null;
21 public $_mIsNew = null;
22 public $mFormFile = null;
23
24 public function getTokenName()
25 {
26 return 'module.legacy.ImageUploadForm.TOKEN' . $this->get('imgcat_id');
27 }
28
29 public function prepare()
30 {
31 //
32 // Set form properties
33 //
34 $this->mFormProperties['image_name'] =new XCube_ImageFileProperty('image_name');
35 $this->mFormProperties['image_nicename'] =new XCube_StringProperty('image_nicename');
36 $this->mFormProperties['imgcat_id'] =new XCube_IntProperty('imgcat_id');
37
38 //
39 // Set field properties
40 //
41 $this->mFieldProperties['image_name'] =new XCube_FieldProperty($this);
42 $this->mFieldProperties['image_name']->setDependsByArray(['extension']);
43 $this->mFieldProperties['image_name']->addVar('extension', 'jpg,gif,png');
44
45 $this->mFieldProperties['image_nicename'] =new XCube_FieldProperty($this);
46 $this->mFieldProperties['image_nicename']->setDependsByArray(['required']);
47 $this->mFieldProperties['image_nicename']->addMessage('required', _MD_LEGACY_ERROR_REQUIRED, _MD_LEGACY_LANG_IMAGE_NICENAME);
48
49 $this->mFieldProperties['imgcat_id'] =new XCube_FieldProperty($this);
50 $this->mFieldProperties['imgcat_id']->setDependsByArray(['required', 'objectExist']);
51 $this->mFieldProperties['imgcat_id']->addMessage('required', _MD_LEGACY_ERROR_REQUIRED, _MD_LEGACY_LANG_IMGCAT_ID);
52 $this->mFieldProperties['imgcat_id']->addMessage('objectExist', _MD_LEGACY_ERROR_OBJECTEXIST, _MD_LEGACY_LANG_IMGCAT_ID);
53 $this->mFieldProperties['imgcat_id']->addVar('handler', 'imagecategory');
54 $this->mFieldProperties['imgcat_id']->addVar('module', 'legacy');
55
56 // Fix the bug #1769768
57 // https://sourceforge.net/tracker/?func=detail&aid=1769768&group_id=159211&atid=943471
58 // The action form should not load language files and should be given resources
59 // from outside. However, the ideal fix needs changing much message catalogs
60 // including code which I can not edit. So I put the following code as an
61 // exception.
62 $root =& XCube_Root::getSingleton();
63 $root->mLanguageManager->loadModuleAdminMessageCatalog('legacy');
64 }
65
69 public function validateImgcat_id()
70 {
71 $imgcat_id = $this->get('imgcat_id');
72 if (null !== $imgcat_id) {
73 $root =& XCube_Root::getSingleton();
74 $xoopsUser =& $root->mController->mRoot->mContext->mXoopsUser;
75
76 $groups = [];
77 if (is_object($xoopsUser)) {
78 $groups =& $xoopsUser->getGroups();
79 } else {
80 $groups = [XOOPS_GROUP_ANONYMOUS];
81 }
82
83 $handler =& xoops_getmodulehandler('imagecategory', 'legacy');
84 $imgcat =& $handler->get($imgcat_id);
85 if (is_object($imgcat) && !$imgcat->hasUploadPerm($groups)) {
86 $this->addErrorMessage(_MD_LEGACY_ERROR_PERMISSION);
87 }
88 }
89 }
90
91 public function validateImage_name()
92 {
93 $formFile = $this->get('image_name');
94
95 if (null == $formFile && $this->_mIsNew) {
96 $this->addErrorMessage(_MD_LEGACY_ERROR_YOU_MUST_UPLOAD);
97 }
98 }
99
100 public function validate()
101 {
102 parent::validate();
103
104 $handler =& xoops_getmodulehandler('imagecategory', 'legacy');
105 $category =& $handler->get($this->get('imgcat_id'));
106
107 $formFile = $this->get('image_name');
108
109 if (null !== $formFile && is_object($category)) {
110 //
111 // Imagefile width & height check.
112 //
113 if ($formFile->getWidth() > $category->get('imgcat_maxwidth') || $formFile->getHeight() > $category->get('imgcat_maxheight')) {
114 $this->addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_IMG_SIZE, $category->get('imgcat_maxwidth'), $category->get('imgcat_maxheight')));
115 }
116
117 //
118 // Check file size
119 //
120 if ($formFile->getFilesize() > $category->get('imgcat_maxsize')) {
121 $this->addErrorMessage(XCube_Utils::formatString(_AD_LEGACY_ERROR_IMG_FILESIZE, $category->get('imgcat_maxsize')));
122 }
123 }
124 }
125
126 public function load(&$obj)
127 {
128 $this->set('image_nicename', $obj->get('image_nicename'));
129 $this->set('imgcat_id', $obj->get('imgcat_id'));
130
131 $this->_mIsNew = $obj->isNew();
132 $this->mOldFileName = $obj->get('image_name');
133 }
134
135 public function update(&$obj)
136 {
137 $obj->set('image_nicename', $this->get('image_nicename'));
138 $obj->set('image_display', true);
139 $obj->set('imgcat_id', $this->get('imgcat_id'));
140
141 $handler =& xoops_getmodulehandler('imagecategory', 'legacy');
142 $category =& $handler->get($this->get('imgcat_id'));
143
144 $this->mFormFile = $this->get('image_name');
145
146 if (null !== $this->mFormFile) {
147 $this->mFormFile->setRandomToBodyName('img');
148
149 $filename = $this->mFormFile->getBodyName();
150 $this->mFormFile->setBodyName(substr($filename, 0, 24));
151
152 $obj->set('image_name', $this->mFormFile->getFileName());
153 $obj->set('image_mimetype', $this->mFormFile->getContentType());
154
155 //
156 // To store db
157 //
158 if ('db' == $category->get('imgcat_storetype')) {
159 $obj->loadImageBody();
160 if (!is_object($obj->mImageBody)) {
161 $obj->mImageBody =& $obj->createImageBody();
162 }
163
164 //
165 // Access to private member property.
166 //
167 $obj->mImageBody->set('image_body', file_get_contents($this->mFormFile->_mTmpFileName));
168 }
169 }
170 }
171}
validate()
Validates fetched values.
update(&$obj)
[Abstract] Updates an object with properties values.
getTokenName()
Gets the token name of this actionform's token.
prepare()
[Abstract] Set up form properties and field properties.
load(&$obj)
[Abstract] Initializes properties' values from an object.
addErrorMessage( $message)
Adds a message to the form's error message buffer.
[Abstract] Used for validating member property values of XCube_ActionForm.
This is extended XCube_FileProperty and limits uploaded files by image files.
Represents int property.
Represents string property.
static formatString()
[Static] Formats string with special care for international.