BugFix : un message d'erreur s'affiche dans le titre de la page de resultat
[bazdig.git] / lib / mark.php
blobe0a1641a66c5410aa445874318c9bbce473d6f3a
1 <?php
2 require_once 'persistance.php';
4 class Mark implements sql, persistance
6 static $db;
8 var $id;
9 var $creationDate;
10 var $pageUrl;
11 var $text;
12 var $startNodePath, $startOffset;
13 var $endNodePath, $endOffset;
14 var $owner;
16 function __construct($id = NULL)
18 $this->creationDate = time();
19 if ($id) {
20 $this->id = $id;
24 static function get_table_name()
26 return "marks";
29 static function sql_select($wildcards = NULL)
31 $tableName = self::get_table_name();
33 $order = ' order by creationDate desc, pageUrl ';
34 if (array_key_exists('order', $wildcards)) {
35 $order = " order by ". $wildcards['order'];
36 unset($wildcards['order']);
39 $limit = '';
40 if (array_key_exists('limit', $wildcards)) {
41 $limit = " limit ". $wildcards['limit'];
42 unset($wildcards['limit']);
45 if (empty($wildcards)) {
46 return "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName $order $limit;";
49 $tuples = array();
50 if (array_key_exists('pageUrl', $wildcards)) {
51 $tuples []= " pageUrl LIKE '". $wildcards['pageUrl'] ."%'";
52 unset($wildcards['pageUrl']);
55 foreach ($wildcards as $key => $value) {
56 $tuples []= "$key='$value'";
59 $conditions = implode(' and ', $tuples);
61 $query = "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName where $conditions $order $limit;";
63 return $query;
66 function toHTMLanchor()
68 $anchor = "<a href='". $this->pageUrl ."' >". $this->text ."</a>";
69 return $anchor;
72 function toSQLinsert()
74 $id = $this->id;
75 $creationDate = date('c', $this->creationDate);
76 $pageUrl = $this->pageUrl;
77 $text = $this->text;
78 $owner = $this->owner;
79 $startNodePath = $this->startNodePath;
80 $startOffset = $this->startOffset;
81 $endNodePath = $this->endNodePath;
82 $endOffset = $this->endOffset;
84 $tableName = Mark::get_table_name();
85 return "INSERT into $tableName (id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset) values ('$id', '$creationDate', '$pageUrl', '$text', '$owner', '$startNodePath', '$startOffset', '$endNodePath', '$endOffset');";
88 function toSQLselect()
90 return "SELECT creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from marks where id='". $this->id ."';";
93 static function set_db($db, $user = "", $password = "")
95 if ($db instanceof PDO) {
96 self::$db =& $db;
97 } else {
98 if (empty($user)) {
99 self::$db =& new PDO($db);
100 } else {
101 self::$db =& new PDO($db, $user, $password);
105 return self::$db;
108 static function count($wildcards = NULL, $db = NULL)
110 if ($db == NULL) {
111 $db = self::$db;
113 if (! $db instanceof PDO) {
114 throw new Exception("Not a Data Base");
117 $tableName = Mark::get_table_name();
118 $query = "select count(*) from $tableName";
119 if ($result = $db->query($query)) {
120 $result = $result->fetchAll();
121 } else {
122 echo "Query error";
123 print_r($db->errorInfo());
124 throw new Exception("Query error");
127 return $result[0][0];
130 static function select($wildcards = NULL, $db = NULL)
132 if ($db == NULL) {
133 $db = self::$db;
135 if (! $db instanceof PDO) {
136 throw new Exception("Not a Data Base");
138 $marks = array();
139 $query = self::sql_select($wildcards);
140 if ($result = $db->query($query)) {
141 $result = $result->fetchAll();
142 } else {
143 echo "Query error";
144 print_r($db->errorInfo());
145 throw new Exception("Query error");
147 foreach ($result as $r) {
148 $mark = new Mark($r['id']);
149 $mark->creationDate = strtotime($r['creationDate']);
150 $mark->pageUrl = strip_http_get_params($r['pageUrl']);
151 $mark->text = $r['text'];
152 $mark->owner = $r['owner'];
153 $mark->startNodePath = json_decode($r['startNodePath']);
154 $mark->startOffset = $r['startOffset'];
155 $mark->endNodePath = json_decode($r['endNodePath']);
156 $mark->endOffset = $r['endOffset'];
157 array_push( $marks, $mark);
160 return $marks;
163 function save($db = NULL)
165 if ($db == NULL) {
166 $db = self::$db;
168 $query = $this->toSQLinsert();
170 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
171 $statement = $db->query($query);
173 return $statement;
176 function load($db = NULL)
178 if ($db == NULL) {
179 $db = self::$db;
181 if (is_string($db)) {
182 $db = new PDO($db, $user, $pass);
184 $query = $this->toSQLselect();
185 if ($result = $db->query($query)) {
186 $result = $result->fetch();
187 } else {
188 throw new Exception("Query error");
191 $this->creationDate = strtotime($result['creationDate']);
192 $this->pageUrl = $result['pageUrl'];
193 $this->text = $result['text'];
194 $this->owner = $result['owner'];
195 $this->startNodePath = $result['startNodePath'];
196 $this->startOffset = $result['startOffset'];
197 $this->endNodePath = $result['endNodePath'];
198 $this->endOffset = $result['endOffset'];
200 return $this;
203 function __toString()
205 return json_encode($this);
209 function strip_http_get_params($url)
211 if (!$urlBaseLength = strpos($url, '?')) return $url;
212 return substr($url, 0, $urlBaseLength);