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