Added possibility add additional map_values for tag query
[ajatus.git] / plugins / ajatus / helpers / tag.php
blobb038e2a2390571ea39e346d1e27aab7a2f43eba0
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))
55 $context = $cparts[0];
56 $tag = $cparts[1];
58 $vparts = explode('=', $tag);
59 if (is_array($vparts))
61 $value = $vparts[1];
62 $tag = $vparts[0];
65 $res = $this->get_tag($tag, $context, $value);
66 if ($res)
68 $results[$k] = $res->id;
71 else
73 $results[$k] = $tag->_id;
77 return $results;
80 public function get_tag($name, $context='', $value='')
82 $result = false;
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}' ";
97 $view .= ') {';
99 $view .= 'map( doc._id, {';
100 $view .= ajatus_types::prepare_map_values($GLOBALS['ajatus']->types->tag->map_values['list']);
101 $view .= '});';
103 $view .= '}}';
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];
115 return $result;
118 public function related(array $tags, array $skip_docs=array(), array $map_values=array())
120 if (empty($tags))
122 return false;
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))
131 $view .= 'if (';
132 $sd_cnt = count($skip_docs);
133 for ($i=0; $i<$sd_cnt; $i++)
135 $view .= "doc._id != '{$skip_docs[$i]->id}' ";
136 if ($i < $sd_cnt-1)
138 $view .= '&& ';
141 $view .= ') {';
144 $view .= 'var hm = false;';
145 $view .= 'for (var i=0; i<doc.value.tags.val.length; i++){';
146 $view .= 'if (';
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)
154 $view .= '|| ';
158 $view .= ') {';
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')))
170 continue;
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+',';
182 // }
183 // });
184 // }
185 // });
187 $view .= '"tags": doc.value.tags });';
188 $view .= '}}';
190 if (! empty($skip_docs))
192 $view .= '}';
195 $view .= '}';
197 // echo "Builded view: \n{$view}\n";
199 $content_db = $this->configuration['content_db'];
200 return $this->connection->$content_db->view->temp($view);