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