/[drupal]/contributions/modules/node_gallery/node_gallery.model.inc
ViewVC logotype

Contents of /contributions/modules/node_gallery/node_gallery.model.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Thu May 7 05:46:51 2009 UTC (6 months, 3 weeks ago) by kmonty
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1, DRUPAL-6--2
Changes since 1.9: +11 -1 lines
File MIME type: text/x-php
#455620 : Delete ImageCache images when deleting an image/gallery.
1 <?php
2 // $Id: node_gallery.model.inc,v 1.9 2009/05/06 01:57:01 kmonty Exp $
3
4 /**
5 * @file
6 * Node gallery module.
7 *
8 */
9
10 class base {
11 function base($o) {
12 foreach ((array)$o as $k => $v) {
13 $this->$k = $v;
14 }
15 }
16 }
17
18 class gallery_config_gateway {
19 static function get_by($type, $op = 'type') {
20 static $types;
21
22 if (empty($types[$type])) {
23 if ($op == 'id' && is_numeric($type)) {
24 $type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $type));
25 }
26 $result = db_query("SELECT * FROM {ng_gallery_config} gt WHERE gt.gallery_type = '%s'", $type);
27 $t = drupal_unpack(db_fetch_object($result));
28 if (!empty($t)) {
29 $types[$type] = new gallery_config($t);
30 }
31 }
32 return $types[$type];
33 }
34
35 static function get_types($op = 'gallery', $type = NULL) {
36 static $types;
37 if (empty($types)) {
38 $result = db_query("SELECT name, gallery_type, image_type FROM {ng_gallery_config}");
39 while ($o = db_fetch_object($result)) {
40 $types['image'][$o->image_type] = $o;
41 $types['gallery'][$o->gallery_type] = $o;
42 }
43 }
44 return empty($type) ? $types[$op] : $types[$op][$type];
45 }
46
47 static function is_type($op, $type) {
48 $types = self::get_types($op);
49 if (in_array($type, array_keys($types))) {
50 return TRUE;
51 }
52 return FALSE;
53 }
54 }
55
56 class gallery_config extends base {
57
58 function save() {
59 if (empty($this->data)) {
60 $this->upload_settings['number_uploads'] = preg_replace("[^0-9]", "", $this->upload_settings['number_uploads']);
61 foreach ((array)$this as $k => $v) {
62 if ($k != 'name' || $k != 'gallery_type' || $k != 'image_type') {
63 $this->data[$k] = $v;
64 }
65 }
66 }
67 $this->data = serialize($this->data);
68
69 if (!empty($this->gallery_type) && gallery_config_gateway::get_types('gallery', $this->gallery_type)) {
70 drupal_write_record('ng_gallery_config', $this, 'gallery_type');
71 }
72 else {
73 drupal_write_record('ng_gallery_config', $this);
74 }
75 }
76
77 function delete() {
78 return db_query("DELETE FROM {ng_gallery_config} WHERE gallery_type = '%s'", $this->gallery_type);
79 }
80 }
81
82 class gallery_gateway {
83
84 static function find_by($uid = NULL, $type = NULL) {
85 $gallery_types = empty($type) ? array_keys(gallery_config_gateway::get_types()) : $type;
86 $args = $gallery_types;
87 $sql = "SELECT n.nid, n.title, n.type, n.created, n.uid FROM {node} n
88 WHERE n.type IN (". db_placeholders($gallery_types, 'varchar') .") AND n.status = 1";
89 if ($uid) {
90 $sql .= " AND n.uid = %d";
91 $args[] = $uid;
92 }
93 $result = db_query($sql, $args);
94 while ($r = db_fetch_array($result)) {
95 $items[$r['nid']] = new Gallery($r);
96 }
97
98 return $items;
99 }
100
101 static function find_covers($gids) {
102 $gids = is_numeric($gids) ? array($gids) : (array) $gids;
103 $result = db_query("SELECT gid, nid FROM {ng_images} WHERE gid IN (". db_placeholders($gids) .") AND is_cover = 1", $gids);
104 while ($r = db_fetch_array($result)) {
105 $covers[$r['gid']] = $r['nid'];
106 }
107 //if no cover was set, get the first image;
108 foreach ($gids as $gid) {
109 if (empty($covers[$gid])) {
110 $gids2[] = $gid;
111 }
112 }
113 if (!empty($gids2)) {
114 $result = db_query("SELECT gid, nid FROM {ng_images} WHERE gid IN (". db_placeholders($gids2) .") GROUP BY gid", $gids2);
115 while ($r = db_fetch_array($result)) {
116 $covers[$r['gid']] = $r['nid'];
117 }
118 }
119 return $covers;
120 }
121
122 static function count_images($gids) {
123 $gids1 = is_numeric($gids) ? array($gids) : (array) $gids;
124 $result = db_query("SELECT COUNT(nid) AS count, gid FROM {ng_images} WHERE gid IN (". db_placeholders($gids1) .")
125 GROUP BY gid", $gids1);
126 while ($r = db_fetch_array($result)) {
127 $items[$r['gid']] = empty($r['count']) ? 0 : $r['count'];
128 }
129 return is_numeric($gids) ? $items[$gids] : $items;
130 }
131
132 static function delete($gids) {
133 $gids = is_numeric($gids) ? array($gids) : (array) $gids;
134 return db_query("DELETE FROM {ng_images} WHERE gid IN (". db_placeholders($gids) .")", $gids);
135 }
136
137 static function get_images($gid) {
138 $result = db_query("SELECT nid FROM {ng_images} WHERE gid = %d", $gid);
139 while ($r = db_fetch_array($result)) {
140 $nids[] = $r['nid'];
141 }
142 if (!empty($nids)) {
143 return ImageGateway::find_details($nids);
144 }
145 }
146 }
147
148 class Gallery extends base {
149 function delete() {
150 return db_query("DELETE FROM {ng_images} WHERE gid = %d", $this->nid);
151 }
152
153 function get_config() {
154 if (empty($this->config)) {
155 $this->config = gallery_config_gateway::get_by($this->type);
156 }
157
158 return $this->config;
159 }
160
161 function get_images() {
162 if (empty($this->images)) {
163 $result = db_query("SELECT nid FROM {ng_images} WHERE gid = %d", $this->nid);
164 while ($r = db_fetch_array($result)) {
165 $nids[] = $r['nid'];
166 }
167 if (!empty($nids)) {
168 $this->images = ImageGateway::find_details($nids);
169 $this->image_count = count($this->images);
170 }
171 }
172 return $this->images;
173 }
174
175 function get_content($teaser = 0) {
176 $node = db_fetch_object(db_query("SELECT teaser, body, format FROM {node_revisions} WHERE nid = %d", $this->nid));
177 $node->body = isset($node->body) ? str_replace('<!--break-->', '', $node->body) : '';
178 $node = node_prepare($node, $teaser);
179 return $node->content['body']['#value'];
180 }
181
182 function get_title() {
183 if (empty($this->title)) {
184 $this->title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $this->nid));
185 }
186 return $this->title;
187 }
188
189 function get_image_navigator($nid) {
190 $result = db_query("SELECT nid FROM {ng_images} WHERE gid = %d ORDER BY weight, nid", $this->nid);
191 while ($r = db_fetch_array($result)) {
192 $items[] = $r['nid'];
193 }
194 $navigator['total'] = count($items);
195 $navigator['parent'] = $this->nid;
196 for ($i = 0; $i < $navigator['total']; $i++) {
197 if ($items[$i] == $nid) {
198 $navigator['current'] = $i + 1;
199 $navigator['prev_nid'] = ($i == 0) ? $items[$navigator['total'] - 1] : $items[$i - 1];
200 $navigator['next_nid'] = ($i == ($navigator['total'] - 1)) ? $items[0] : $items[$i + 1];
201 }
202 }
203 return $navigator;
204 }
205 }
206
207 class ImageGateway {
208 static function find_details($nids) {
209 $nids = is_numeric($nids) ? array($nids) : (array) $nids;
210
211 $sql = "SELECT n.nid, n.vid, n.title, n.type, n.created, nr.body, i.*, f.* FROM {node} n
212 INNER JOIN {node_revisions} nr ON n.vid = nr.vid INNER JOIN {ng_images} i
213 ON n.nid = i.nid INNER JOIN {files} f ON i.fid = f.fid WHERE n.nid IN (". db_placeholders($nids) .") AND n.status = 1
214 ORDER BY i.weight, i.nid";
215 $result = db_query($sql, $nids);
216
217 while ($o = db_fetch_object($result)) {
218 $items[$o->nid] = new Image($o);
219 }
220
221 if (module_exists('content')) {
222 foreach ($items as &$item) {
223 $content_type = content_types($item->type);
224 if (!empty($content_type['fields'])) {
225 content_load($item);
226 }
227 }
228 }
229
230 return $items;
231 }
232
233 static function get_file($nids) {
234 $nids2 = is_numeric($nids) ? array($nids) : (array) $nids;
235 $result = db_query("SELECT i.*, f.* FROM {ng_images} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid
236 IN (". db_placeholders($nids2) .")", $nids2);
237 while ($r = db_fetch_array($result)) {
238 $items[$r['nid']] = new Image($r);
239 }
240 return is_numeric($nids) ? $items[$nids] : $items;
241 }
242
243 static function delete($image) {
244 db_query("DELETE FROM {ng_images} WHERE nid = %d", $image->nid);
245 db_query("DELETE FROM {files} WHERE fid = %d", $image->fid);
246 file_delete($image->filepath);
247
248 // Make sure to flush out the old imagecache images
249 if (function_exists(imagecache_image_flush)) {
250 imagecache_image_flush($image->filepath);
251 }
252 }
253 }
254
255 class Image extends base {
256 function delete() {
257 node_delete($this->nid);
258 db_query("DELETE FROM {ng_images} WHERE nid = %d", $this->nid);
259 db_query("DELETE FROM {files} WHERE fid = %d", $this->fid);
260 file_delete($this->filepath);
261
262 // Make sure to flush out the old imagecache images
263 if (function_exists(imagecache_image_flush)) {
264 imagecache_image_flush($this->filepath);
265 }
266 }
267
268 function save($form) {
269 module_load_include('inc', 'node', 'node.pages');
270 $form_state['values'] = (array)$this;
271 node_form_submit($form, $form_state);
272 //insert node;
273 if (empty($this->nid)) {
274 $this->nid = $form_state['nid'];
275 }
276 file_set_status($this, FILE_STATUS_PERMANENT);
277 if ($this->gid && $this->nid) {
278 $this->add_to_gallery();
279 }
280 }
281
282 function add_to_gallery() {
283 //update
284 if (db_result(db_query("SELECT nid FROM {ng_images} WHERE gid = %d AND nid = %d", $this->gid, $this->nid))) {
285 return drupal_write_record('ng_images', $this, array('gid', 'nid'));
286 }
287 //insert
288 else {
289 $has_images = db_result(db_query("SELECT nid FROM {ng_images} WHERE gid = %d", $this->gid));
290 //the first upload image is set to default cover;
291 if (!$has_images) {
292 $this->is_cover = 1;
293 }
294 else {
295 //only one image can be set to cover;
296 if ($this->is_cover) {
297 db_query("UPDATE {ng_images} SET is_cover = %d WHERE is_cover = 1 AND gid = %d", $this->gid);
298 }
299 }
300 return drupal_write_record('ng_images', $this);
301 }
302 }
303 }

  ViewVC Help
Powered by ViewVC 1.1.2