1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
19: |
|
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: |
|
27: | class XoopsImage extends XoopsObject
|
28: | {
|
29: |
|
30: | public $image_id;
|
31: | public $image_nam;
|
32: | public $image_nicename;
|
33: | public $image_mimetype;
|
34: | public $image_created;
|
35: | public $image_display;
|
36: | public $image_weight;
|
37: | public $image_body;
|
38: | public $imgcat_id;
|
39: |
|
40: | |
41: | |
42: |
|
43: | public function __construct()
|
44: | {
|
45: | parent::__construct();
|
46: | $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
|
47: | $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
|
48: | $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
|
49: | $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
|
50: | $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
|
51: | $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
|
52: | $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
|
53: | $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
|
54: | $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
|
55: | }
|
56: |
|
57: | |
58: | |
59: | |
60: | |
61: |
|
62: | public function id($format = 'N')
|
63: | {
|
64: | return $this->getVar('image_id', $format);
|
65: | }
|
66: |
|
67: | |
68: | |
69: | |
70: | |
71: |
|
72: | public function image_id($format = '')
|
73: | {
|
74: | return $this->getVar('image_id', $format);
|
75: | }
|
76: |
|
77: | |
78: | |
79: | |
80: | |
81: |
|
82: | public function image_name($format = '')
|
83: | {
|
84: | return $this->getVar('image_name', $format);
|
85: | }
|
86: |
|
87: | |
88: | |
89: | |
90: | |
91: |
|
92: | public function image_nicename($format = '')
|
93: | {
|
94: | return $this->getVar('image_nicename', $format);
|
95: | }
|
96: |
|
97: | |
98: | |
99: | |
100: | |
101: |
|
102: | public function image_mimetype($format = '')
|
103: | {
|
104: | return $this->getVar('image_mimetype', $format);
|
105: | }
|
106: |
|
107: | |
108: | |
109: | |
110: | |
111: |
|
112: | public function image_created($format = '')
|
113: | {
|
114: | return $this->getVar('image_created', $format);
|
115: | }
|
116: |
|
117: | |
118: | |
119: | |
120: | |
121: |
|
122: | public function image_display($format = '')
|
123: | {
|
124: | return $this->getVar('image_display', $format);
|
125: | }
|
126: |
|
127: | |
128: | |
129: | |
130: | |
131: |
|
132: | public function image_weight($format = '')
|
133: | {
|
134: | return $this->getVar('image_weight', $format);
|
135: | }
|
136: |
|
137: | |
138: | |
139: | |
140: | |
141: |
|
142: | public function image_body($format = '')
|
143: | {
|
144: | return $this->getVar('image_body', $format);
|
145: | }
|
146: |
|
147: | |
148: | |
149: | |
150: | |
151: |
|
152: | public function imgcat_id($format = '')
|
153: | {
|
154: | return $this->getVar('imgcat_id', $format);
|
155: | }
|
156: | }
|
157: |
|
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: |
|
169: | class XoopsImageHandler extends XoopsObjectHandler
|
170: | {
|
171: | |
172: | |
173: | |
174: | |
175: | |
176: |
|
177: | public function create($isNew = true)
|
178: | {
|
179: | $image = new XoopsImage();
|
180: | if ($isNew) {
|
181: | $image->setNew();
|
182: | }
|
183: |
|
184: | return $image;
|
185: | }
|
186: |
|
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: |
|
194: | public function get($id, $getbinary = true)
|
195: | {
|
196: | $image = false;
|
197: | $id = (int)$id;
|
198: | if ($id > 0) {
|
199: | $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;
|
200: | $result = $this->db->query($sql);
|
201: | if (!$this->db->isResultSet($result)) {
|
202: | return $image;
|
203: | }
|
204: | $numrows = $this->db->getRowsNum($result);
|
205: | if ($numrows == 1) {
|
206: | $image = new XoopsImage();
|
207: | $image->assignVars($this->db->fetchArray($result));
|
208: | }
|
209: | }
|
210: |
|
211: | return $image;
|
212: | }
|
213: |
|
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: |
|
221: | public function insert(XoopsObject $image)
|
222: | {
|
223: | $className = 'XoopsImage';
|
224: | if (!($image instanceof $className)) {
|
225: | return false;
|
226: | }
|
227: |
|
228: | if (!$image->isDirty()) {
|
229: | return true;
|
230: | }
|
231: | if (!$image->cleanVars()) {
|
232: | return false;
|
233: | }
|
234: | foreach ($image->cleanVars as $k => $v) {
|
235: | ${$k} = $v;
|
236: | }
|
237: | if ($image->isNew()) {
|
238: | $image_id = $this->db->genId('image_image_id_seq');
|
239: | $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);
|
240: | if (!$result = $this->db->query($sql)) {
|
241: | return false;
|
242: | }
|
243: | if (empty($image_id)) {
|
244: | $image_id = $this->db->getInsertId();
|
245: | }
|
246: | if (isset($image_body) && $image_body != '') {
|
247: | $sql = sprintf('INSERT INTO %s (image_id, image_body) VALUES (%u, %s)', $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
|
248: | if (!$result = $this->db->query($sql)) {
|
249: | $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id);
|
250: | $this->db->query($sql);
|
251: |
|
252: | return false;
|
253: | }
|
254: | }
|
255: | $image->assignVar('image_id', $image_id);
|
256: | } else {
|
257: | $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);
|
258: | if (!$result = $this->db->query($sql)) {
|
259: | return false;
|
260: | }
|
261: | if (isset($image_body) && $image_body != '') {
|
262: | $sql = sprintf('UPDATE %s SET image_body = %s WHERE image_id = %u', $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
|
263: | if (!$result = $this->db->query($sql)) {
|
264: | $this->db->query(sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id));
|
265: |
|
266: | return false;
|
267: | }
|
268: | }
|
269: | }
|
270: |
|
271: | return true;
|
272: | }
|
273: |
|
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: |
|
281: | public function delete(XoopsObject $image)
|
282: | {
|
283: | $className = 'XoopsImage';
|
284: | if (!($image instanceof $className)) {
|
285: | return false;
|
286: | }
|
287: |
|
288: | $id = $image->getVar('image_id');
|
289: | $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $id);
|
290: | if (!$result = $this->db->query($sql)) {
|
291: | return false;
|
292: | }
|
293: | $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('imagebody'), $id);
|
294: | $this->db->query($sql);
|
295: |
|
296: | return true;
|
297: | }
|
298: |
|
299: | |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | |
306: |
|
307: | public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $getbinary = false)
|
308: | {
|
309: | $ret = array();
|
310: | $limit = $start = 0;
|
311: | if ($getbinary) {
|
312: | $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';
|
313: | } else {
|
314: | $sql = 'SELECT * FROM ' . $this->db->prefix('image');
|
315: | }
|
316: | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
|
317: | $sql .= ' ' . $criteria->renderWhere();
|
318: | $sort = $criteria->getSort() == '' ? 'image_weight' : $criteria->getSort();
|
319: | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
|
320: | $limit = $criteria->getLimit();
|
321: | $start = $criteria->getStart();
|
322: | }
|
323: | $result = $this->db->query($sql, $limit, $start);
|
324: | if (!$this->db->isResultSet($result)) {
|
325: | return $ret;
|
326: | }
|
327: |
|
328: | while (false !== ($myrow = $this->db->fetchArray($result))) {
|
329: | $image = new XoopsImage();
|
330: | $image->assignVars($myrow);
|
331: | if (!$id_as_key) {
|
332: | $ret[] =& $image;
|
333: | } else {
|
334: | $ret[$myrow['image_id']] =& $image;
|
335: | }
|
336: | unset($image);
|
337: | }
|
338: |
|
339: | return $ret;
|
340: | }
|
341: |
|
342: | |
343: | |
344: | |
345: | |
346: | |
347: |
|
348: | public function getCount(CriteriaElement $criteria = null)
|
349: | {
|
350: | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('image');
|
351: | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
|
352: | $sql .= ' ' . $criteria->renderWhere();
|
353: | }
|
354: | $result = $this->db->query($sql);
|
355: | if (!$this->db->isResultSet($result)) {
|
356: | return 0;
|
357: | }
|
358: | list($count) = $this->db->fetchRow($result);
|
359: |
|
360: | return (int)$count;
|
361: | }
|
362: |
|
363: | |
364: | |
365: | |
366: | |
367: | |
368: | |
369: |
|
370: | public function getList($imgcat_id, $image_display = null)
|
371: | {
|
372: | $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
|
373: | if (isset($image_display)) {
|
374: | $criteria->add(new Criteria('image_display', (int)$image_display));
|
375: | }
|
376: | $images = $this->getObjects($criteria, false, true);
|
377: | $ret = array();
|
378: | foreach (array_keys($images) as $i) {
|
379: | $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
|
380: | }
|
381: |
|
382: | return $ret;
|
383: | }
|
384: | }
|
385: | |