Make the tag result list scrollable so long tags can be read
[ajatus.git] / plugins / ajatus / helpers / tag.php
blob4d2c96daf6b98950c6151018ccc20d27169544be
1 <?php
2 /**
3 * This file is part of
4 * Ajatus - Distributed CRM
5 *
6 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
7 * Copyright (c) 2008 Nemein Oy <http://nemein.com>
8 * Website: http://ajatus.info
9 * Licensed under the GPL license
10 * http://www.gnu.org/licenses/gpl.html
14 class ajatus_helpers_tag
16 private $connection;
17 private $configuration;
18 private $cache;
20 public function __construct(&$connection, &$configuration)
22 $this->connection =& $connection;
23 $this->configuration =& $configuration;
26 public function id_to_tag($id)
28 static $cache = array();
29 if (isset($cache[$id]))
31 return $cache[$id];
33 try
35 $tag_object = $GLOBALS['ajatus']->types->tag->get_object($id)->value;
37 catch (couchdb_document_exception $e)
39 if ($e->getCode() == 404)
41 // document does not exist
42 return false;
44 // Otherwise bubble up
45 throw $e;
48 $ret = $tag_object->title->val;
49 if ( isset($tag_object->title->widget->config->context)
50 && !empty($tag_object->title->widget->config->context))
52 $ret = "{$tag_object->title->widget->config->context}:{$tag_object->title->val}";
54 if ( isset($tag_object->title->widget->config->value)
55 && !empty($tag_object->title->widget->config->value))
57 $ret .= "={$tag_object->title->widget->config->value}";
59 if (!empty($ret))
61 $cache[$id] = $ret;
63 return $ret;
66 public function tag_to_id($tag)
68 if (is_object($tag))
70 return $tag->_id;
72 list ($tag, $context, $value) = $this->tag_parts($tag);
73 $res = $this->get_tag($tag, $context, $value);
74 if ($res)
76 return $res->id;
78 return false;
81 public function ids_to_tags(array $ids)
83 $ret = array();
84 foreach ($ids as $id)
86 $ret[$id] = $this->id_to_tag($id);
88 return $ret;
91 public function tag_parts($tag)
93 $context = '';
94 $value = '';
95 if (is_object($tag))
97 // Already a document, take the parts.
98 $tag_object = $tag->value;
99 $tag = $tag_object->title->val;
100 if ( isset($tag_object->title->widget->config->context)
101 && !empty($tag_object->title->widget->config->context))
103 $context = $tag_object->title->widget->config->context;
105 if ( isset($tag_object->title->widget->config->value)
106 && !empty($tag_object->title->widget->config->value))
108 $value = $tag_object->title->widget->config->value;
110 return array($tag, $context, $value);
113 $cparts = explode(':', $tag);
114 if ( is_array($cparts)
115 && count($cparts) === 2)
117 $context = $cparts[0];
118 $tag = $cparts[1];
120 $vparts = explode('=', $tag);
121 if (is_array($vparts)
122 && count($vparts) === 2)
124 $value = $vparts[1];
125 $tag = $vparts[0];
128 return array($tag, $context, $value);
131 public function tags_to_id(array $tags)
133 $results = array();
135 foreach ($tags as $k => $tag)
137 $id = $this->tag_to_id($tag);
138 if ($id === false)
140 continue;
142 $results[$k] = $id;
145 return $results;
148 public function get_tag($name, $context='', $value='')
150 $result = false;
152 $view = 'function(doc){';
153 $view .= "if (doc.value._type == 'tag' && doc.value.title.val == '{$name}' ";
155 if (! empty($context))
157 $view .= "&& doc.value.title.widget.config.context == '{$context}' ";
160 if (! empty($value))
162 $view .= "&& doc.value.title.widget.config.value == '{$value}' ";
165 $view .= ') {';
167 $view .= 'map( doc._id, {';
168 $view .= ajatus_types::prepare_map_values($GLOBALS['ajatus']->types->tag->map_values['list']);
169 $view .= '});';
171 $view .= '}}';
173 // echo "Builded view: \n{$view}\n";
175 $content_db = $this->configuration['content_db'];
176 $results = $this->connection->$content_db->view->temp($view);
178 if ($results->total_rows > 0)
180 $result = $results->rows[0];
183 return $result;
186 public function related(array $tags, array $skip_docs=array(), array $map_values=array(), $include_archived=false)
188 if (empty($tags))
190 return false;
193 $view = 'function(doc){';
194 $view .= 'if (typeof(doc.value.tags) != "undefined" && doc.value.tags.val.length > 0';
195 if (!$include_archived)
197 $view .= ' && doc.value.metadata.archived.val == false';
199 $view .= ' && doc.value.metadata.deleted.val == false){';
201 if (! empty($skip_docs))
203 $view .= 'if (';
204 $sd_cnt = count($skip_docs);
205 for ($i=0; $i<$sd_cnt; $i++)
207 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
208 if ($i < $sd_cnt-1)
210 $view .= '&& ';
213 $view .= ') {';
216 $view .= 'var hm = false;';
217 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
218 $view .= 'if (';
220 $tags_cnt = count($tags);
221 for ($i=0; $i<$tags_cnt; $i++)
223 $view .= "doc.value.tags.val[i] == '{$tags[$i]}' ";
224 if ($i < $tags_cnt-1)
226 $view .= '|| ';
230 $view .= ') {';
231 $view .= 'hm = true;}';
232 $view .= '} if (hm) {';
233 $view .= 'map( doc.value._type, {"_id": doc._id, "_rev": doc._rev, "_type": doc.value._type,';
234 $view .= '"title": doc.value.title,';
236 if (! empty($map_values))
238 foreach ($map_values as $key => $value)
240 if (in_array($key, array('title', 'tags')))
242 continue;
245 $view .= json_encode($key) . ": {$value},";
249 // $.each($.ajatus.preferences.client.content_types, function(key,type){
250 // if (type['title_item']) {
251 // $.each(type['title_item'], function(i,part){
252 // if (type.schema[part]) {
253 // rel_view += '"'+part+'": doc.value.'+part+',';
254 // }
255 // });
256 // }
257 // });
259 $view .= '"tags": doc.value.tags });';
260 $view .= '}}';
262 if (! empty($skip_docs))
264 $view .= '}';
267 $view .= '}';
269 // echo "Builded view: \n{$view}\n";
271 $content_db = $this->configuration['content_db'];
272 return $this->connection->$content_db->view->temp($view);