|
| 1 | +<?php |
| 2 | +/// Copyright (c) 2004-2009, Needlworks / Tatter Network Foundation |
| 3 | +/// All rights reserved. Licensed under the GPL. |
| 4 | +/// See the GNU General Public License for more details. (/doc/LICENSE, /doc/COPYRIGHT) |
| 5 | + |
| 6 | +final class Line extends Singleton { |
| 7 | + private $filter = array(); |
| 8 | + public function __constructor() { |
| 9 | + $this->reset(); |
| 10 | + } |
| 11 | + |
| 12 | + public static function getInstance() { |
| 13 | + return self::_getInstance(__CLASS__); |
| 14 | + } |
| 15 | + |
| 16 | + public function reset() { |
| 17 | + global $database; |
| 18 | + $this->id = null; |
| 19 | + $this->blogid = getBlogId(); |
| 20 | + $this->category = 'public'; |
| 21 | + $this->content = ''; |
| 22 | + $this->created = null; |
| 23 | + $this->filter = array(); |
| 24 | + $this->_error = array(); |
| 25 | + $query = new TableQuery($database['prefix'].'Lines'); |
| 26 | + } |
| 27 | +/// Methods for managing |
| 28 | + public function add() { |
| 29 | + global $database; |
| 30 | + if(is_null($this->created)) $this->created = Timestamp::getUNIXTime(); |
| 31 | + if(!$this->validate()) return false; |
| 32 | + $query = new TableQuery($database['prefix'].'Lines'); |
| 33 | + $query->setAttribute('id',$this->id); |
| 34 | + $query->setAttribute('blogid',$this->blogid); |
| 35 | + $query->setAttribute('category',$this->category,true); |
| 36 | + $query->setAttribute('content',$this->content,true); |
| 37 | + $query->setAttribute('created',$this->created); |
| 38 | + return $query->insert(); |
| 39 | + } |
| 40 | + |
| 41 | + public function delete(){ |
| 42 | + global $database; |
| 43 | + if(empty($this->filter)) return $this->error('Filter empty'); |
| 44 | + $query = new TableQuery($database['prefix'].'Lines'); |
| 45 | + foreach($this->filter as $filter) { |
| 46 | + if(count($filter) == 3) { |
| 47 | + $query->setQualifier($filter[0],$filter[1],$filter[2]); |
| 48 | + } else { |
| 49 | + $query->setQualifier($filter[0],$filter[1],$filter[2],$filter[3]); |
| 50 | + } |
| 51 | + } |
| 52 | + return $query->delete(); |
| 53 | + } |
| 54 | +/// Methods for querying |
| 55 | + public function get($fields = '*') { |
| 56 | + global $database; |
| 57 | + if(empty($this->filter)) return $this->error('Filter empty'); |
| 58 | + $query = new TableQuery($database['prefix'].'Lines'); |
| 59 | + foreach($this->filter as $filter) { |
| 60 | + if(count($filter) == 3) { |
| 61 | + $query->setQualifier($filter[0],$filter[1],$filter[2]); |
| 62 | + } else { |
| 63 | + $query->setQualifier($filter[0],$filter[1],$filter[2],$filter[3]); |
| 64 | + } |
| 65 | + } |
| 66 | + $query->setOrder('created','desc'); |
| 67 | + return $query->getAll($fields); |
| 68 | + } |
| 69 | + |
| 70 | + /// @input condition<array> [array(name, condition, value, [need_escaping])] |
| 71 | + public function setFilter($condition) { |
| 72 | + if(!in_array(count($condition),array(3,4))) return $this->error('wrong filter'); |
| 73 | + array_push($this->filter, $condition); |
| 74 | + } |
| 75 | + |
| 76 | +/// Aliases |
| 77 | + public function getWithConditions($conditions) { |
| 78 | + |
| 79 | + } |
| 80 | +/// Private members |
| 81 | + private function validate() { |
| 82 | + if(is_null($this->id)) $this->id = $this->getNextId(); |
| 83 | + $this->category = UTF8::lessenAsByte($this->category, 11); |
| 84 | + $this->content = UTF8::lessenAsByte($this->content, 512); |
| 85 | + if(!Validator::isInteger($this->blogid, 1)) return $this->error('blogid'); |
| 86 | + if(!Validator::timestamp($this->created)) return $this->error('created'); |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + private function getNextId() { |
| 91 | + global $database; |
| 92 | + $query = new TableQuery($database['prefix'].'Lines'); |
| 93 | + $maxId = $query->getCell('MAX(id)'); |
| 94 | + if(!empty($maxId)) return $maxId + 1; |
| 95 | + else return 1; |
| 96 | + } |
| 97 | + public function showResult($result) { |
| 98 | + echo "<html><head></head><body>"; |
| 99 | + echo '<script type="text/javascript">alert("'; |
| 100 | + if($result) { |
| 101 | + echo _t('Line이 추가되었습니다.'); |
| 102 | + } else { |
| 103 | + echo _t('Line 추가에 실패했습니다.'); |
| 104 | + } |
| 105 | + echo '");history.back(-1);</script></body></html>'; |
| 106 | + } |
| 107 | + private function error($state) { |
| 108 | + $this->_error['message'] = $state; |
| 109 | + return false; |
| 110 | + } |
| 111 | +} |
| 112 | +?> |
0 commit comments