make get_by_tags actually work
[ajatus.git] / plugins / ajatus / couchdb / views / view.php
blob48ef481c9260a0f9b388a97e58af2775bbaff082
1 <?php
2 /**
3 *
4 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 */
10 class couchdb_views_view extends couchdb_transport
12 protected $name;
13 protected $args = '';
14 protected $data = null;
16 private $static_views;
18 public function __construct($name, $configuration)
20 if (empty($name))
22 throw new couchdb_view_exception('No view name given!');
25 parent::__construct($configuration);
27 $this->name = $name;
29 $this->static_views = array( '_all_docs' );
32 public function get()
34 return $this->execute("_design/{$this->name}");
37 public function exists()
39 if ( in_array($this->name, $this->static_views)
40 || $this->name == '_temp_view')
42 return true;
45 try
47 $prefix = '_design';
48 $suffix = '?revs_info=true';
49 if (strpos($this->name, '/') !== false)
51 $prefix = '_view';
52 $suffix = '';
55 $this->execute("{$prefix}/{$this->name}{$suffix}");
56 return ($this->lastStatusCode == 200);
58 catch (couchdb_transport_exception $e)
60 throw new couchdb_view_exception("View {$this->name} not found.");
63 return false;
66 public function set_args($args=null, $raw = false)
68 if (is_null($args))
70 return;
72 if ($raw)
74 $this->data = $args;
75 return;
78 if ( is_array($args)
79 && !isset($args['views']))
81 $this->args = '?';
82 foreach ($args as $key => $value)
84 $value = rawurlencode($value);
85 $this->args .= "{$key}={$value}&";
87 $this->args = substr($this->args, 0, -1);
89 else if( is_array($args)
90 && isset($args['views']))
92 $this->data = json_encode($args);
94 else
96 $this->data = json_encode($args);
100 public function run($create_mode=false)
102 if (in_array($this->name, $this->static_views))
104 return $this->execute("{$this->name}{$this->args}");
107 if ( $this->name == '_temp_view'
108 && !is_null($this->data))
110 /**
111 * This cannot be right
112 return $this->execute("{$this->name}{$this->args}", 'POST', json_decode($this->data));
114 if (!is_string($this->data))
116 return $this->execute("{$this->name}{$this->args}", 'POST', json_encode($this->data));
118 return $this->execute("{$this->name}{$this->args}", 'POST', $this->data);
121 if ($create_mode)
123 return $this->execute("_design/{$this->name}", 'PUT', $this->data);
126 return $this->execute("_view/{$this->name}{$this->args}");