4 * Ajatus - Distributed CRM
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
17 private $configuration;
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]))
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
44 // Otherwise bubble up
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}";
66 public function tag_to_id($tag)
72 list ($tag, $context, $value) = $this->tag_parts($tag);
73 $res = $this->get_tag($tag, $context, $value);
81 public function ids_to_tags(array $ids)
86 $ret[$id] = $this->id_to_tag($id);
91 public function tag_parts($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];
120 $vparts = explode('=', $tag);
121 if (is_array($vparts)
122 && count($vparts) === 2)
128 return array($tag, $context, $value);
131 public function tags_to_id(array $tags)
135 foreach ($tags as $k => $tag)
137 $id = $this->tag_to_id($tag);
148 public function get_tag($name, $context='', $value='')
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}' ";
162 $view .= "&& doc.value.title.widget.config.value == '{$value}' ";
167 $view .= 'map( doc._id, {';
168 $view .= ajatus_types
::prepare_map_values($GLOBALS['ajatus']->types
->tag
->map_values
['list']);
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];
186 public function related(array $tags, array $skip_docs=array(), array $map_values=array(), $include_archived=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))
204 $sd_cnt = count($skip_docs);
205 for ($i=0; $i<$sd_cnt; $i++
)
207 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
216 $view .= 'var hm = false;';
217 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
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)
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')))
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+',';
259 $view .= '"tags": doc.value.tags });';
262 if (! empty($skip_docs))
269 // echo "Builded view: \n{$view}\n";
271 $content_db = $this->configuration
['content_db'];
272 return $this->connection
->$content_db->view
->temp($view);