make get_by_tags actually work
[ajatus.git] / plugins / ajatus / helpers / tag.php
blob087a511c9ed0795fa69fb44b08f1dac99a3e6694
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)
31 public function tag_to_id($tag)
36 public function ids_to_tags(array $ids)
41 public function tags_to_id(array $tags)
43 $results = array();
45 foreach ($tags as $k => $tag)
47 if (is_string($tag))
49 $context = '';
50 $value = '';
52 $cparts = explode(':', $tag);
53 if ( is_array($cparts)
54 && count($cparts) === 2)
56 $context = $cparts[0];
57 $tag = $cparts[1];
59 $vparts = explode('=', $tag);
60 if (is_array($vparts)
61 && count($vparts) === 2)
63 $value = $vparts[1];
64 $tag = $vparts[0];
67 $res = $this->get_tag($tag, $context, $value);
68 if ($res)
70 $results[$k] = $res->id;
73 else
75 $results[$k] = $tag->_id;
79 return $results;
82 public function get_tag($name, $context='', $value='')
84 $result = false;
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}' ";
94 if (! empty($value))
96 $view .= "&& doc.value.title.widget.config.value == '{$value}' ";
99 $view .= ') {';
101 $view .= 'map( doc._id, {';
102 $view .= ajatus_types::prepare_map_values($GLOBALS['ajatus']->types->tag->map_values['list']);
103 $view .= '});';
105 $view .= '}}';
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];
117 return $result;
120 public function related(array $tags, array $skip_docs=array(), array $map_values=array())
122 if (empty($tags))
124 return false;
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))
133 $view .= 'if (';
134 $sd_cnt = count($skip_docs);
135 for ($i=0; $i<$sd_cnt; $i++)
137 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
138 if ($i < $sd_cnt-1)
140 $view .= '&& ';
143 $view .= ') {';
146 $view .= 'var hm = false;';
147 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
148 $view .= 'if (';
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)
156 $view .= '|| ';
160 $view .= ') {';
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')))
172 continue;
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+',';
184 // }
185 // });
186 // }
187 // });
189 $view .= '"tags": doc.value.tags });';
190 $view .= '}}';
192 if (! empty($skip_docs))
194 $view .= '}';
197 $view .= '}';
199 // echo "Builded view: \n{$view}\n";
201 $content_db = $this->configuration['content_db'];
202 return $this->connection->$content_db->view->temp($view);