1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category DicFro
9 : * @package Model
10 : * @subpackage Query
11 : * @author Michel Corne <mcorne@yahoo.com>
12 : * @copyright 2008-2010 Michel Corne
13 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
14 : * @link http://www.micmap.org/dicfro
15 : */
16 :
17 : require_once 'Model/Query.php';
18 :
19 : /**
20 : * Queries a Godefroy like dictionary database
21 : *
22 : * @category DicFro
23 : * @package Model
24 : * @subpackage Query
25 : * @author Michel Corne <mcorne@yahoo.com>
26 : * @copyright 2008-2010 Michel Corne
27 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
28 : */
29 :
30 : abstract class Model_Query_GdfLike extends Model_Query
31 : {
32 :
33 : /**
34 : * List of additional columns to select, format: ", <column-name>, <column-name>, ..."
35 : * @var string
36 : */
37 : public $extraColumns = '';
38 :
39 : /**
40 : * Goes to the first page of the dictionary
41 : *
42 : * @return array the first two rows
43 : */
44 : public function goToFirstPage()
45 : {
46 3 : return $this->execute("SELECT ascii, image, original {$this->extraColumns} FROM word ORDER BY image ASC LIMIT 2");
47 : }
48 :
49 : /**
50 : * Goes to the last page of the dictionary
51 : *
52 : * @return array the last row
53 : */
54 : public function goToLastPage()
55 : {
56 4 : return $this->execute("SELECT ascii, image, original {$this->extraColumns} FROM word ORDER BY image DESC LIMIT 1");
57 : }
58 :
59 : /**
60 : * Goes to the next page
61 : *
62 : * @param numeric $imageNumber the page from where to go next
63 : * @return array the next two rows, or goes to the last row if none
64 : */
65 : public function goToNextPage($imageNumber)
66 : {
67 3 : $sql = "SELECT ascii, image, original {$this->extraColumns} FROM word WHERE image > :image LIMIT 2";
68 :
69 3 : $result = $this->execute($sql, array(':image' => $imageNumber)) or
70 1 : $result = $this->goToLastPage();
71 :
72 3 : return $result;
73 : }
74 :
75 : /**
76 : * Goes to a given page
77 : *
78 : * @param numeric $imageNumber the page to go to
79 : * @return array the given page row and the next,
80 : * or goes to the first page if before, or to the last page if after
81 : */
82 : public function goToPage($imageNumber)
83 : {
84 3 : $sql = "SELECT ascii, image, original {$this->extraColumns} FROM word WHERE image >= :image LIMIT 2";
85 :
86 3 : $result = $this->execute($sql, array(':image' => $imageNumber)) or
87 1 : $result = $this->goToLastPage();
88 :
89 3 : return $result;
90 : }
91 :
92 : /**
93 : * Goes to the previous page
94 : *
95 : * @param numeric $imageNumber the page from where to go
96 : * @return array the previous row and the current row, or goes to the first page
97 : */
98 : public function goToPreviousPage($imageNumber)
99 : {
100 3 : $sql = "SELECT ascii, image, original {$this->extraColumns} FROM word WHERE image <= :image ORDER BY image DESC LIMIT 2";
101 :
102 3 : $result = array_reverse($this->execute($sql, array(':image' => $imageNumber))) and
103 3 : count($result) >= 2 or
104 1 : $result = $this->goToFirstPage();
105 :
106 3 : return $result;
107 : }
108 :
109 : /**
110 : * Searches a word
111 : *
112 : * @param string $word the word in UTF-8
113 : * @return array two rows including the page where the word is or would be located
114 : */
115 : public function searchWord($word)
116 : {
117 4 : $ascii = $this->string->utf8toASCII($word);
118 :
119 4 : $sql = "SELECT ascii, image, original, previous {$this->extraColumns} FROM word WHERE ascii >= :ascii LIMIT 2";
120 :
121 4 : if ($result = $this->execute($sql, array(':ascii' => $ascii))) {
122 : // found same or next word, searches previous word if different
123 : // note: need to find the first occurence of the previous word, ex. first page with "A"
124 : // note: previous is empty if this is the first page
125 4 : $result[0]['ascii'] == $ascii or empty($result[0]['previous']) or
126 1 : $result = $this->execute($sql, array(':ascii' => $result[0]['previous']));
127 :
128 : } else {
129 : // no word found, returns the last one
130 1 : $result = $this->goToLastPage();
131 : }
132 :
133 4 : return $result;
134 : }
|