don't try to show non existing images in index matrix
[phpfspot.git] / phpfspot.class.php
1 <?php
2
3 require_once "phpfspot_cfg.php";
4 require_once "phpfspot_db.php";
5 require_once "phpfspot_tmpl.php";
6
7 class PHPFSPOT {
8
9    var $cfg;
10    var $db;
11    var $tmpl;
12    var $tags;
13    var $avail_tags;
14    var $photos;
15    var $avail_photos;
16    var $current_photo;
17    var $current_tags;
18
19    public function __construct()
20    {
21       $this->cfg = new PHPFSPOT_CFG;
22
23       $this->db = new PHPFSPOT_DB(&$this, $this->cfg->db);
24       $this->tmpl = new PHPFSPOT_TMPL($this);
25
26       $this->get_tags();
27       $this->get_photos();
28
29       if(isset($_GET['id']) && is_numeric($_GET['id']))
30          $this->current_photo = $_GET['id'];
31
32    } // __construct()
33
34    public function __destruct()
35    {
36
37    } // __destruct()
38
39    public function show()
40    {
41
42       $this->tmpl->assign('tags', $this->tags);
43       $this->tmpl->show("index.tpl");
44
45
46    } // show()
47
48    private function get_tags()
49    {
50       $this->avail_tags = Array();
51       $count = 0;
52    
53       $result = $this->db->db_query("
54          SELECT id,name
55          FROM tags
56          ORDER BY sort_priority ASC
57       ");
58       
59       while($row = $this->db->db_fetch_object($result)) {
60
61          $tag_id = $row['id'];
62          $tag_name = $row['name'];
63
64          $this->tags[$tag_id] = $tag_name; 
65          $this->avail_tags[$count] = $tag_id;
66
67          $count++;
68
69       }
70
71    } // get_tags()
72
73    private function get_photos()
74    {
75       $this->avail_photos = Array();
76       $count = 0;
77
78       $result = $this->db->db_query("
79          SELECT id, name
80          FROM photos
81          ORDER BY time ASC
82       ");
83       
84       while($row = $this->db->db_fetch_object($result)) {
85
86          $photo_id = $row['id'];
87          $photo_name = $row['name'];
88
89          $this->photos[$photo_id] = $photo_name;
90          $this->avail_photos[$count] = $photo_id;
91
92          if(!isset($this->current_photo))
93             $this->current_photo = $count;
94
95          $count++;
96
97       }
98
99    } // get_photos()
100
101    public function get_photo_details($idx)
102    {
103       $result = $this->db->db_query("
104          SELECT *
105          FROM photos
106          WHERE id='". $idx ."'
107       ");
108       
109       return $this->db->db_fetch_object($result);
110
111    } // get_photo_details
112
113    public function translate_path($path, $width = 0)
114    {  
115       return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
116
117    } // translate_path
118
119    public function showPhoto($photo)
120    {
121       $all_photos = $this->getAllTagPhotos();
122
123       foreach($all_photos as $all_photo) {
124          
125          if($get_next) {
126             $next_img = $all_photo;
127             break;
128          }
129
130          if($all_photo == $photo) {
131             $get_next = 1;
132          }
133          else {
134             $previous_img = $all_photo;
135          }
136       }
137
138       if(isset($photo)) {
139          $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
140       }
141
142       if($previous_img) {
143          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
144       }
145
146       if($next_img) {
147          $this->tmpl->assign('next_url', "javascript:showImage(". $next_img .");");
148       }
149
150       $this->tmpl->show("single_photo.tpl");
151
152    } // showPhoto()
153
154    public function getAvailableTags()
155    {
156       foreach($this->avail_tags as $tag)
157       {
158          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
159             continue;
160
161          // return all available (= not selected) tags
162          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
163
164       }
165
166    } // getAvailableTags()
167
168    public function getSelectedTags()
169    {
170       foreach($this->avail_tags as $tag)
171       {
172          // return all selected tags
173          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
174             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
175          }
176
177       }
178
179       print "<a href=\"javascript:Tags('reset', 0);\">Reset Tags</a>";
180
181    } // getSelectedTags()
182
183    public function addTag($tag)
184    {
185       if(!isset($_SESSION['selected_tags']))
186          $_SESSION['selected_tags'] = Array();
187
188       array_push($_SESSION['selected_tags'], $tag);
189    
190    } // addTag()
191
192    public function delTag($tag)
193    {
194       if(isset($_SESSION['selected_tags'])) {
195          $key = array_search($tag, $_SESSION['selected_tags']);
196          unset($_SESSION['selected_tags'][$key]);
197       }
198
199    } // delTag()
200
201    public function resetTags()
202    {
203       unset($_SESSION['selected_tags']);
204
205    } // resetTags()
206
207    public function getAllTagPhotos()
208    {  
209       $tagged_photos = Array();
210
211       if(isset($_SESSION['selected_tags'])) {
212          $selected = "";
213          foreach($_SESSION['selected_tags'] as $tag)
214             $selected.= $tag .",";
215          $selected = substr($selected, 0, strlen($selected)-1);
216          $result = $this->db->db_query("
217             SELECT DISTINCT photo_id
218             FROM photo_tags
219             WHERE tag_id IN (". $selected .")
220          ");
221       }
222       else {
223          $result = $this->db->db_query("
224             SELECT DISTINCT photo_id
225             FROM photo_tags
226          ");
227       }
228
229       while($row = $this->db->db_fetch_object($result)) {
230          array_push($tagged_photos, $row['photo_id']);
231       }
232
233       return $tagged_photos;
234
235    } // getAllTagPhotos()
236
237    public function showPhotoIndex()
238    {
239       $photos = $this->getAllTagPhotos();
240       $count = count($photos);
241
242       $rows = 0;
243       $cols = 0;
244       $images[$rows] = Array();
245
246       for($i = 0; $i < $count; $i++) {
247
248          $images[$rows][$cols] = $photos[$i];
249
250          if($cols == $this->cfg->thumbs_per_row-1) {
251             $cols = 0;
252             $rows++;
253             $images[$rows] = Array();
254          }
255          else {
256             $cols++;
257          }
258       } 
259
260       // +1 for for smarty's selection iteration
261       $rows++;
262
263          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
264
265       $this->tmpl->assign('count', $count);
266       $this->tmpl->assign('width', $this->cfg->thumb_width);
267       $this->tmpl->assign('images', $images);
268       $this->tmpl->assign('rows', $rows);
269       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
270       $this->tmpl->show("photo_index.tpl");
271
272
273    } // showPhotoIndex()
274
275 }
276
277 ?>