5 * Copyright 2004 (c) Dominik Haas, GForge Team
12 require_once $gfcommon.'search/SearchQuery.class.php';
14 class DocsSearchQuery extends SearchQuery {
24 * flag if non public items are returned
26 * @var boolean $showNonPublic
33 * @param string $words words we are searching for
34 * @param int $offset offset
35 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
36 * @param int $groupId group id
37 * @param array $sections sections to search in
38 * @param boolean $showNonPublic flag if private sections are searched too
40 function DocsSearchQuery($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
41 $this->groupId = $groupId;
42 $this->showNonPublic = $showNonPublic;
44 $this->SearchQuery($words, $offset, $isExact);
46 $this->setSections($sections);
50 * getQuery - get the sql query built to get the search results
52 * @return string sql query to execute
57 return $this->getFTIQuery();
59 $sql = 'SELECT doc_data.docid, doc_data.title, doc_data.description, doc_groups.groupname'
60 .' FROM doc_data, doc_groups'
61 .' WHERE doc_data.doc_group = doc_groups.doc_group'
62 .' AND doc_data.group_id ='.$this->groupId;
63 if ($this->sections != SEARCH__ALL_SECTIONS) {
64 $sql .= ' AND doc_groups.doc_group IN ('.$this->sections.') ';
66 if ($this->showNonPublic) {
67 $sql .= ' AND doc_data.stateid IN (1, 4, 5)';
69 $sql .= ' AND doc_data.stateid = 1';
71 $sql .= ' AND (('.$this->getIlikeCondition('title', $this->words).')'
72 .' OR ('.$this->getIlikeCondition('description', $this->words).'))'
73 .' ORDER BY doc_groups.groupname, doc_data.docid';
78 function getFTIQuery() {
79 if ($this->showNonPublic) {
80 $nonPublic = "1, 4, 5";
84 if ($this->sections != SEARCH__ALL_SECTIONS) {
85 $sections = "AND doc_groups.doc_group IN ($this->sections)";
89 $words = $this->getFormattedWords();
90 $group_id=$this->groupId;
92 if(count($this->words)) {
93 $tsquery0 = "headline(doc_data.title, q) AS title, headline(doc_data.description, q) AS description";
94 $tsquery = ", doc_data_idx, to_tsquery('".$words()."') q";
95 $tsmatch = "vectors @@ q";
97 $tsjoin = 'AND doc_data.docid = doc_data_idx.docid ';
98 $orderBy = "ORDER BY rank(vectors, q) DESC, groupname ASC";
99 $phraseOp = $this->getOperator();
101 $tsquery0 = "title, description";
106 $orderBy = "ORDER BY groupname";
111 if(count($this->phrases)) {
112 $titleCond = $this->getMatchCond('title', $this->phrases);
113 $descCond = $this->getMatchCond('description', $this->phrases);
114 $phraseCond = $phraseOp.' (('.$titleCond.') OR ('.$descCond.'))';
117 $sql="SELECT doc_data.docid, $tsquery0, doc_groups.groupname
118 FROM doc_data, doc_groups $tsquery
119 WHERE doc_data.doc_group = doc_groups.doc_group
120 $tsjoin AND ($tsmatch $phraseCond )
121 AND doc_data.group_id = '$group_id'
123 AND doc_data.stateid IN ($nonPublic)
129 * getSections - returns the list of available doc groups
131 * @param $groupId int group id
132 * @param $showNonPublic boolean if we should consider non public sections
134 function getSections($groupId, $showNonPublic=false) {
135 $sql = 'SELECT doc_groups.doc_group, doc_groups.groupname FROM doc_groups, doc_data'
136 .' WHERE doc_groups.doc_group = doc_data.doc_group AND doc_groups.group_id = '.$groupId;
137 if ($showNonPublic) {
138 $sql .= ' AND doc_data.stateid IN (1, 4, 5)';
140 $sql .= ' AND doc_data.stateid = 1';
142 $sql .= ' ORDER BY groupname';
145 $res = db_query($sql);
146 while($data = db_fetch_array($res)) {
147 $sections[$data['doc_group']] = $data['groupname'];