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)
31 public function tag_to_id($tag)
36 public function ids_to_tags(array $ids)
41 public function tags_to_id(array $tags)
45 foreach ($tags as $k => $tag)
52 $cparts = explode(':', $tag);
53 if (is_array($cparts))
55 $context = $cparts[0];
58 $vparts = explode('=', $tag);
59 if (is_array($vparts))
65 $res = $this->get_tag($tag, $context, $value);
68 $results[$k] = $res->id
;
73 $results[$k] = $tag->_id
;
80 public function get_tag($name, $context='', $value='')
84 $view = 'function(doc){';
85 $view .= "if (doc.value._type == 'tag' && doc.value.title.val == '{$name}' ";
87 if (! is_null($context))
89 $view .= "&& doc.value.title.widget.config.context == '{$context}' ";
92 if (! is_null($value))
94 $view .= "&& doc.value.title.widget.config.value == '{$value}' ";
99 $view .= 'map( doc._id, {';
100 $view .= ajatus_types
::prepare_map_values($GLOBALS['ajatus']->types
->tag
->map_values
['list']);
105 // echo "Builded view: \n{$view}\n";
107 $content_db = $this->configuration
['content_db'];
108 $results = $this->connection
->$content_db->view
->temp($view);
110 if ($results->total_rows
> 0)
112 $result = $results->rows
[0];
118 public function related(array $tags, array $skip_docs=array(), array $map_values=array())
125 $view = 'function(doc){';
126 $view .= 'if (typeof(doc.value.tags) != "undefined" && doc.value.tags.val.length > 0 ';
127 $view .= '&& doc.value.metadata.archived.val == false && doc.value.metadata.deleted.val == false){';
129 if (! empty($skip_docs))
132 $sd_cnt = count($skip_docs);
133 for ($i=0; $i<$sd_cnt; $i++
)
135 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
144 $view .= 'var hm = false;';
145 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
148 $tags_cnt = count($tags);
149 for ($i=0; $i<$tags_cnt; $i++
)
151 $view .= "doc.value.tags.val[i] == '{$tags[$i]}' ";
152 if ($i < $tags_cnt-1)
159 $view .= 'hm = true;}';
160 $view .= '} if (hm) {';
161 $view .= 'map( doc.value._type, {"_id": doc._id, "_rev": doc._rev, "_type": doc.value._type,';
162 $view .= '"title": doc.value.title,';
164 if (! empty($map_values))
166 foreach ($map_values as $key => $value)
168 if (in_array($key, array('title', 'tags')))
173 $view .= json_encode($key) . ": {$value},";
177 // $.each($.ajatus.preferences.client.content_types, function(key,type){
178 // if (type['title_item']) {
179 // $.each(type['title_item'], function(i,part){
180 // if (type.schema[part]) {
181 // rel_view += '"'+part+'": doc.value.'+part+',';
187 $view .= '"tags": doc.value.tags });';
190 if (! empty($skip_docs))
197 // echo "Builded view: \n{$view}\n";
199 $content_db = $this->configuration
['content_db'];
200 return $this->connection
->$content_db->view
->temp($view);