1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: 37: 38: 39: 40: 41: 42:
43: class XoopsImage extends XoopsObject
44: {
45: 46: 47:
48: function XoopsImage()
49: {
50: $this->XoopsObject();
51: $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
52: $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
53: $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
54: $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
55: $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
56: $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
57: $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
58: $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
59: $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
60: }
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
74: class XoopsImageHandler extends XoopsObjectHandler
75: {
76:
77: 78: 79: 80: 81: 82:
83: function &create($isNew = true)
84: {
85: $image =new XoopsImage();
86: if ($isNew) {
87: $image->setNew();
88: }
89: return $image;
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: function &get($id, $getbinary=true)
100: {
101: $ret = false;
102: $id = (int)$id;
103: if ($id > 0) {
104: $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;
105: if ($result = $this->db->query($sql)) {
106: $numrows = $this->db->getRowsNum($result);
107: if ($numrows == 1) {
108: $image =new XoopsImage();
109: $image->assignVars($this->db->fetchArray($result));
110: $ret =& $image;
111: }
112: }
113: }
114: return $ret;
115: }
116:
117: 118: 119: 120: 121: 122:
123: function insert(&$image)
124: {
125: if (strtolower(get_class($image)) != 'xoopsimage') {
126: return false;
127: }
128: if (!$image->isDirty()) {
129: return true;
130: }
131: if (!$image->cleanVars()) {
132: return false;
133: }
134: foreach ($image->cleanVars as $k => $v) {
135: ${$k} = $v;
136: }
137: if ($image->isNew()) {
138: $image_id = $this->db->genId('image_image_id_seq');
139: $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);
140: if (!$result = $this->db->query($sql)) {
141: return false;
142: }
143: if (empty($image_id)) {
144: $image_id = $this->db->getInsertId();
145: }
146: if (isset($image_body) && $image_body != '') {
147: $sql = sprintf("INSERT INTO %s (image_id, image_body) VALUES (%u, %s)", $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
148: if (!$result = $this->db->query($sql)) {
149: $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id);
150: $this->db->query($sql);
151: return false;
152: }
153: }
154: $image->assignVar('image_id', $image_id);
155: } else {
156: $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);
157: if (!$result = $this->db->query($sql)) {
158: return false;
159: }
160: if (isset($image_body) && $image_body != '') {
161: $sql = sprintf("UPDATE %s SET image_body = %s WHERE image_id = %u", $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
162: if (!$result = $this->db->query($sql)) {
163: $this->db->query(sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id));
164: return false;
165: }
166: }
167: }
168: return true;
169: }
170:
171: 172: 173: 174: 175: 176:
177: function delete(&$image)
178: {
179: if (strtolower(get_class($image)) != 'xoopsimage') {
180: return false;
181: }
182: $id = $image->getVar('image_id');
183: $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $id);
184: if (!$result = $this->db->query($sql)) {
185: return false;
186: }
187: $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('imagebody'), $id);
188: $this->db->query($sql);
189: return true;
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199:
200: function &getObjects($criteria = null, $id_as_key = false, $getbinary = false)
201: {
202: $ret = array();
203: $limit = $start = 0;
204: if ($getbinary) {
205: $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';
206: } else {
207: $sql = 'SELECT * FROM '.$this->db->prefix('image');
208: }
209: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
210: $sql .= ' '.$criteria->renderWhere();
211: $sort = !in_array($criteria->getSort(), array('image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight')) ? 'image_weight' : $criteria->getSort();
212: $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
213: $limit = $criteria->getLimit();
214: $start = $criteria->getStart();
215: }
216: $result = $this->db->query($sql, $limit, $start);
217: if (!$result) {
218: return $ret;
219: }
220: while ($myrow = $this->db->fetchArray($result)) {
221: $image =new XoopsImage();
222: $image->assignVars($myrow);
223: if (!$id_as_key) {
224: $ret[] =& $image;
225: } else {
226: $ret[$myrow['image_id']] =& $image;
227: }
228: unset($image);
229: }
230: return $ret;
231: }
232:
233: 234: 235: 236: 237: 238:
239: function getCount($criteria = null)
240: {
241: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('image');
242: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
243: $sql .= ' '.$criteria->renderWhere();
244: }
245: if (!$result =& $this->db->query($sql)) {
246: return 0;
247: }
248: list($count) = $this->db->fetchRow($result);
249: return $count;
250: }
251:
252: 253: 254: 255: 256: 257: 258:
259: function &getList($imgcat_id, $image_display = null)
260: {
261: $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
262: if (isset($image_display)) {
263: $criteria->add(new Criteria('image_display', (int)$image_display));
264: }
265: $images =& $this->getObjects($criteria, false, true);
266: $ret = array();
267: foreach (array_keys($images) as $i) {
268: $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
269: }
270: return $ret;
271: }
272: }
273: ?>
274: