format() public static method

Get the data format expected in the response.
public static format ( string $default = 'html' ) : string
$default string
return string
Example #1
0
 public function respond($results, $view, $view_options, $message = null)
 {
     if (Request::format() == 'html') {
         if (!$results) {
             return View::make('404');
         }
         return View::make($view, $view_options);
     } else {
         if (!$results) {
             return Response::json(null, 404);
         }
         return Response::json(array('data' => $results->toArray(), 'status' => 'success', 'message' => "Success"), 200);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function omdbUpdate($id)
 {
     if (Request::format() == 'json') {
     }
     $rev = Movie::find($id);
     $rev->fl_dir_ar_id = Input::get('info.Director');
     $rev->fl_writer = Input::get('info.Writer');
     $rev->fl_stars = Input::get('info.Actors');
     $rev->fl_outline = Input::get('info.Plot');
     $rev->fl_genre = Input::get('info.Genre');
     $rev->fl_duration = Input::get('info.Runtime');
     $rev->fl_country = Input::get('info.Country');
     $rev->fl_imdbID = Input::get('info.imdbID');
     $rev->fl_imdbRating = Input::get('info.imdbRating');
     $rev->fl_imdbVotes = Input::get('info.imdbVotes');
     $rev->fl_metascore = Input::get('info.Metascore');
     $rev->save();
 }
 /**
  * Set page content
  *
  * @param array
  * @param string
  * @return void
  */
 protected function setContent($data, $settings = array())
 {
     // Debugging
     if (!empty($settings) && array_key_exists('debug', $settings)) {
         dd(compact('data', 'view'));
     }
     // Handle request
     switch (\Request::format()) {
         case 'json':
             return Response::json($data);
             // API
             break;
         default:
             if (!empty($settings) && array_key_exists('view', $settings)) {
                 $view = $settings['view'];
             } else {
                 $view = $this->view;
             }
             $this->layout->content = view($view, $data);
             // HTML
             break;
     }
 }
 public function get_fav_products()
 {
     $user_id = $this->user_id;
     $store_id = Request::segment(2);
     $products = DB::table('fav_products')->leftJoin('products', 'fav_products.product_id', '=', 'products.id')->where('fav_products.user_id', $user_id)->where('products.store_id', $store_id)->select('products.*')->get();
     if (empty($products)) {
         $response_array['products'] = array();
     } else {
         $response_array['products'] = $products;
     }
     if (Request::format() == 'html') {
         Session::put('store_id', $store_id);
         return View::make('favs')->with('data', $response_array)->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores', $this->stores);
     } else {
         $response_array['success'] = 'true';
         $response_code = 200;
         $response = Response::json($response_array, $response_code);
         return $response;
     }
 }
 public function checkout_step3()
 {
     // Processing Cart
     $cart_id = $this->cart_id;
     if (!$cart_id) {
         $cart_id = Input::get('cart_id');
     }
     $address_id = Input::get('address_id');
     $payment_id = Input::get('payment_id');
     $cart_products = DB::table('cart_products')->where('cart_id', $cart_id)->leftJoin('products', 'cart_products.product_id', '=', 'products.id')->leftJoin('users', 'cart_products.added_by', '=', 'users.id')->select('products.*', 'cart_products.quantity as cart_quantity', 'users.first_name as added_by')->orderBy('store_id')->get();
     $cart_users = DB::table('cart_users')->leftJoin('users', 'cart_users.user_id', '=', 'users.id')->select('users.*')->get();
     $response_array['cart'] = Cart::find($cart_id)->toArray();
     $response_array['cart']['total_amount'] = 0;
     $response_array['cart']['total_quantity'] = 0;
     $response_array['cart']['users'] = array();
     $response_array['cart']['stores'] = array();
     $store_id = 0;
     if ($cart_products) {
         $response_array['cart']['is_minimum_reached'] = 1;
         foreach ($cart_products as $cart_product) {
             if ($store_id != $cart_product->store_id) {
                 $store_id = $cart_product->store_id;
                 // Retriving Store
                 $store = Store::find($store_id)->toArray();
                 // Retriving Delivery Slot
                 $time = date("Y-m-d H:i:s");
                 $slot = Slot::where('store_id', $store_id)->where('start_time', '>', $time)->where('is_available', 1)->orderBy('start_time')->first();
                 if ($slot) {
                     $next_slot_start = date("D, jS M, H a", strtotime($slot->start_time));
                     $next_slot_end = date("h a", strtotime($slot->end_time));
                     $next_slot = $next_slot_start . " - " . $next_slot_end;
                 } else {
                     $next_slot = "No Slots available";
                 }
                 $store['slot'] = $next_slot;
                 $store['count'] = 1;
                 $store['sub_total'] = 0;
                 $response_array['cart']['stores'][$store_id] = $store;
                 $response_array['cart']['stores'][$store_id]['products'] = array();
             } else {
                 $response_array['cart']['stores'][$store_id]['count'] += 1;
             }
             $product = json_decode(json_encode($cart_product), true);
             $product['sub_total'] = $product['cart_quantity'] * $product['price'];
             $response_array['cart']['stores'][$store_id]['sub_total'] += $product['sub_total'];
             $response_array['cart']['total_amount'] += $product['sub_total'];
             $response_array['cart']['total_quantity']++;
             array_push($response_array['cart']['stores'][$store_id]['products'], $product);
         }
     }
     if ($cart_users) {
         foreach ($cart_users as $cart_user) {
             $product = json_decode(json_encode($cart_user), true);
             array_push($response_array['cart']['users'], $cart_user);
         }
     }
     foreach ($response_array['cart']['stores'] as $st) {
         if ($st['sub_total'] < $st['minimum_order_amount']) {
             $response_array['cart']['is_minimum_reached'] = 0;
         }
     }
     if (isset($response_array['cart']['is_minimum_reached']) && $response_array['cart']['is_minimum_reached'] == 1) {
         $order = new Order();
         $order->user_id = $this->user_id;
         $order->date = date("Y-m-d H:i:s");
         $order->total_products = $response_array['cart']['total_quantity'];
         $order->total_amount = $response_array['cart']['total_amount'];
         $order->payment_id = $payment_id;
         $order->address_id = $address_id;
         $payment = Payment::find($payment_id);
         $order->payment_customer_id = $payment->customer_id;
         $address = UserAddress::find($address_id);
         $order->address = $address->address . " " . $address->zipcode;
         $order->status = "Initiated";
         $order->save();
         foreach ($cart_products as $cart_product) {
             $order_product = new OrderProduct();
             $order_product->order_id = $order->id;
             $product = Product::find($cart_product->id);
             $product->total_sale += $cart_product->cart_quantity;
             $product->save();
             $order_product->price = $cart_product->price;
             $order_product->quantity = $cart_product->cart_quantity;
             $order_product->fulfilment_status = "Pending";
             $order_product->type = "Product";
             $order_product->parent_id = 0;
             $order_product->product_name = $product->name;
             $order_product->product_image_url = $product->image_url;
             $order_product->product_quantity = $product->quantity;
             $order_product->product_unit = $product->unit;
             $order_product->store_id = $product->store_id;
             $order_product->save();
         }
         // removing products from cart
         CartProduct::where('cart_id', $cart_id)->delete();
         // Order Placement Mail
         $user = User::find($this->user_id);
         Mail::send('emails.order_success', array('user' => $user, 'order' => $order), function ($message) use($user) {
             $message->to($user->email, $user->first_name)->subject('Thanks for placing the order!');
         });
         // Order Placement Mail to Admin
         $admin_email = Config::get('app.admin_email');
         Mail::send('emails.admin_order_success', array('user' => $user, 'order' => $order), function ($message) use($admin_email) {
             $message->to($admin_email, "Admin")->subject('New Order!');
         });
     } else {
         $message = "Minimum order amount not reached";
         if (Request::format() == 'html') {
             return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', $message);
         } else {
             $response_array = array('success' => false, 'error_code' => 411, 'error' => $message);
             $response_code = 200;
             $response = Response::json($response_array, $response_code);
             return $response;
         }
     }
     if (Request::format() == 'html') {
         return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', 'Thanks for placing your Order!!');
     } else {
         $response_array = array('success' => true);
         $response_code = 200;
         $response = Response::json($response_array, $response_code);
         return $response;
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $message = Message::findOrFail($id);
     if ($message->delete()) {
         $format = \Request::format();
         switch ($format) {
             case 'js':
                 // Just renders messages/destroy_js.blade.php
                 $render = $this->render(['js' => 'messages.destroy'], compact('message'));
                 break;
             case 'html':
             default:
                 // No js fallback
                 $render = \Redirect::route('messages.index');
                 break;
         }
         return $render;
     }
     return \Redirect::route('home')->with('message', "Error: Unable to delete this message");
 }
 public function backup()
 {
     $store = !Input::get('html');
     $path = public_path() . '/backups/' . date("Y-m-d_H-i-s");
     if ($store) {
         mkdir($path, 0777, true);
     }
     if (Input::has('prefix')) {
         $simulations = Simulation::where("Caption", "LIKE", Input::get('prefix') . "%")->get();
     } else {
         $simulations = Simulation::all();
     }
     $couldNotStore = [];
     $simulations->each(function ($simulation) use(&$couldNotStore, $store, $path) {
         $xml = new DOMDocument('1.0');
         $root = $xml->createElement('simulationDefinition');
         $xml->appendChild($root);
         try {
             $simulation->xml($root, true);
         } catch (Exception $e) {
             $couldNotStore[] = $simulation;
             return;
         }
         $xml->preserveWhiteSpace = false;
         $xml->formatOutput = true;
         if ($store) {
             $xml->save($path . '/' . $simulation->Id . '.xml');
         }
     });
     if (Request::format() == 'json') {
         if (count($store)) {
             $responseArray = array_map(function ($s) {
                 return ['id' => $s->Id, 'description' => $s->asString];
             });
             return json_encode($responseArray);
         }
         return true;
     } else {
         $err = "Backup<br/>\n";
         foreach ($couldNotStore as $simulation) {
             $err .= "Error for " . $simulation->Id . ':' . $simulation->asString . "<br\\>\n";
         }
         if ($store) {
             $err .= 'Complete.';
         } else {
             $err .= 'Tested but not stored.';
         }
         return $err;
     }
 }
Example #8
0
 public function index($requestPath = '')
 {
     $path = Path::fromRelative('/' . $requestPath);
     if (!$path->exists()) {
         Auth::basic('username');
         if (!Auth::check()) {
             // do auth
             Auth::basic('username');
             if (!Auth::check()) {
                 return Response::make(View::make('unauth', array()), 401)->header('WWW-Authenticate', 'Basic');
             }
         }
         App::abort(404, 'Path not found');
     }
     // if it's a file then download
     if ($path->isFile()) {
         return $this->download($path);
     }
     $path->loadCreateRecord($path);
     $children = $this->exportChildren($path);
     $orderParams = $this->doSorting($children);
     $groupedStaff = null;
     $genres = null;
     $categories = null;
     $userIsWatching = null;
     $pageTitle = null;
     $pageDescription = null;
     $pageImage = null;
     $relatedSeries = null;
     if ($series = $path->record->series) {
         $groupedStaff = $series->getGroupedStaff();
         $genres = $series->getFacetNames('genre');
         $categories = $series->getFacetNames('category');
         $pageTitle = $series->name;
         $pageDescription = $series->description;
         if ($series->hasImage()) {
             $pageImage = $series->getImageUrl();
         }
         $relatedSeries = $series->getRelated();
         $user = Auth::user();
         if ($user) {
             $userIsWatching = $user->isWatchingSeries($series);
         }
     } else {
         if (!$path->isRoot()) {
             $pageTitle = $path->getRelativeTop();
         }
     }
     $params = array('path' => $path, 'groupedStaff' => $groupedStaff, 'genres' => $genres, 'categories' => $categories, 'breadcrumbs' => $path->getBreadcrumbs(), 'children' => $children, 'userIsWatching' => $userIsWatching, 'pageTitle' => $pageTitle, 'pageDescription' => $pageDescription, 'pageImage' => $pageImage, 'relatedSeries' => $relatedSeries);
     $params = array_merge($params, $orderParams);
     $updated = 0;
     foreach ($children as $child) {
         if (!$child->isDir && $child->rawTime > $updated) {
             $updated = $child->rawTime;
         }
     }
     $params['updated'] = $updated;
     if (Request::format() == 'atom' || Input::get('t') == 'atom') {
         return Response::make(View::make('index-atom', $params))->header('Content-Type', 'application/atom+xml; charset=UTF-8');
     } else {
         if (Request::format() == 'rss' || Input::get('t') == 'rss') {
             return Response::make(View::make('index-rss', $params))->header('Content-Type', 'application/rss+xml; charset=UTF-8');
         } else {
             Auth::basic('username');
             if (!Auth::check()) {
                 // do auth
                 Auth::basic('username');
                 if (!Auth::check()) {
                     return Response::make(View::make('unauth', array()), 401)->header('WWW-Authenticate', 'Basic');
                 }
             }
             return View::make('index', $params);
         }
     }
 }
Example #9
0
/**
 * Get requested response format
 */
function zbase_request_format()
{
    return \Request::format();
}
 static function routing()
 {
     if (!System::$conf->php_parses_routes) {
         return;
     }
     $url_route = ltrim(Request::$url, '/');
     $i = 0;
     foreach (self::$routes as $rkey => $route) {
         Request::$action = Request::$controller = $requirements = null;
         if (!is_numeric($rkey)) {
             if (strstr($rkey, '#')) {
                 list(Request::$controller, Request::$action) = explode('#', $rkey);
             } else {
                 Request::$controller = $rkey;
             }
         }
         if (is_array($route)) {
             if (array_key_exists('requirements', $route)) {
                 $requirements = $route['requirements'];
             }
             $route = array_shift($route);
         }
         if (empty(Request::$controller) && !strstr($route, '$controller') || empty(Request::$action) && !strstr($route, '$action')) {
             throw new Exception('Insufficient arguments in routes file.');
         }
         $patt = array('/', '.', '*');
         $repl = array('\\/', '\\.', '.+');
         $route = str_replace($patt, $repl, $route);
         if ($route == '$root') {
             $route = '';
         }
         # Parse variables.
         if (strstr($route, '$')) {
             preg_match_all('/\\$(\\w+)/', $route, $vars);
             $vars = $vars[1];
             if (!empty($requirements)) {
                 foreach ($vars as $var) {
                     if ($var != 'controller' && $var != 'action' && !array_key_exists($var, $requirements)) {
                         $route = str_replace('$' . $var, '([^\\/]+)?', $route);
                     }
                 }
                 unset($var);
             }
             $route = preg_replace('/\\$\\w+/', '([^\\/]+)', $route);
         }
         $route = "/^{$route}\$/";
         if (!preg_match($route, $url_route, $match)) {
             if ($i == count(self::$routes) - 1) {
                 # Find default /controller/action
                 preg_match("/^(\\w+)(\\/(\\w+))?/", $url_route, $match);
             } else {
                 $i++;
                 continue;
             }
         }
         if (!empty($vars)) {
             array_shift($match);
             $i = 0;
             foreach ($vars as $var) {
                 if ($i == count($match)) {
                     continue;
                 }
                 if ($var == 'controller') {
                     Request::$controller = $match[$i];
                     $i++;
                     continue;
                 } elseif ($var == 'action') {
                     Request::$action = $match[$i];
                     $i++;
                     continue;
                 } elseif ($var == 'format') {
                     Request::$format = $match[$i];
                     $i++;
                     continue;
                 }
                 Request::$params->{$var} = $match[$i];
                 $i++;
             }
         }
         if (isset($requirements)) {
             foreach ($requirements as $var => $req_patt) {
                 if (!isset(Request::$params->{$var})) {
                     throw new Exception("Unknown match {$var} in routes file.");
                 }
                 $req_patt = "~^{$req_patt}\$~";
                 if (!preg_match($req_patt, Request::$params->{$var})) {
                     $i++;
                     continue 2;
                 }
             }
         }
         if (empty(Request::$action)) {
             Request::$action = 'index';
         }
         if (empty(Request::$format)) {
             if (preg_match('~\\.([a-zA-Z0-9]+)$~', $url_route, $m)) {
                 Request::$format = $m[1];
             } else {
                 Request::$format = 'html';
             }
         }
         break;
     }
 }
Example #11
0
 static function parse_request()
 {
     self::$params = new RequestParams();
     # Get method
     self::$method = $_SERVER['REQUEST_METHOD'];
     self::$remote_ip = $_SERVER['REMOTE_ADDR'];
     if (self::$method === 'POST') {
         self::$post = true;
     } elseif (self::$method === 'GET') {
         self::$get = true;
     }
     self::$abs_url =& $_SERVER['REQUEST_URI'];
     self::$url = preg_replace('~\\?.*~', '', $_SERVER['REQUEST_URI']);
     if (!System::$conf->php_parses_routes) {
         if (empty($_GET['URLtoken'])) {
             exit_with_status(404);
             die('Impossible to find route. Bad htaccess config?');
         }
         # $_GET is filled with parameters from htaccess.
         # Parse them accordingly and fill $_GET with url parameters.
         list($_GET['controller'], $_GET['action']) = explode('@', $_GET['URLtoken']);
         empty($_GET['controller']) && exit_with_status(404);
         empty($_GET['action']) && ($_GET['action'] = 'index');
         unset($_GET['URLtoken']);
         foreach ($_GET as $param => $value) {
             if (!property_exists('Request', $param)) {
                 continue;
             }
             self::${$param} = $value;
             unset($_GET[$param]);
         }
         empty(self::$format) && (self::$format = 'html');
         # Parse GET params from $abs_url
         if (!is_bool(strpos(self::$abs_url, '?'))) {
             $get_params = urldecode(substr(self::$abs_url, strpos(self::$abs_url, '?') + 1));
             $get_params = explode('&', $get_params);
             foreach ($get_params as $gp) {
                 $param = explode('=', $gp);
                 if (empty($param[0]) || empty($param[1])) {
                     continue;
                 }
                 $_GET[$param[0]] = $param[1];
             }
         }
     }
     # Get post/get parameters
     add_props(self::$params, $_GET, false);
     add_props(self::$params, $_POST, false);
     self::$get_params =& $_GET;
     self::$post_params =& $_POST;
 }