2 require_once('includes.inc.php');
3 require_once('util.inc.php');
6 * Handles information from database
15 public function __construct($namespace)
18 $this->db = SingletonDB::connect();
20 // Get languages from DB.
21 if(!$this->db->query("SET NAMES 'utf8'")) throw new DBException('query');
22 if( !($result = $this->db->query("SELECT * FROM `languages`")) )
23 throw new DBException('query');
24 while($row = $result->fetch_assoc())
26 $this->languages[$row['code']]['name'] = $row['name'];
27 $this->languages[$row['code']]['img'] = $row['image'];
30 // Set current language
32 $matchedLang = DEFAULT_LANG;//$this->matchLanguage();
33 if(isset($_GET['lang']))
35 $lang = $this->processLangCode($_GET['lang']);
37 if($lang != $matchedLang)
38 setcookie("lang", $lang, time() + 30*24*60*60);
40 setcookie("lang", $lang, time() - 3600);
42 else if(isset($_COOKIE['lang']))
43 $this->lang = $this->processLangCode($_COOKIE['lang']);
45 $this->lang = $matchedLang;
47 // Get content from DB, from common and specified namespace.
48 if( !($result = $this->db->query("SELECT * FROM `content` WHERE namespace = 'common' OR namespace = '"
49 . $namespace . "'")) )
50 throw new DBException('query');
51 while($row = $result->fetch_assoc())
53 $this->content[$row['name']] = json_decode($row['value'], true);
57 catch(DBException $e) {
58 echo $e->errorMessage();
63 // Verifies if the lang code exists in the database and if so it returns it
64 // in lower case. It not it returns the default language.
65 public function processLangCode($lang)
67 $lang = strtolower($lang);
68 if(isset($this->languages[$lang]))
74 public function getLanguages()
76 return $this->languages;
79 public function getLanguage($code)
81 if(isset($this->languages[$code]))
82 return $this->languages[$code];
87 public function getCrtLanguage()
92 public function getContent($name, $lang)
94 if(isset($this->content[$name]))
95 return $this->content[$name][$lang];
100 public function __get($name)
102 if(isset($this->content[$name]))
103 return $this->content[$name][$this->lang];
108 // Returns user's prefered language if the site supports it or english otherwise.
109 public function matchLanguage()
111 $pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
112 '(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
113 '(?P<quantifier>\d\.\d))?$/';
115 foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang)
119 if (preg_match($pattern, $lang, $splits))
121 $language = $splits['primarytag'];
122 if(isset($this->languages[$language]))
132 public function isRowIncomplete($row)
136 if(empty($row['name']))
138 if(empty($row['address']))
140 if(empty($row['developer']))
142 if(empty($row['chief_architect']))
143 $incomplete .= '{ca}';
144 if($row['whole_area'] == null || $row['whole_area'] == '0000')
145 $incomplete .= '{wa}';
146 if(empty($row['height']))
148 if(empty($row['phase']))
149 $incomplete .= '{ph}';
150 if($row['year_begin'] == null)
151 $incomplete .= '{yb}';
153 $descriptions = json_decode($row['description'], true);
154 foreach($this->getLanguages() as $code => $value)
156 if(empty($descriptions[$code]))
157 $incomplete .= '{de}';
160 $images = json_decode($row['images'], true);
161 if(count($images) < 1)
167 // Returns a list of buildings with a specified category as an
168 // associative array with ID keys. Every element has associated:
169 // name, year_begin, image (thumbnail).
170 public function getPortofolioSummary($category, $page, $checkIncomplete=false)
174 if(!$checkIncomplete)
175 $columns = "id, name, year_begin, images, category";
179 $query = "SELECT ". $columns. " FROM `portofolio`";
180 if($category != null)
181 $query .= " WHERE category = '". $category . "'";
182 $query .= " ORDER BY year_begin DESC, name ASC";
184 $query .= " LIMIT ". ($page * PRJ_PER_PAGE) . ", " . PRJ_PER_PAGE;
185 $result = $this->db->query($query);
187 while($row = $result->fetch_assoc())
189 $portofolio[$row['id']]['name'] = $row['name'];
190 $portofolio[$row['id']]['year_begin'] = $row['year_begin'];
191 $images = json_decode($row['images'], true);
193 $portofolio[$row['id']]['image'] = null;
195 $portofolio[$row['id']]['image'] = getThumbFileName($images[0]);
196 $portofolio[$row['id']]['category'] = $row['category'];
199 $portofolio[$row['id']]['incomplete'] = $this->isRowIncomplete($row);
205 // Returns 3 dictionaries representing projects from portofolio.
206 // Each dictionary has id as key and another dictionary as value with:
207 // name, year_begin, description, thumnail.
208 public function getPortofolioAds()
212 $query = "SELECT id, name, year_begin, description, images FROM `portofolio` ORDER BY RAND() LIMIT "
214 $result = $this->db->query($query);
216 while($row = $result->fetch_assoc())
218 $portofolio[$row['id']]['name'] = $row['name'];
219 $portofolio[$row['id']]['year_begin'] = $row['year_begin'];
220 $images = json_decode($row['images'], true);
222 $portofolio[$row['id']]['image'] = null;
224 $portofolio[$row['id']]['image'] = getThumbFileName($images[0]);
225 $descriptions = json_decode($row['description'], true);
226 $portofolio[$row['id']]['description'] = $descriptions[$this->lang];
232 public function getPortofolioCardinality($category)
234 $query = "SELECT count(id) c FROM `portofolio` WHERE category = '". $category. "'";
235 $result = $this->db->query($query);
237 $row = $result->fetch_row();
242 public function getRowNumber($id, $category)
245 SELECT id id_t, name name_t, year_begin year_begin_t, (
249 WHERE category = '". $category. "'
251 AND (year_begin = year_begin_t AND name < name_t OR year_begin > year_begin_t)
252 ORDER BY year_begin DESC , name ASC
256 $result = $this->db->query($query);
258 $row = $result->fetch_assoc();
263 // If fullInfo is true, information for all languages will be returned.
264 public function getProject($id, $fullInfo = false)
266 $result = $this->db->query(
267 "SELECT * from `portofolio` WHERE id = " . $id);
268 $row = $result->fetch_assoc();
273 $row['category_code'] = $row['category'];
274 switch($row['category_code'])
277 $row['category'] = $this->project_category_office;
280 $row['category'] = $this->project_category_residential;
283 $row['category'] = $this->project_category_hotel;
286 $row['category'] = $this->project_category_industrial;
289 $row['category'] = $this->project_category_commercial;
292 $row['category'] = $this->project_category_restorations;
295 $row['category'] = $this->project_category_category;
298 $row['category'] = $this->project_category_other;
302 // Translate NULL values.
303 if(!isset($project['whole_area']) || $project['whole_area'] == null)
304 $project['whole_area'] = '';
305 if(!isset($project['year_begin']) || $project['year_begin'] == null)
306 $project['year_begin'] = '';
307 if(!isset($project['year_end']) || $project['year_end'] == null)
308 $project['year_end'] = '';
310 $descriptions = json_decode($row['description'], true);
311 $row['description'] = $descriptions[$this->lang];
313 $row['descriptions'] = $descriptions;
315 $images = json_decode($row['images'], true);
316 $row['images'] = $images;
321 public static function insertProject($project)
324 $db = SingletonDB::connect();
326 $db->query("SET NAMES 'utf8'");
328 $project['name'] = str_replace("'", "`", $project['name']);
329 $project['description'] = json_encode(str_replace("'", "`", $project['descriptions']));
330 $project['images'] = json_encode($project['images']);
332 $result = $db->query(
333 "INSERT INTO `portofolio` (name, category, address, developer, chief_architect, whole_area, height, phase, year_begin, year_end, description, images)
334 VALUES ('". $project['name']. "', '". $project['category_code']. "', '". $project['address']. "', '". $project['developer']. "', '". $project['chief_architect']. "', ". $project['whole_area']. ", '". $project['height']. "', '". $project['phase']. "', ". $project['year_begin']. ", ". $project['year_end']. ", '". $project['description']. "', '". $project['images']. "')");
337 throw new DBException('query');
339 catch(DBException $e) {
340 echo $e->errorMessage();
345 public static function updateProject($id, $project)
348 $db = SingletonDB::connect();
350 $db->query("SET NAMES 'utf8'");
352 $project['name'] = str_replace("'", "`", $project['name']);
353 $project['description'] = json_encode(str_replace("'", "`", $project['descriptions']));
354 $project['images'] = json_encode($project['images']);
356 $query = "UPDATE `portofolio` SET name='". $project['name']. "', category='". $project['category_code']. "', address='". $project['address']. "', developer='". $project['developer']. "', chief_architect='". $project['chief_architect']. "', whole_area=". $project['whole_area']. ", height='". $project['height']. "', phase='". $project['phase']. "', year_begin=". $project['year_begin']. ", year_end=". $project['year_end']. ", description='". $project['description']. "', images='". $project['images']. "' WHERE id=". $id;
357 $result = $db->query($query);
360 throw new DBException('query');
362 catch(DBException $e) {
363 echo $e->errorMessage();
368 public static function deleteProject($id, $project)
370 foreach($project['images'] as $image)
376 $db = SingletonDB::connect();
378 $query = "DELETE FROM `portofolio` WHERE id=". $id;
379 $result = $db->query($query);
382 throw new DBException('query');
384 catch(DBException $e) {
385 echo $e->errorMessage();
391 //$x = new Content('common');
392 //echo $x->getRowNumber(56, 'residential');