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