Snippets

Trevor Sacks kqkg: Untitled snippet

Created by Trevor Sacks
<?php

namespace Constellation;

class Controller_Ajax extends \Controller
{

	public function before(){
		parent::before();
		if(\Uri::segment(4) !== NULL){
			$convention = Model_Convention::query()->where('short_code',\Uri::segment(4))->get_one();
			if($convention === NULL)
				\Response::redirect('constellation');
			$on_staff = Model_Staff::query()->where('convention_id',$convention->id)->where('email',\Sentry2::getUser()->email)->get_one();
			if($on_staff === NULL)
				\Response::redirect('constellation');
		}
	}

	//convention/$id[/$subitem/$subitem_id]
	//put this in the admin controller, convention function, everything else arguments
	//conventions lists the conventions
	//conventions/$id gets the information for a single convention
	//conventions/$id/badges lists the badges for that convention
	//conventiosn/$id/badges/$badge_id lists a single badge of id $badge_id
	//GET returns the data, POST creates a new item, PUT/PATCH modifies an item in entirety or partially, DELETE will delete the item
	public function get_conventions($convention_shortcode = NULL, $subitem = NULL, $subitem_id = NULL){
	
		$convention = Model_Convention::query()->where('short_code',$convention_shortcode)->get_one();
		//Start the query. If we have a subitem, get it's related class. Otherwise, just use the Model_Convention.
		if($subitem_id !== NULL){
			$query = call_user_func([Model_Convention::related_class($subitem), 'query'])->where('convention_id',$convention->id)->where('id',$subitem_id);
		}elseif($subitem !== NULL){
			$query = call_user_func([Model_Convention::related_class($subitem), 'query'])->where('convention_id',$convention->id);
		}elseif($convention_shortcode !== NULL){
			$query = Model_Convention::query()->where('short_code',$convention_shortcode);
		}else{
			$conventions = Model_Staff::query()->select('convention_id')->where('email',\Sentry2::getUser()->email)->get();
			$query = Model_Convention::query();
			foreach($conventions as $key => $value){
				$query = $query->or_where('id',$value->convention_id);
			}
		}
		
		//See if we have any get parameters
		if(\Input::get('limit',NULL) !== NULL){
			$query = $query->rows_limit(\Input::get('limit'));
		}
		if(\Input::get('offset',NULL) !== NULL){
			$query = $query->rows_offset(\Input::get('offset'));
		}
		if(\Input::get('fields',NULL) !== NULL){
			$fields = explode(',',\Input::get('fields'));
			foreach($fields as $field)
				$query = $query->select($field);
		}
		if(\Input::get('sort',NULL) !== NULL){
			$sorts = explode(',',\Input::get('sort'));
			if(is_array($sorts)){
				foreach($sorts as $sort){
					$desc = (substr($sort,0,1) === '-')?true:false;
					$query = $query->order_by(str_replace('-','',$sort),$desc?'desc':'asc');
				}
			}else{
				$desc = (substr($sorts,0,1) === '-')?true:false;
				$query = $query->order_by(str_replace('-','',$sorts),$desc?'desc':'asc');
			}
		}
		/*if(\Input::get('q',Null) !== NULL){
			if($subitem_id !== NULL){
				$searchable_fields = call_user_func([Model_Convention::related_class($subitem), 'searchable_fields']);
			}else{
				$searchable_fields = Model_Convention::searchable_fields();
			}
			$field_string = implode(",",$searchable_fields);
			$query = $query->where(\DB::expr("MATCH ($field_string) AGAINST ('".\Input::get('q')."')"));
			//die($query->get_query());
		}*/
		
		//Pull the result from the query. Process it if it's an array, output it.
		$result = $query->get();
		//die(var_dump($query));
		if(is_array($result)){
			$output = array();
			$i = 0;
			foreach($result as $key => $item){
				$output[$i] = $item->to_array();
				$i++;
			}
		}else{
			$output = $result->to_array();
		}
		return $output;
	}
	
	public function post_conventions($convention_shortcode = NULL, $subitem = NULL, $subitem_id = NULL){
		if($subitem_id !== NULL){
			$item = call_user_func_array([Model_Convention::related_class($subitem),'find'],[$subitem_id]);
			foreach(\Input::post() as $key => $value){
				if($key !== 'id')
					$item->$key = $value;
			}
			$item->save();
		}elseif($subitem !== NULL){
			$post = \Input::post();
			unset($post['id']);
			$convention = Model_Convention::query()->where('short_code',$convention_shortcode)->get_one();
			$post['convention_id'] = $convention->id;
			$setup = call_user_func_array([Model_Convention::related_class($subitem),'setup'],[$convention]);
			$data = array_merge($post, $setup);
			$item = call_user_func_array([Model_Convention::related_class($subitem),'forge'],[$data]);
			$item->save();
			$subitem_id = $item->id;
		}
		return \Response::redirect('constellation/ajax/conventions/'.$convention_shortcode.'/'.$subitem.'/'.$subitem_id);
	}
	
	public function delete_index(){
		
	}
	
	public function after($response){
		if(is_array($response)){
			return \Response::forge(json_encode($response,JSON_PRETTY_PRINT),'200',['Content-Type'=>'text/plain']);
		}else{
			return $response;
		}
	}	

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.