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