4 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
10 class couchdb_views_view
extends couchdb_transport
14 protected $data = null;
16 private $static_views;
18 public function __construct($name, $configuration)
22 throw new couchdb_view_exception('No view name given!');
25 parent
::__construct($configuration);
29 $this->static_views
= array( '_all_docs' );
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')
48 $suffix = '?revs_info=true';
49 if (strpos($this->name
, '/') !== false)
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.");
66 public function set_args($args=null, $raw = false)
79 && !isset($args['views']))
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);
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
))
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
);
123 return $this->execute("_design/{$this->name}", 'PUT', $this->data
);
126 return $this->execute("_view/{$this->name}{$this->args}");