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