Esempio n. 1
0
	public static function GetDatasource($datasource,$offset=null,$limit=null,&$count=null)
	{
		// format for datasource is:
		// controller://path/path?arg1=val&q=asdads asd ad ad&arg=[123,232,123]
		// channel://channel/datasource?arg1=val&q=asdads asd ad ad&arg=[123,232,123]
		// model://profiles/profile_view?arg1!=val&q=asdads asd ad ad&arg=[123,232,123]
		
		$matches=array();
		if (preg_match_all('#^([^:]*):\/\/([^?]*)(.*)$#',$datasource,$matches))
		{
			switch($matches[1][0])
			{
				case 'controller':
					return Dispatcher::Call($matches[2][0]);
				case 'model':
					$parsed=explode('.',$matches[2][0]);
					if (count($parsed)==2)
					{
						$filter=filter($matches[2][0]);
							
						if ($offset)
							$filter->offset=$offset;
							
						if ($limit)
							$filter->limit=$limit;
							
						if ($matches[3][0]!='')
							$filter->parse(trim($matches[3][0],'?'));
	
                        if ($count==null)
    						$count=$filter->get_count();
		
                        return $filter->find(); 
					}
					
					return null;
				case 'channel':
					$parsed=explode('/',$matches[2][0]);
					$channel=Channel::Get($parsed[0]);
					$query=trim($matches[3][0],'?');
					
					$args=array();
					
					if ($query!="")
					{
						$items=explode('&',$query);
						foreach($items as $item)
						{
							$element=explode('=',$item);
							$args[trim($element[0])]=trim($element[1]);						
						}
						
					}
						
					return $channel->datasource($parsed[1],$offset,$limit,$count,$args);
					
			}
		}
	}
Esempio n. 2
0
 /**
  * Fetches the data
  *
  * @return mixed The data from the datasource
  */
 protected function get_data($order_by = null, $dir = 'asc')
 {
     if ($this->sortable != null) {
         $conf = Config::Get('search/sorts');
         $this->sorting = $conf->{$this->sortable};
         $this->sort_options = $this->sorting->options->items;
         if (!$this->sort_options) {
             throw new Exception("No sort options found");
         }
         $this->sortby = $this->controller->get->exists("sortby") ? $this->controller->get->get_string("sortby") : ($this->sortby = $conf->{$this->sortable}->default_option);
         $order_by = $this->sort_options[$this->sortby]->orby_by;
         $dir = $this->sort_options[$this->sortby]->direction;
     }
     if ($this->filtrable != null) {
         $conf = Config::Get('search/filters');
         $this->filtering = $conf->{$this->filtrable};
         $this->filters = $this->filtering->options->items;
         if (!$this->filters) {
             throw new Exception("No filters found");
         }
         $this->filter = $this->controller->get->exists("filter") ? $this->controller->get->get_string("filter") : ($this->filter = $this->filtering->default_option);
         $filter = $this->filters[$this->filter]->filter;
         if ($filter) {
             if (!strpos($this->datasource, '?')) {
                 $this->datasource .= '?';
             } else {
                 $this->datasource .= '&';
             }
             $this->datasource .= $this->filters[$this->filter]->filter;
         }
     }
     $rows = null;
     if ($this->channel != null) {
         user_error('Using the channel attribute on a repeater is deprecated', E_USER_WARNING);
         $channel = Channel::Get($this->channel);
         $rows = $channel->datasource($this->datasource, $this->current_page * $this->page_size, $this->page_size, $this->total_count);
     } else {
         if (gettype($this->datasource) == 'string') {
             if (strpos($this->datasource, '://') > 1) {
                 $ds = $this->datasource;
                 if ($order_by) {
                     if (!strpos($ds, '?')) {
                         $ds .= '?';
                     } else {
                         $ds .= '&';
                     }
                     $ds .= 'order by ' . $order_by . ' ' . $dir;
                 }
                 $rows = Channel::GetDatasource($ds, $this->current_page * $this->page_size, $this->page_size, $this->total_count);
             } else {
                 user_error('Using datasources on controllers is deprecated', E_USER_WARNING);
                 $rows = $this->controller->datasource($this->datasource, $this->current_page * $this->page_size, $this->page_size, $this->total_count);
             }
         } else {
             $rows = $this->datasource;
             if (is_array($rows)) {
                 if (isset($rows['total_count'])) {
                     $this->total_count = $rows['total_count'];
                 }
                 $this->count = isset($rows['count']) ? $rows['count'] : count($rows);
             }
         }
     }
     return $rows;
 }