XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
image.php
1<?php
11
12if (!defined('XOOPS_ROOT_PATH')) {
13 exit();
14}
15
22{
23
24 public function __construct()
25 {
26 parent::__construct();
27 $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
28 $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
29 $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
30 $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
31 $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
32 $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
33 $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
34 $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
35 $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
36 }
37}
38
50{
51
58 public function &create($isNew = true)
59 {
60 $image =new XoopsImage();
61 if ($isNew) {
62 $image->setNew();
63 }
64 return $image;
65 }
66
74 public function &get($id, $getbinary=true)
75 {
76 $ret = false;
77 $id = (int)$id;
78 if ($id > 0) {
79 $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id WHERE i.image_id='.$id;
80 if ($result = $this->db->query($sql)) {
81 $numrows = $this->db->getRowsNum($result);
82 if (1 == $numrows) {
83 $image =new XoopsImage();
84 $image->assignVars($this->db->fetchArray($result));
85 $ret =& $image;
86 }
87 }
88 }
89 return $ret;
90 }
91
98 public function insert(&$image)
99 {
100 $image_name = null;
101 $image_nicename = null;
102 $image_mimetype = null;
103 $image_display = null;
104 $image_weight = null;
105 $imgcat_id = null;
106 if ('xoopsimage' != strtolower(get_class($image))) {
107 return false;
108 }
109 if (!$image->isDirty()) {
110 return true;
111 }
112 if (!$image->cleanVars()) {
113 return false;
114 }
115 foreach ($image->cleanVars as $k => $v) {
116 ${$k} = $v;
117 }
118 if ($image->isNew()) {
119 $image_id = $this->db->genId('image_image_id_seq');
120 $sql = sprintf('INSERT INTO %s (image_id, image_name, image_nicename, image_mimetype, image_created, image_display, image_weight, imgcat_id) VALUES (%u, %s, %s, %s, %u, %u, %u, %u)', $this->db->prefix('image'), $image_id, $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $this->db->quoteString($image_mimetype), time(), $image_display, $image_weight, $imgcat_id);
121 if (!$result = $this->db->query($sql)) {
122 return false;
123 }
124 if (empty($image_id)) {
125 $image_id = $this->db->getInsertId();
126 }
127 if (isset($image_body) && '' != $image_body) {
128 $sql = sprintf('INSERT INTO %s (image_id, image_body) VALUES (%u, %s)', $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
129 if (!$result = $this->db->query($sql)) {
130 $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id);
131 $this->db->query($sql);
132 return false;
133 }
134 }
135 $image->assignVar('image_id', $image_id);
136 } else {
137 $sql = sprintf('UPDATE %s SET image_name = %s, image_nicename = %s, image_display = %u, image_weight = %u, imgcat_id = %u WHERE image_id = %u', $this->db->prefix('image'), $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $image_display, $image_weight, $imgcat_id, $image_id);
138 if (!$result = $this->db->query($sql)) {
139 return false;
140 }
141 if (isset($image_body) && '' != $image_body) {
142 $sql = sprintf('UPDATE %s SET image_body = %s WHERE image_id = %u', $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
143 if (!$result = $this->db->query($sql)) {
144 $this->db->query(sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id));
145 return false;
146 }
147 }
148 }
149 return true;
150 }
151
158 public function delete(&$image)
159 {
160 if ('xoopsimage' != strtolower(get_class($image))) {
161 return false;
162 }
163 $id = $image->getVar('image_id');
164 $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $id);
165 if (!$result = $this->db->query($sql)) {
166 return false;
167 }
168 $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('imagebody'), $id);
169 $this->db->query($sql);
170 return true;
171 }
172
181 public function &getObjects($criteria = null, $id_as_key = false, $getbinary = false)
182 {
183 $ret = [];
184 $limit = $start = 0;
185 if ($getbinary) {
186 $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id';
187 } else {
188 $sql = 'SELECT * FROM '.$this->db->prefix('image');
189 }
190 if (isset($criteria) && $criteria instanceof \criteriaelement) {
191 $sql .= ' '.$criteria->renderWhere();
192 $sort = !in_array($criteria->getSort(), ['image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight']) ? 'image_weight' : $criteria->getSort();
193 $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
194 $limit = $criteria->getLimit();
195 $start = $criteria->getStart();
196 }
197 $result = $this->db->query($sql, $limit, $start);
198 if (!$result) {
199 return $ret;
200 }
201 while ($myrow = $this->db->fetchArray($result)) {
202 $image =new XoopsImage();
203 $image->assignVars($myrow);
204 if (!$id_as_key) {
205 $ret[] =& $image;
206 } else {
207 $ret[$myrow['image_id']] =& $image;
208 }
209 unset($image);
210 }
211 return $ret;
212 }
213
220 public function getCount($criteria = null)
221 {
222 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('image');
223 if (isset($criteria) && $criteria instanceof \criteriaelement) {
224 $sql .= ' '.$criteria->renderWhere();
225 }
226 if (!$result =& $this->db->query($sql)) {
227 return 0;
228 }
229 [$count] = $this->db->fetchRow($result);
230 return $count;
231 }
232
240 public function &getList($imgcat_id, $image_display = null)
241 {
242 $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
243 if (isset($image_display)) {
244 $criteria->add(new Criteria('image_display', (int)$image_display));
245 }
246 $images =& $this->getObjects($criteria, false, true);
247 $ret = [];
248 foreach (array_keys($images) as $i) {
249 $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
250 }
251 return $ret;
252 }
253}
insert(&$image)
Definition image.php:98
& getObjects($criteria=null, $id_as_key=false, $getbinary=false)
Definition image.php:181
getCount($criteria=null)
Definition image.php:220
& getList($imgcat_id, $image_display=null)
Definition image.php:240
& create($isNew=true)
Definition image.php:58
__construct()
Definition image.php:24
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206