Snippets
Created by
Trevor Sacks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | <?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)
You can clone a snippet to your computer for local editing. Learn more.