dbefee8b37f52ec13bd2bb61cf7529df3fa3d8cf
[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       if(isset($photo)) {
122          $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
123       }
124
125       if($photo > 0) {
126          $this->tmpl->assign('previous_url', "javascript:showImage(". ($photo-1) .");");
127       }
128
129       if($photo < count($this->photos)) {
130          $this->tmpl->assign('next_url', "javascript:showImage(". ($photo+1) .");");
131       }
132
133       $this->tmpl->show("single_photo.tpl");
134
135    } // showPhoto()
136
137    public function getAvailableTags()
138    {
139       foreach($this->avail_tags as $tag)
140       {
141          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags']))
142             continue;
143
144          // return all available (= not selected) tags
145          print "<a href=\"javascript:Tags('add', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
146
147       }
148
149    } // getAvailableTags()
150
151    public function getSelectedTags()
152    {
153       foreach($this->avail_tags as $tag)
154       {
155          // return all selected tags
156          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
157             print "<a href=\"javascript:Tags('del', ". $tag .");\">". $this->tags[$tag] ."</a>&nbsp;";
158          }
159
160       }
161
162       print "<a href=\"javascript:Tags('reset', 0);\">Reset Tags</a>";
163
164    } // getSelectedTags()
165
166    public function addTag($tag)
167    {
168       if(!isset($_SESSION['selected_tags']))
169          $_SESSION['selected_tags'] = Array();
170
171       array_push($_SESSION['selected_tags'], $tag);
172    
173    } // addTag()
174
175    public function delTag($tag)
176    {
177       if(isset($_SESSION['selected_tags'])) {
178          $key = array_search($tag, $_SESSION['selected_tags']);
179          unset($_SESSION['selected_tags'][$key]);
180       }
181
182    } // delTag()
183
184    public function resetTags()
185    {
186       unset($_SESSION['selected_tags']);
187
188    } // resetTags()
189
190    public function getAllTagPhotos()
191    {  
192       $tagged_photos = Array();
193
194       if(isset($_SESSION['selected_tags'])) {
195          $selected = "";
196          foreach($_SESSION['selected_tags'] as $tag)
197             $selected.= $tag .",";
198          $selected = substr($selected, 0, strlen($selected)-1);
199          $result = $this->db->db_query("
200             SELECT DISTINCT photo_id
201             FROM photo_tags
202             WHERE tag_id IN (". $selected .")
203          ");
204       }
205       else {
206          $result = $this->db->db_query("
207             SELECT DISTINCT photo_id
208             FROM photo_tags
209          ");
210       }
211
212       while($row = $this->db->db_fetch_object($result)) {
213          array_push($tagged_photos, $row['photo_id']);
214       }
215
216       return $tagged_photos;
217
218    } // getAllTagPhotos()
219
220    public function showPhotoIndex()
221    {
222       $photos = $this->getAllTagPhotos();
223       $count = count($photos);
224
225       $rows = 0;
226       $cols = 0;
227       $images[$rows] = Array();
228
229       for($i = 0; $i < $count; $i++) {
230
231          $images[$rows][$cols] = $photos[$i];
232
233          if($cols == $this->cfg->thumbs_per_row-1) {
234             $cols = 0;
235             $rows++;
236             $images[$rows] = Array();
237          }
238          else {
239             $cols++;
240          }
241       } 
242
243       // +1 for for smarty's selection iteration
244       $rows++;
245
246          //$images.= "<img src=\"phpfspot_img.php?idx=". $photo ."&amp;width=". $this->cfg->thumb_width ."\" /><br />\n";
247
248       $this->tmpl->assign('count', $count);
249       $this->tmpl->assign('width', $this->cfg->thumb_width);
250       $this->tmpl->assign('images', $images);
251       $this->tmpl->assign('rows', $rows);
252       $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
253       $this->tmpl->show("photo_index.tpl");
254
255
256    } // showPhotoIndex()
257
258 }
259
260 ?>