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)
54 && count($cparts) === 2)
56 $context = $cparts[0];
59 $vparts = explode('=', $tag);
61 && count($vparts) === 2)
67 $res = $this->get_tag($tag, $context, $value);
70 $results[$k] = $res->id
;
75 $results[$k] = $tag->_id
;
82 public function get_tag($name, $context='', $value='')
86 $view = 'function(doc){';
87 $view .= "if (doc.value._type == 'tag' && doc.value.title.val == '{$name}' ";
89 if (! empty($context))
91 $view .= "&& doc.value.title.widget.config.context == '{$context}' ";
96 $view .= "&& doc.value.title.widget.config.value == '{$value}' ";
101 $view .= 'map( doc._id, {';
102 $view .= ajatus_types
::prepare_map_values($GLOBALS['ajatus']->types
->tag
->map_values
['list']);
107 // echo "Builded view: \n{$view}\n";
109 $content_db = $this->configuration
['content_db'];
110 $results = $this->connection
->$content_db->view
->temp($view);
112 if ($results->total_rows
> 0)
114 $result = $results->rows
[0];
120 public function related(array $tags, array $skip_docs=array(), array $map_values=array())
127 $view = 'function(doc){';
128 $view .= 'if (typeof(doc.value.tags) != "undefined" && doc.value.tags.val.length > 0 ';
129 $view .= '&& doc.value.metadata.archived.val == false && doc.value.metadata.deleted.val == false){';
131 if (! empty($skip_docs))
134 $sd_cnt = count($skip_docs);
135 for ($i=0; $i<$sd_cnt; $i++
)
137 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
146 $view .= 'var hm = false;';
147 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
150 $tags_cnt = count($tags);
151 for ($i=0; $i<$tags_cnt; $i++
)
153 $view .= "doc.value.tags.val[i] == '{$tags[$i]}' ";
154 if ($i < $tags_cnt-1)
161 $view .= 'hm = true;}';
162 $view .= '} if (hm) {';
163 $view .= 'map( doc.value._type, {"_id": doc._id, "_rev": doc._rev, "_type": doc.value._type,';
164 $view .= '"title": doc.value.title,';
166 if (! empty($map_values))
168 foreach ($map_values as $key => $value)
170 if (in_array($key, array('title', 'tags')))
175 $view .= json_encode($key) . ": {$value},";
179 // $.each($.ajatus.preferences.client.content_types, function(key,type){
180 // if (type['title_item']) {
181 // $.each(type['title_item'], function(i,part){
182 // if (type.schema[part]) {
183 // rel_view += '"'+part+'": doc.value.'+part+',';
189 $view .= '"tags": doc.value.tags });';
192 if (! empty($skip_docs))
199 // echo "Builded view: \n{$view}\n";
201 $content_db = $this->configuration
['content_db'];
202 return $this->connection
->$content_db->view
->temp($view);