Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 45.84 KB | None | 0 0
  1. <?php
  2.    
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Http\Request;
  7. use RecursiveIteratorIterator;
  8. use RecursiveDirectoryIterator;
  9. use ZipArchive;
  10.  
  11. class Admin extends Controller
  12. {
  13.     public $cfg;
  14.     public $style;
  15.  
  16.     public function __construct()
  17.     {
  18.         $this->cfg = \App\Config::first();
  19.         $this->style = \App\Style::first();
  20.     }
  21.     public function header($title = 'Admin',$area = false)
  22.     {
  23.         $title = $title.' | '.$this->cfg->name;
  24.         $cfg = $this->cfg;
  25.         $tp = url("/themes/".$this->cfg->theme);
  26.         return view('admin/header')->with(compact('title','cfg','tp','area'))->render();
  27.     }
  28.     public function footer()
  29.     {
  30.         return view('admin/footer')->render();
  31.     }
  32.     public function login()
  33.     {
  34.         if(isset(request()->login)){
  35.             // Check email and password and redirect to the dashboard
  36.             $email = escape(request()->email);
  37.             $password = md5(request()->pass);
  38.             if(empty($email) or empty($password)){
  39.                 $error = '<div class="alert alert-danger"> All fields are required </div>';
  40.             } else {
  41.                 if(\App\Administrator::where(['email' => $email,'password' => $password])->count() > 0) {
  42.                     // Generate a new secure ID for this session and redirect to dashboard
  43.                     $secure_id = md5(microtime());
  44.                     \App\Administrator::where(['email' => $email])->update(['secure' => $secure_id]);
  45.                     session(['admin' => $secure_id]);
  46.                     return redirect('admin');
  47.                 } else {
  48.                     $error = '<div class="alert alert-danger"> Wrong email or password </div>';
  49.                 }
  50.             }
  51.         }
  52.         $cfg = $this->cfg;
  53.         $tp = url("/themes/".$this->cfg->theme);
  54.         return view('admin/login')->with(compact('cfg','error','tp'))->render();
  55.     }
  56.     public function logout()
  57.     {
  58.         // Clear the admin seesion
  59.         session(['admin' => '']);
  60.         return redirect('admin/login');
  61.     }
  62.     public function index()
  63.     {
  64.         // Statistics data for the last 7 days
  65.         for ($day = 6; $day >= 0; $day--) {
  66.             $d[7 - $day] = "'".date('Y-m-d', strtotime("-" . $day . " day"))."'";
  67.             $i[date('Y-m-d', strtotime("-" . $day . " day"))] = 0;
  68.             $o[date('Y-m-d', strtotime("-" . $day . " day"))] = 0;
  69.             $s[date('Y-m-d', strtotime("-" . $day . " day"))] = 0;
  70.             $c[date('Y-m-d', strtotime("-" . $day . " day"))] = 0;
  71.         }
  72.         $fs = \App\Visitor::whereRaw('date > '.$d[1])->orderby("date","asc")->get();
  73.         foreach($fs as $visits){
  74.             $i[$visits->date] = $visits->visits;
  75.         }
  76.         $yesterday = date('Y-m-d', strtotime(date('Y-m-d') .' -1 day'));
  77.         $yvisits = (!in_array($yesterday,$i)) ? $i[$yesterday] : 0;
  78.         $tvisits = (!in_array(date('Y-m-d'),$i)) ? $i[date('Y-m-d')] : 0;
  79.         // Order and sales stats
  80.         $order_query = \App\Order::whereRaw('date > '.$d[1])->orderby("date","asc")->get();
  81.         foreach ($order_query as $order){
  82.             $o[$order->date] = $o[$order->date]+1;
  83.             $s[$order->date] = $s[$order->date]+$order->summ;
  84.             $c[$order->date] = $o[$order->date] / $i[$order->date]*100;
  85.         }
  86.         if ($yorders = \App\Order::whereRaw('date = '.$yesterday)->count() > 0){
  87.             $ysales = \App\Order::whereRaw('date = '.$yesterday)->sum('summ');
  88.         } else {
  89.             $ysales = 0;
  90.         }
  91.         $torders = \App\Order::whereRaw('date = '.date('Y-m-d'))->count();
  92.         if ($torders > 0){
  93.             $tsales = \App\Order::whereRaw('date = '.date('Y-m-d'))->sum('summ');
  94.         } else {
  95.             $tsales = 0;
  96.         }
  97.         $yconversion = $c[$yesterday];
  98.         $tconversion = $c[date('Y-m-d')];
  99.         // Charts - Max value
  100.         $mvisits = max($i)+max($i)*2/6+1;
  101.         $morders = max($o)+max($o)*2/6+1;
  102.         $msales = max($s)+max($s)*2/6+1;
  103.         $mconversion = max($c)+max($c)*2/6+1;
  104.         // Charts - difference between yesterday and today in percentage
  105.         $porders = percentage($yorders,$torders);
  106.         $pvisits = percentage($yvisits,$tvisits);
  107.         $psales = percentage($ysales,$tsales);
  108.         $pconversion = percentage($yconversion,$tconversion);
  109.         // Order counts by status
  110.         $stat['0'] = \App\Order::count();
  111.         $stat['1'] = \App\Order::where('stat',1)->count();
  112.         $stat['2'] = \App\Order::where('stat',2)->count();
  113.         $stat['3'] = \App\Order::where('stat',3)->count();
  114.         $stat['4'] = \App\Order::where('stat',4)->count();
  115.         // Email subscribers
  116.         $emails = array();
  117.         $emails['orders'] = \App\Order::count();
  118.         $emails['support'] = \App\Ticket::count();
  119.         $emails['newsletter'] = \App\Subscriber::count();
  120.         // Charts Data
  121.         $chart = array();
  122.         $chart['days'] = implode(', ',$d);
  123.         $i = implode(', ',$i);
  124.         $o = implode(', ',$o);
  125.         $s = implode(', ',$s);
  126.         $c = implode(', ',$c);
  127.         $ssales = \App\Order::sum('summ');
  128.         // Last site activities
  129.         $orders = \App\Order::orderby('id','desc')->limit(3)->get();
  130.         $reviews = \App\Review::orderby('id','desc')->limit(3)->get();
  131.         $tickets = \App\Ticket::orderby('id','desc')->limit(3)->get();
  132.         $referrers = \App\Referrer::orderby('visits','desc')->limit(3)->get();
  133.         $oss = \App\Os::orderby('visits','desc')->limit(3)->get();
  134.         $browsers = \App\Browser::orderby('visits','desc')->limit(3)->get();
  135.         $subscribers = \App\Subscriber::limit(6)->get();
  136.         $countries = \App\Country::orderby('visitors','desc')->orderby("orders",'desc')->limit(10)->get();
  137.         $cfg = $this->cfg;
  138.         $tp = url("/themes/".$this->cfg->theme);
  139.         $header = $this->header('Admin','index');
  140.         $footer = $this->footer();
  141.         return view('admin/index')->with(compact('header','cfg','tp','d','i','o','s','c','stat','porders','pvisits','psales','pconversion','ssales','orders','reviews','tickets','referrers','oss','browsers','emails','subscribers','countries','chart','morders','mvisits','mconversion','msales','footer'));
  142.     }
  143.     public function map(){
  144.         $json = array();
  145.         // return visitor count and orders by country
  146.         $countries = \App\Country::get();
  147.         foreach ($countries as $country) {
  148.             $json[strtolower($country->iso)] = array(
  149.                 'total'  => $country->orders,
  150.                 'visitors'  => $country->visitors,
  151.             );
  152.         }
  153.         return json_encode($json);
  154.     }
  155.     public function products($action = 'list',$action_id = 0)
  156.     {
  157.         if(isset(request()->add)){
  158.             $data['title'] = request()->title;
  159.             $data['text'] = request()->text;
  160.             $data['price'] = request()->price;
  161.             $data['category'] = (int)request()->category;
  162.             $data['quantity'] = (int)request()->quantity;
  163.             $data['images'] = '';
  164.             $data['download'] = '';
  165.             $options = array();
  166.             if (isset(request()->option_title)){
  167.                 $choice_titles = request()->option_title;
  168.                 $choice_types = request()->option_type;
  169.                 $choice_no = request()->option_no;
  170.                 if(count($choice_titles ) > 0){
  171.                     foreach ($choice_titles as $i => $row) {
  172.                         $choice_options = request()->input('option_set'.$choice_no[$i]);
  173.                         $options[] = array(
  174.                                         'no' => $choice_no[$i],
  175.                                         'title' => $choice_titles[$i],
  176.                                         'name' => 'choice_'.$choice_no[$i],
  177.                                         'type' => $choice_types[$i],
  178.                                         'option' => $choice_options
  179.                                     );
  180.                     }
  181.                 }
  182.             }
  183.             $data['options'] = json_encode($options);
  184.             $product = \App\Product::insertGetId($data);
  185.                 if (request()->file('images')) {
  186.                     // Upload selected images to product assets directory
  187.                     $order = 0;
  188.                     $images = array();
  189.                     foreach (request()->file('images') as $file) {
  190.                         $name = $file->getClientOriginalName();
  191.                         if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  192.                             $images[] = $image = $product.'-'.$order.'.'.$file->getClientOriginalExtension();
  193.                             $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'products';
  194.                             $file->move($path,$image);
  195.                             $order++;
  196.                         } else {
  197.                             notices("warning","$name is not a valid format");
  198.                         }
  199.                     }
  200.                     \App\Product::where(["id" => $product])->update(["images" => implode(',',$images)]);
  201.                 }
  202.                 if (request()->file('download')) {
  203.                     // Upload the downloadable file to product downloads directory
  204.                     $name = request()->file('download')->getClientOriginalName();
  205.                     $file = md5(time()).'.'.request()->file('download')->getClientOriginalExtension();
  206.                     $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'downloads';
  207.                     request()->file('download')->move($path,$file);
  208.                     \App\Product::where(["id" => $product])->update(["download" => $file]);
  209.                 }
  210.             notices("success","Product has been added successfully !");
  211.         }
  212.         if(isset(request()->edit)){
  213.             $data['title'] = request()->title;
  214.             $data['text'] = request()->text;
  215.             $data['price'] = request()->price;
  216.             $data['category'] = (int)request()->category;
  217.             $data['quantity'] = (int)request()->quantity;
  218.             $options = array();
  219.             if (isset(request()->option_title)){
  220.                 $choice_titles = request()->option_title;
  221.                 $choice_types = request()->option_type;
  222.                 $choice_no = request()->option_no;
  223.                 if(count($choice_titles ) > 0){
  224.                     foreach ($choice_titles as $i => $row) {
  225.                         $choice_options = request()->input('option_set'.$choice_no[$i]);
  226.                         $options[] = array(
  227.                                         'no' => $choice_no[$i],
  228.                                         'title' => $choice_titles[$i],
  229.                                         'name' => 'choice_'.$choice_no[$i],
  230.                                         'type' => $choice_types[$i],
  231.                                         'option' => $choice_options
  232.                                     );
  233.                     }
  234.                 }
  235.             }
  236.             $data['options'] = json_encode($options);
  237.             \App\Product::where(["id" => $action_id])->update($data);
  238.                 if (request()->file('images')) {
  239.                     // Update product images
  240.                     $order = 0;
  241.                     $images = array();
  242.                     foreach (request()->file('images') as $file) {
  243.                         $name = $file->getClientOriginalName();
  244.                         if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  245.                             $images[] = $image = $action_id.'-'.$order.'.'.$file->getClientOriginalExtension();
  246.                             $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'products';
  247.                             $file->move($path,$image);
  248.                             $order++;
  249.                         } else {
  250.                             notices("warning","$name is not a valid format");
  251.                         }
  252.                     }
  253.                     \App\Product::where(["id" => $action_id])->update(["images" => implode(',',$images)]);
  254.                 }
  255.                 if (request()->file('download')) {
  256.                     // Update the downloadable file
  257.                     $name = request()->file('download')->getClientOriginalName();
  258.                     $file = md5(time()).'.'.request()->file('download')->getClientOriginalExtension();
  259.                     $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'downloads';
  260.                     request()->file('download')->move($path,$file);
  261.                     \App\Product::where(["id" => $action_id])->update(["download" => $file]);
  262.                 }
  263.                 notices("success","Product has been updated successfully !");
  264.         }
  265.         if($action == "delete"){
  266.             \App\Product::where(["id" => $action_id])->delete();
  267.             notices("success","Product has been deleted successfully !");
  268.         }
  269.         if($action == "edit"){
  270.             $product = \App\Product::where(["id" => $action_id])->first();
  271.         }
  272.         $header = $this->header('Products','products');
  273.         $products = \App\Product::get();
  274.         $categories = \App\Category::where(["parent" => 0])->get();
  275.         $footer = $this->footer();
  276.         return view('admin/products')->with(compact('header','action','products','product','categories','footer'));
  277.     }
  278.     public function categories($action = 'list',$action_id = 0){
  279.         if(isset(request()->add)){
  280.             $data['name'] = request()->name;
  281.             $data['path'] = request()->path;
  282.             $data['parent'] = request()->parent;
  283.             \App\Category::insert($data);
  284.             notices("success","Category has been added successfully !");
  285.         }
  286.         if(isset(request()->edit)){
  287.             $data['name'] = request()->name;
  288.             $data['path'] = request()->path;
  289.             $data['parent'] = request()->parent;
  290.             \App\Category::where(["id" => $action_id])->update($data);
  291.             notices("success","Category has been edited successfully !");
  292.         }
  293.         if($action == "delete")
  294.         {
  295.             \App\Category::where(["id" => $action_id])->delete();
  296.             notices("success","Category has been deleted successfully !");
  297.         }
  298.         if($action == "edit") {
  299.             $category = \App\Category::where(["id" => $action_id])->first();
  300.         }
  301.         $header = $this->header('Categories','categories');
  302.         $categories = \App\Category::get();
  303.         $footer = $this->footer();
  304.         $parents = \App\Category::where(["parent" => 0])->get();
  305.         return view('admin/categories')->with(compact('header','action','categories','parents','category','footer'));
  306.     }
  307.     public function pages($action = 'list',$action_id = 0){
  308.         if(isset(request()->add)){
  309.             $data['title'] = request()->title;
  310.             $data['content'] = request()->content;
  311.             $data['path'] = request()->path;
  312.             \App\Page::insert($data);
  313.             notices("success","Page has been added successfully !");
  314.         }
  315.         if(isset(request()->edit)){
  316.             $data['title'] = request()->title;
  317.             $data['content'] = request()->content;
  318.             $data['path'] = request()->path;
  319.             \App\Page::where(["id" => $action_id])->update($data);
  320.             notices("success","Category has been edited successfully !");
  321.         }
  322.         if($action == "delete")
  323.         {
  324.             \App\Page::where(["id" => $action_id])->delete();
  325.             notices("success","Category has been deleted successfully !");
  326.         }
  327.         if($action == "edit") {
  328.             $page = \App\Page::where(["id" => $action_id])->first();
  329.         }
  330.         $header = $this->header('Pages','pages');
  331.         $pages = \App\Page::get();
  332.         $tp = url("/themes/".$this->cfg->theme);
  333.         $footer = $this->footer();
  334.         return view('admin/pages')->with(compact('header','action','pages','page','tp','footer'));
  335.     }
  336.     public function blog($action = 'list',$action_id = 0 ){
  337.         if(isset(request()->add)){
  338.             $data['title'] = request()->title;
  339.             $data['content'] = request()->content;
  340.             $data['time'] = time();
  341.             $data['images'] = '';
  342.             $post = \App\Post::insertGetId($data);
  343.                 if (request()->file('image')) {
  344.                     // Upload blog post image to blog assets directory
  345.                     $file = request()->file('image');
  346.                     $name = $file->getClientOriginalName();
  347.                     if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  348.                         $image = $post.'.'.$file->getClientOriginalExtension();
  349.                         $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'blog';
  350.                         $file->move($path,$image);
  351.                     } else {
  352.                         notices("warning","$name is not a valid format");
  353.                     }
  354.                     \App\Post::where(["id" => $post])->update(["images" => $image]);
  355.                 }
  356.             notices("success","Post has been added successfully !");
  357.         }
  358.         if(isset(request()->edit)){
  359.             $data['title'] = request()->title;
  360.             $data['content'] = request()->content;
  361.             \App\Post::where(["id" => $action_id])->update($data);
  362.                 if (request()->file('image')) {
  363.                     // Update the blog post image
  364.                     $file = request()->file('image');
  365.                     $name = $file->getClientOriginalName();
  366.                     if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  367.                         $image = $action_id.'.'.$file->getClientOriginalExtension();
  368.                         $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'blog';
  369.                         $file->move($path,$image);
  370.                     } else {
  371.                         notices("warning","$name is not a valid format");
  372.                     }
  373.                     \App\Post::where(["id" => $action_id])->update(["images" => $image]);
  374.                 }
  375.             notices("success","Post has been updated successfully !");
  376.         }
  377.         if($action == "delete")
  378.         {
  379.             \App\Post::where(["id" => $action_id])->delete();
  380.             notices("success","Post has been deleted successfully !");
  381.         }
  382.         if($action == "edit") {
  383.             $post = \App\Post::where(["id" => $action_id])->first();
  384.         }
  385.         $header = $this->header('Blog','blog');
  386.         $posts = \App\Post::get();
  387.         $footer = $this->footer();
  388.         return view('admin/blog')->with(compact('header','action','posts','post','footer'));
  389.     }
  390.     public function customers($action = 'list',$action_id = 0 ){
  391.         if($action == "delete")
  392.         {
  393.             \App\Customer::where(["id" => $action_id])->delete();
  394.             notices("success","Customer has been deleted successfully !");
  395.         }
  396.         $header = $this->header('Customers','customers');
  397.         $customers = \App\Customer::get();
  398.         $footer = $this->footer();
  399.         return view('admin/customers')->with(compact('header','action','customers','footer'));
  400.     }
  401.     public function coupons($action = 'list',$action_id = 0 ){
  402.         if(isset(request()->add)){
  403.             $data['code'] = request()->code;
  404.             $data['discount'] = (int)request()->discount;
  405.             $data['type'] = request()->type;
  406.             \App\Coupon::insert($data);
  407.             notices("success","Coupon has been added successfully !");
  408.         }
  409.         if(isset(request()->edit)){
  410.             $data['code'] = request()->code;
  411.             $data['discount'] = (int)request()->discount;
  412.             $data['type'] = request()->type;
  413.             \App\Coupon::where(["id" => $action_id])->update($data);
  414.             notices("success","Coupon has been updated successfully !");
  415.         }
  416.         if($action == "delete")
  417.         {
  418.             \App\Coupon::where(["id" => $action_id])->delete();
  419.             notices("success","Coupon has been deleted successfully !");
  420.         }
  421.         if($action == "edit") {
  422.             $coupon = \App\Coupon::where(["id" => $action_id])->first();
  423.         }
  424.         $header = $this->header('Coupons','coupons');
  425.         $coupons = \App\Coupon::get();
  426.         $footer = $this->footer();
  427.         return view('admin/coupons')->with(compact('header','action','coupons','coupon','footer'));
  428.     }
  429.     public function shipping($action = 'list',$action_id = 0 ){
  430.         if(isset(request()->add)){
  431.             $data['country'] = request()->country;
  432.             $data['cost'] = request()->cost;
  433.             \App\Shipping::insert($data);
  434.             notices("success","Shipping cost has been added successfully !");
  435.         }
  436.         if(isset(request()->edit)){
  437.             $data['country'] = request()->country;
  438.             $data['cost'] = request()->cost;
  439.             \App\Shipping::where(["id" => $action_id])->update($data);
  440.             notices("success","Shipping cost has been edited successfully !");
  441.         }
  442.         if($action == "delete")
  443.         {
  444.             \App\Shipping::where(["id" => $action_id])->delete();
  445.             notices("success","Shipping cost has been deleted successfully !");
  446.         }
  447.         if($action == "edit") {
  448.             $cost = \App\Shipping::where(["id" => $action_id])->first();
  449.         }
  450.         $header = $this->header('Shipping cost','shipping');
  451.         $costs = \App\Shipping::get();
  452.         $countries = \App\Country::orderby('nicename','asc')->get();
  453.         $footer = $this->footer();
  454.         return view('admin/shipping')->with(compact('header','action','costs','countries','cost','footer'));
  455.     }
  456.     public function reviews($action = 'list',$action_id = 0){
  457.         if($action == 'approve')
  458.         {
  459.             \App\Review::where(["id" => $action_id])->update(["active" => 1]);
  460.             notices("success","Review has been approved !");
  461.         }
  462.         $header = $this->header('Admin','reviews');
  463.         $reviews = \App\Review::get();
  464.         $footer = $this->footer();
  465.         return view('admin/reviews')->with(compact('header','reviews','footer'));
  466.     }
  467.     public function orders($action = 'list',$action_id = 0){
  468.         if($action == 'delete')
  469.         {
  470.             \App\Order::where(["id" => $action_id])->delete();
  471.             notices("success","Order has been deleted successfully !");
  472.         }
  473.         $header = $this->header('Orders','orders');
  474.         $fields = \App\Field::get();
  475.         $orders = \App\Order::get();
  476.         if($action == 'details') {
  477.             if(isset(request()->save)){
  478.                 \App\Order::where(["id" => $action_id])->update(["stat" => request()->stat]);
  479.                 notices("success","Order status has been changed successfully !");
  480.             }
  481.             $order = \App\Order::where(["id" => $action_id])->first();
  482.         }
  483.         $footer = $this->footer();
  484.         return view('admin/orders')->with(compact('header','action','fields','orders','order','footer'));
  485.     }
  486.     public function statistics($term = 'week'){
  487.         // Create default statistics by selected period
  488.         if ($term == 'year'){
  489.             for ($iDay = 365; $iDay >= 0; $iDay--) {
  490.                 $d[366 - $iDay] = "'".date('Y-m-d', strtotime("-" . $iDay . " day"))."'";
  491.                 $i[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  492.                 $o[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  493.                 $s[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  494.                 $c[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  495.             }
  496.         } elseif ($term == 'month'){
  497.             for ($iDay = 30; $iDay >= 0; $iDay--) {
  498.                 $d[31 - $iDay] = "'".date('Y-m-d', strtotime("-" . $iDay . " day"))."'";
  499.                 $i[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  500.                 $o[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  501.                 $s[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  502.                 $c[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  503.             }
  504.         } else {
  505.             for ($iDay = 6; $iDay >= 0; $iDay--) {
  506.                 $d[7 - $iDay] = "'".date('Y-m-d', strtotime("-" . $iDay . " day"))."'";
  507.                 $i[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  508.                 $o[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  509.                 $s[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  510.                 $c[date('Y-m-d', strtotime("-" . $iDay . " day"))] = 0;
  511.             }
  512.         }
  513.        
  514.         // Visitors statistics
  515.         $fs = \App\Visitor::whereRaw('date > '.$d[1])->orderby("date","asc")->get();
  516.         $visit = array();
  517.         foreach ($fs as $visits){
  518.             $i[$visits->date] = $visits->visits;
  519.         }
  520.         $yesterday = date('Y-m-d', strtotime(date('Y-m-d') .' -1 day'));
  521.         $yvisits = (!in_array($yesterday,$i)) ? $i[$yesterday] : 0;
  522.         $tvisits = (!in_array(date('Y-m-d'),$i)) ? $i[date('Y-m-d')] : 0;
  523.        
  524.         // Order and sales statistics
  525.         $order_query = \App\Order::whereRaw('date > '.$d[1])->orderby('date','asc')->get();
  526.         foreach ($order_query as $order){
  527.             $o[$order->date] = $o[$order->date]+1;
  528.             $s[$order->date] = $s[$order->date]+$order->summ;
  529.             $c[$order->date] = $o[$order->date] / $i[$order->date]*100;
  530.         }
  531.         if ($yorders = \App\Order::whereRaw('date = '.$yesterday)->count() > 0){
  532.             $ysales = \App\Order::whereRaw('date = '.$yesterday)->sum('summ');
  533.         } else {
  534.             $ysales = 0;
  535.         }
  536.         $torders = \App\Order::whereRaw('date = '.date('Y-m-d'))->count();
  537.         if ($torders > 0){
  538.             $tsales = \App\Order::whereRaw('date = '.date('Y-m-d'))->sum('summ');
  539.         } else {
  540.             $tsales = 0;
  541.         }
  542.         $yconversion = $c[$yesterday];
  543.         $tconversion = $c[date('Y-m-d')];
  544.         // Charts - Max value
  545.         $morders = max($o)+max($o)*2/6+1;
  546.         $msales = max($s)+max($s)*2/6+1;
  547.         $mconversion = max($c)+max($c)*2/6+1;
  548.         $mvisits = max($i)+max($i)*2/6+1;
  549.         // Charts - difference between yesterday and today in percentage
  550.         $porders = percentage($yorders,$torders);
  551.         $pvisits = percentage($yvisits,$tvisits);
  552.         $psales = percentage($ysales,$tsales);
  553.         $pconversion = percentage($yconversion,$tconversion);
  554.         // Charts Data
  555.         $orders = \App\Order::count();
  556.         $ssales = \App\Order::sum('summ');
  557.         $chart = array();
  558.         $chart['days'] = implode(', ',$d);
  559.         $i = implode(', ',$i);
  560.         $o = implode(', ',$o);
  561.         $s = implode(', ',$s);
  562.         $c = implode(', ',$c);
  563.         $header = $this->header('Statistics','statistics');
  564.         $cfg = $this->cfg;
  565.         $footer = $this->footer();
  566.         return view('admin/statistics')->with(compact('header','term','cfg','i','o','s','c','stat','porders','pvisits','psales','pconversion','orders','ssales','chart','morders','mvisits','mconversion','msales','footer'));
  567.     }
  568.     public function tracking($action = 'list',$action_id = 0 ){
  569.         if(isset(request()->add)){
  570.             $data['name'] = request()->name;
  571.             $data['code'] = request()->code;
  572.             \App\Tracking::insert($data);
  573.             notices("success","Tracking code has been added successfully !");
  574.         }
  575.         if($action == 'delete')
  576.         {
  577.             \App\Tracking::where(["id" => $action_id])->delete();
  578.             notices("success","Tracking code has been deleted successfully !");
  579.         }
  580.         $header = $this->header('Tracking','tracking');
  581.         $codes = \App\Tracking::get();
  582.         $footer = $this->footer();
  583.         return view('admin/tracking')->with(compact('header','action','codes','footer'));
  584.     }
  585.     public function newsletter(){
  586.         if(isset(request()->send)){
  587.             $emails['orders'] = array();
  588.             $emails['support'] = array();
  589.             $emails['newsletter'] = array();
  590.             $orders = \App\Order::get();
  591.             foreach ($orders as $order){
  592.                 $emails['orders'][$order->email] = $order->email;
  593.             }
  594.             $tickets = \App\Ticket::get();
  595.             foreach ($tickets as $ticket){
  596.                 $emails['support'][$ticket->email] = $ticket->email;
  597.             }
  598.             $subscribers = \App\Subscriber::get();
  599.             foreach ($subscribers as $subscriber){
  600.                 $emails['newsletter'][$subscriber->email] = $subscriber->email;
  601.             }
  602.             if (request()->group == 'orders') {
  603.                 $tos = $emails['orders'];
  604.             } elseif (request()->group == 'newsletter') {
  605.                 $tos = $emails['newsletter'];
  606.             } elseif (request()->group == 'support') {
  607.                 $tos = $emails['support'];
  608.             } else {
  609.                 $tos = array_merge($emails['support'],$emails['newsletter'],$emails['orders']);
  610.             }
  611.             // Send email to every email in the slected group
  612.             foreach ($tos as $to){
  613.                 mailing('newsletter',array('title'=>request()->title,'email'=>$to,'content'=>nl2br(request()->content)),request()->title,$to);
  614.             }
  615.             notices("success","Newsletter has been sent successfully !");
  616.         }
  617.         $header = $this->header('Newsletter','newsletter');
  618.         $footer = $this->footer();
  619.         return view('admin/newsletter')->with(compact('header','footer'));
  620.     }
  621.     public function referrers(){
  622.         $header = $this->header('Referrers','referrers');
  623.         $referrers = \App\Referrer::orderby('visits','desc')->get();
  624.         $footer = $this->footer();
  625.         return view('admin/referrers')->with(compact('header','referrers','footer'));
  626.     }
  627.     public function os(){
  628.         $header = $this->header('Operating systems','os');
  629.         $OSs = \App\Os::orderby('visits','desc')->get();
  630.         $footer = $this->footer();
  631.         return view('admin/os')->with(compact('header','OSs','footer'));
  632.     }
  633.     public function browsers(){
  634.         $header = $this->header('Browsers','browsers');
  635.         $browsers = \App\Browser::orderby('visits','desc')->get();
  636.         $footer = $this->footer();
  637.         return view('admin/browsers')->with(compact('header','browsers','footer'));
  638.     }
  639.     public function payment($action = 'list',$action_id = 0 ){
  640.         if(isset(request()->edit)){
  641.             $method = \App\Payment::where(["id" => $action_id])->first();
  642.             $method_options = json_decode($method->options,true);
  643.             $options = array();
  644.             foreach ($method_options as $key => $value){
  645.                 $options[$key] = request()->$key;
  646.             }
  647.             $data_options = json_encode($options);
  648.             $data['title'] = request()->title;
  649.             $data['active'] = request()->active;
  650.             $data['options'] = $data_options;
  651.             \App\Payment::where(["id" => $action_id])->update($data);
  652.             notices("success","Payment method has been updated successfully !");
  653.         }
  654.         $header = $this->header('Payment methods','payment');
  655.         $methods = \App\Payment::get();
  656.         if($action == "edit") {
  657.             $method = \App\Payment::where(["id" => $action_id])->first();
  658.         }
  659.         $footer = $this->footer();
  660.         return view('admin/payment')->with(compact('header','action','methods','method','footer'));
  661.     }
  662.     public function currency($action = 'list',$action_id = 0 ){
  663.         if(isset(request()->add)){
  664.             $data['name'] = request()->name;
  665.             $data['code'] = request()->code;
  666.             $data['rate'] = request()->rate;
  667.             \App\Currency::insert($data);
  668.             notices("success","Currency has been added successfully !");
  669.         }
  670.         if(isset(request()->edit)){
  671.             $data['name'] = request()->name;
  672.             $data['code'] = request()->code;
  673.             $data['rate'] = request()->rate;
  674.             \App\Currency::where(["id" => $action_id])->update($data);
  675.             notices("success","Currency has been updated successfully !");
  676.         }
  677.         if($action == "delete")
  678.         {
  679.             \App\Currency::where(["id" => $action_id])->delete();
  680.             notices("success","Currency has been deleted successfully !");
  681.         }
  682.         if($action == "default")
  683.         {
  684.             \App\Currency::where(["default" => 1])->update(["default" => 0]);
  685.             \App\Currency::where(["id" => $action_id])->update(["default" => 1]);
  686.             notices("success","Currency has been set as default !");
  687.         }
  688.         $header = $this->header('Currency','currency');
  689.         $currencies = \App\Currency::get();
  690.         if($action == "edit") {
  691.             $currency = \App\Currency::where(["id" => $action_id])->first();
  692.         }
  693.         $footer = $this->footer();
  694.         return view('admin/currency')->with(compact('header','action','currencies','currency','footer'));
  695.     }
  696.     public function settings(){
  697.         if(isset(request()->save)){
  698.             if (request()->file('logo_upload')) {
  699.                 // Upload the logo to the assets directory
  700.                 $file = request()->file('logo_upload');
  701.                 if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  702.                     $image = 'logo.'.$file->getClientOriginalExtension();
  703.                     $path = base_path().DIRECTORY_SEPARATOR.'assets';
  704.                     $file->move($path,$image);
  705.                     $_POST['logo'] = 'assets/'.$image;
  706.                 } else {
  707.                     notices("warning",$file->getClientOriginalExtension()." is not a valid format");
  708.                 }
  709.             }
  710.             unset($_POST['save'],$_POST['_token'],$_POST['logo_upload']);
  711.             DB::table('config')->update($_POST);
  712.             notices("success","Settings have been updated successfully !");
  713.         }
  714.         $header = $this->header('Settings','settings');
  715.         $languages = \App\Language::get();
  716.         $cfg = $this->cfg;
  717.         $footer = $this->footer();
  718.         return view('admin/settings')->with(compact('header','languages','cfg','footer'));
  719.     }
  720.     public function theme(){
  721.         if(isset(request()->save)){
  722.             unset($_POST['save'],$_POST['_token']);
  723.             $_POST['background'] = $_POST['color1'].','.$_POST['color2'];
  724.             $_POST['button'] = $_POST['button_text'].','.$_POST['button_link'];
  725.             unset($_POST['color1'],$_POST['color2'],$_POST['button_text'],$_POST['button_link']);
  726.             DB::table('style')->update($_POST);
  727.             notices("success","Style settings have been deleted successfully !");
  728.         }
  729.         $header = $this->header('Theme settings','theme');
  730.         $style = $this->style;
  731.         $footer = $this->footer();
  732.         return view('admin/theme')->with(compact('header','style','footer'));
  733.     }
  734.     public function languages($action = 'list',$action_id = 0){
  735.         if (isset(request()->language)){
  736.             // Use requested language
  737.             $l = request()->language;
  738.         } else {
  739.             // Use default language
  740.             $l = $this->cfg->lang;
  741.         }
  742.         if($action == "save") {
  743.             // Saving new translation
  744.             \App\Translation::where(["id" => $action_id])->update(["translation" => request()->translation]);
  745.             return "success";
  746.         }
  747.         if(isset(request()->add)){
  748.             $data['name'] = request()->name;
  749.             $data['code'] = request()->code;
  750.             \App\Language::insert($data);
  751.             notices("success","Language has been added successfully !");
  752.         }
  753.         if(isset(request()->edit)){
  754.             $data['name'] = request()->name;
  755.             $data['code'] = request()->code;
  756.             \App\Language::where(["id" => $action_id])->update($data);
  757.             notices("success","Language has been updated successfully !");
  758.         }
  759.         if($action == "delete")
  760.         {
  761.             // Delete translation from database
  762.             \App\Translation::where(["id" => $action_id])->delete();
  763.             notices("success","Translation has been deleted successfully !");
  764.         }
  765.         if($action == "delete_language") {
  766.             // Delete lang from database
  767.             \App\Language::where(["id" => $action_id])->delete();
  768.             notices("success","Language has been deleted successfully !");
  769.         }
  770.         if($action == "edit"){
  771.             $lang = \App\Language::where(["id" => $action_id])->first();
  772.         }
  773.         $header = $this->header('Language','lang');
  774.         $langs = \App\Language::get();
  775.         $translations = \App\Translation::where(["lang" => $l])->get();
  776.         $footer = $this->footer();
  777.         return view('admin/languages')->with(compact('header','action','l','langs','translations','lang','footer'));
  778.     }
  779.     public function tokens($action = 'list',$action_id = 0){
  780.         if($action == "add"){
  781.             \App\Token::insert(["token" => md5(time()),"requests" => 0]);
  782.             notices("success","The API token has been generated successfully !");
  783.         }
  784.         if($action == "delete")
  785.         {
  786.             \App\Token::where(["token" => $action_id])->delete();
  787.             notices("success","Token has been deleted successfully !");
  788.         }
  789.         $header = $this->header('API Tokens','tokens');
  790.         $tokens = \App\Token::get();
  791.         $footer = $this->footer();
  792.         return view('admin/tokens')->with(compact('header','tokens','footer'));
  793.     }
  794.     public function export($action = 'list'){
  795.         if($action == 'database')
  796.         {
  797.             // we'll try to dump the database
  798.             exec(sprintf('mysqldump --force --compress --disable-keys  --user=%s --password=%s --host=%s %s > %s',escapeshellarg(env('DB_USERNAME')),escapeshellarg(env('DB_PASSWORD')),escapeshellarg(env('DB_HOST', '127.0.0.1')),escapeshellarg(env('DB_DATABASE')),escapeshellarg('backup.sql')),$dumpResult, $result);
  799.             if ($result == 1){
  800.                 // we'll use full path if you're using windows , ex : C:\xampp\mysql\bin\mysqldump.exe
  801.                 exec(sprintf('C:\xampp\mysql\bin\mysqldump.exe --force --compress --disable-keys  --user=%s --password=%s --host=%s %s > %s',escapeshellarg(env('DB_USERNAME')),escapeshellarg(env('DB_PASSWORD')),escapeshellarg(env('DB_HOST', '127.0.0.1')),escapeshellarg(env('DB_DATABASE')),escapeshellarg('backup.sql')),$dumpResult, $result);
  802.                 if ($result == 1){
  803.                     return 'Database backup failed , try to change the path to the mysqldump file in line 789 in the Admin controller';
  804.                 }
  805.             }
  806.             header( "Pragma: public" );
  807.             header( "Expires: 0" );
  808.             header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  809.             header( "Cache-Control: public" );
  810.             header( "Content-Description: File Transfer" );
  811.             header( "Content-type: application/sql" );
  812.             header( "Content-Disposition: attachment; filename=\"backup.sql\"" );
  813.             header( "Content-Transfer-Encoding: binary" );
  814.             // Download file and delete it
  815.             readfile( base_path('backup.sql') );
  816.             unlink( base_path('backup.sql') );
  817.             return;
  818.         }
  819.         elseif ($action == 'files') {
  820.             $rootPath = base_path();
  821.             $backup = md5(time()).'.zip';
  822.             // Initialize archive object
  823.             $zip = new ZipArchive();
  824.             $zip->open($backup, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  825.            
  826.             // Create recursive directory iterator
  827.             $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath),RecursiveIteratorIterator::LEAVES_ONLY);
  828.            
  829.             foreach ($files as $name => $file)
  830.             {
  831.                 // Skip directories (they would be added automatically)
  832.                 if (!$file->isDir())
  833.                 {
  834.                     // Get real and relative path for current file
  835.                     $filePath = $file->getRealPath();
  836.                     $relativePath = substr($filePath, strlen($rootPath) + 1);
  837.                    
  838.                     // Add current file to archive
  839.                     $zip->addFile($filePath, $relativePath);
  840.                 }
  841.             }
  842.            
  843.             // Zip archive will be created only after closing object
  844.             $zip->close();
  845.            
  846.             header( "Pragma: public" );
  847.             header( "Expires: 0" );
  848.             header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  849.             header( "Cache-Control: public" );
  850.             header( "Content-Description: File Transfer" );
  851.             header( "Content-type: application/zip" );
  852.             header( "Content-Disposition: attachment; filename=\"" . $backup . "\"" );
  853.             header( "Content-Transfer-Encoding: binary" );
  854.             header( "Content-Length: " . filesize( $backup ) );
  855.            
  856.             ob_get_clean();
  857.             readfile( $backup );
  858.             ob_get_clean();
  859.             unlink($backup);
  860.             return;
  861.         }
  862.         $header = $this->header('Export','export');
  863.         $footer = $this->footer();
  864.         return view('admin/export')->with(compact('header','footer'));
  865.     }
  866.     public function slider($action = 'list',$action_id = 0){
  867.         if(isset(request()->add)){
  868.             $data['title'] = request()->title;
  869.             $data['link'] = request()->link;
  870.             $data['image'] = '';
  871.             $slide = \App\Slide::insertGetId($data);
  872.             if (request()->file('image')) {
  873.                 $file = request()->file('image');
  874.                 $name = $file->getClientOriginalName();
  875.                 if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  876.                     $image = $slide.'.'.$file->getClientOriginalExtension();
  877.                     $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'slider';
  878.                     $file->move($path,$image);
  879.                     \App\Slide::where(["id" => $slide])->update(["image" => $image]);
  880.                 } else {
  881.                     notices("warning","$name is not a valid format");
  882.                 }
  883.             }
  884.             notices("success","The slide has been added successfully !");
  885.         }
  886.         if(isset(request()->edit)){
  887.             \App\Slide::where(["id" => $action_id])->update(["title" => request()->title,"link" => request()->link]);
  888.             if (request()->file('image')) {
  889.                 $file = request()->file('image');
  890.                 $name = $file->getClientOriginalName();
  891.                 if (in_array($file->getClientOriginalExtension(), array("jpg", "png", "gif", "bmp"))){
  892.                     $image = $action_id.'.'.$file->getClientOriginalExtension();
  893.                     $path = base_path().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'slider';
  894.                     $file->move($path,$image);
  895.                 } else {
  896.                     notices("warning","$name is not a valid format");
  897.                 }
  898.                 \App\Slide::where(["id" => $action_id])->update(["image" => $image]);
  899.             }
  900.             notices("success","The slide has been updated successfully !");
  901.         }
  902.         if($action == 'delete')
  903.         {
  904.             \App\Slide::where(["id" => $action_id])->delete();
  905.             notices("success","The slide has been deleted successfully !");
  906.         }
  907.         $header = $this->header('Slider','slider');
  908.         $slides = \App\Slide::get();
  909.         if($action == 'edit') {
  910.             $slide = \App\Slide::where(["id" => $action_id])->first();
  911.         }
  912.         $footer = $this->footer();
  913.         return view('admin/slider')->with(compact('header','action','slides','slide','footer'));
  914.     }
  915.     public function editor($file = 'index.php'){
  916.         $cfg = $this->cfg;
  917.         if (isset($file)){
  918.             $file = resource_path('views/'.$file);
  919.             if (!file_exists($file)){
  920.                 $file = resource_path('views/index.php');
  921.                 notices("warning","File not found , edititng index.php ");
  922.             }
  923.         } else {
  924.             $file = resource_path('views/index.php');
  925.         }
  926.        
  927.         if (isset(request()->text))
  928.         {
  929.             // Save the new content
  930.             file_put_contents($file, request()->text);
  931.             notices("success","The file has been saved successfully !");
  932.         }
  933.        
  934.         // read the file
  935.         $text = file_get_contents($file);
  936.         $header = $this->header('Editor','editor');
  937.         $files = glob(resource_path('views/*.php'), GLOB_BRACE);
  938.         $footer = $this->footer();
  939.         return view('admin/editor')->with(compact('header','cfg','files','text','footer'));
  940.     }
  941.     public function templates($action = 'list',$action_id = 0){
  942.         if(isset(request()->edit)){
  943.             $data['title'] = request()->title;
  944.             $data['template'] = request()->template;
  945.             \App\Template::where(["id" => $action_id])->update($data);
  946.             notices("success","Template has been updated successfully !");
  947.         }
  948.         if($action == 'edit')
  949.         {
  950.             $template = \App\Template::where(["id" => $action_id])->first();
  951.         }
  952.         $header = $this->header('Templates','templates');
  953.         $templates = \App\Template::get();
  954.         $footer = $this->footer();
  955.         return view('admin/templates')->with(compact('header','action','template','templates','footer'));
  956.     }
  957.     public function builder($action = 'list',$action_id = 0){
  958.         if ($action == 'page') {
  959.             $area = 'page';
  960.         } elseif ($action == 'post') {
  961.             $area = 'post';
  962.         } else {
  963.             $area = 'home';
  964.         }
  965.         if($action == 'save'){
  966.             // Save the new order of items
  967.             $data = request()->data;
  968.             parse_str($data,$str);
  969.             $builder = $str['item'];
  970.             foreach($builder as $key => $value){
  971.                 $key=$key+1;
  972.                 \App\Bloc::where(["id" => $value])->update(['o' => $key]);
  973.             }
  974.             return "Succesfully updated";
  975.         }
  976.         if(isset(request()->add)){
  977.             $data['area'] = request()->area;
  978.             $data['content'] = request()->content;
  979.             $data['title'] = request()->title;
  980.             \App\Bloc::insert($data);
  981.             notices("success","The bloc has been added successfully !");
  982.         }
  983.         if(isset(request()->edit)){
  984.             $data['content'] = request()->content;
  985.             $data['title'] = request()->title;
  986.             \App\Bloc::where(["id" => $action_id])->update($data);
  987.             notices("success","The bloc has been updated successfully !");
  988.         }
  989.         if($action == 'edit') {
  990.             $bloc = \App\Bloc::where(["id" => $action_id])->first();
  991.         }
  992.         if($action == 'delete')
  993.         {
  994.             \App\Bloc::where(["id" => $action_id])->delete();
  995.             notices("success","The bloc has been deleted successfully !");
  996.         }
  997.         $header = $this->header('Page builder','builder');
  998.         $blocs = \App\Bloc::where('area',$area)->get();
  999.         $tp = url("/themes/".$this->cfg->theme);
  1000.         $footer = $this->footer();
  1001.         return view('admin/builder')->with(compact('header','action','bloc','blocs','tp','footer'));
  1002.     }
  1003.     public function menu($action = 'list',$action_id = 0){
  1004.         if($action == 'save'){
  1005.             $data = request()->data;
  1006.             parse_str($data,$str);
  1007.             $builder = $str['item'];
  1008.             foreach($builder as $key => $value){
  1009.                 $key=$key+1;
  1010.                 \App\Menu::where(["id" => $value])->update(['o' => $key]);
  1011.             }
  1012.             return "Succesfully updated";
  1013.         }
  1014.         if(isset(request()->add)){
  1015.             $data['link'] = request()->link;
  1016.             $data['title'] = request()->title;
  1017.             $data['parent'] = request()->parent;
  1018.             \App\Menu::insert($data);
  1019.             notices("success","The menu item has been added successfully !");
  1020.         }
  1021.         if(isset(request()->edit)){
  1022.             $data['link'] = request()->link;
  1023.             $data['title'] = request()->title;
  1024.             $data['parent'] = request()->parent;
  1025.             \App\Menu::where(["id" => $action_id])->update($data);
  1026.             notices("success","The menu item has been updated successfully !");
  1027.         }
  1028.         if($action == 'edit') {
  1029.             $item = \App\Menu::where(["id" => $action_id])->first();
  1030.         }
  1031.         if($action == 'delete')
  1032.         {
  1033.             \App\Menu::where(["id" => $action_id])->delete();
  1034.             notices("success","The menu item has been deleted successfully !");
  1035.         }
  1036.         $header = $this->header('Menu','menu');
  1037.         $items = \App\Menu::orderby('o','asc')->get();
  1038.         $parents = \App\Menu::where(['parent' => 0])->get();
  1039.         $tp = url("/themes/".$this->cfg->theme);
  1040.         $footer = $this->footer();
  1041.         return view('admin/menu')->with(compact('header','action','item','items','tp','parents','footer'));
  1042.     }
  1043.     public function bottom($action = 'list',$action_id = 0){
  1044.         if($action == 'save'){
  1045.             $data = request()->data;
  1046.             parse_str($data,$str);
  1047.             $builder = $str['item'];
  1048.             foreach($builder as $key => $value){
  1049.                 $key=$key+1;
  1050.                 \App\Footer::where(["id" => $value])->update(['o' => $key]);
  1051.             }
  1052.             return "Succesfully updated";
  1053.         }
  1054.         if(isset(request()->add)){
  1055.             $data['link'] = request()->link;
  1056.             $data['title'] = request()->title;
  1057.             \App\Footer::insert($data);
  1058.             notices("success","The menu item has been added successfully !");
  1059.         }
  1060.         if(isset(request()->edit)){
  1061.             $data['link'] = request()->link;
  1062.             $data['title'] = request()->title;
  1063.             \App\Footer::where(["id" => $action_id])->update($data);
  1064.             notices("success","The menu item has been updated successfully !");
  1065.         }
  1066.         if($action == 'edit') {
  1067.             $item = \App\Footer::where(["id" => $action_id])->first();
  1068.         }
  1069.         if($action == 'delete')
  1070.         {
  1071.             \App\Footer::where(["id" => $action_id])->delete();
  1072.             notices("success","The menu item has been deleted successfully !");
  1073.         }
  1074.         $header = $this->header('Footer menu','bottom');
  1075.         $items = \App\Footer::orderby('o','asc')->get();
  1076.         $tp = url("/themes/".$this->cfg->theme);
  1077.         $footer = $this->footer();
  1078.         return view('admin/bottom')->with(compact('header','action','item','items','tp','footer'));
  1079.     }
  1080.    
  1081.     public function fields($action = 'list',$action_id = 0){
  1082.         if(isset(request()->add)){
  1083.             $data['name'] = request()->name;
  1084.             $data['code'] = request()->code;
  1085.             DB::statement(DB::raw("ALTER TABLE `orders` ADD `".$data['code']."` VARCHAR(255) NOT NULL"));
  1086.             \App\Field::insert($data);
  1087.             notices("success","Field has been added successfully !");
  1088.         }
  1089.         if(isset(request()->edit)){
  1090.             $data['name'] = request()->name;
  1091.             $data['code'] = request()->code;
  1092.             $field = \App\Field::where(["id" => $action_id])->first();
  1093.             DB::statement(DB::raw("ALTER TABLE `orders` CHANGE `".$field->code."` `".$data['code']."` VARCHAR(255) NOT NULL"));
  1094.             \App\Field::where(["id" => $action_id])->update($data);
  1095.             notices("success","Field has been updated successfully !");
  1096.         }
  1097.         if($action == 'delete')
  1098.         {
  1099.             // Delete field from database
  1100.             $field = \App\Field::where(["id" => $action_id])->first();
  1101.             DB::statement(DB::raw("ALTER TABLE `orders` DROP `".$field->code."`"));
  1102.             \App\Field::where(["id" => $action_id])->delete();
  1103.             notices("success","Field has been deleted successfully !");
  1104.         }
  1105.         if($action == 'edit')
  1106.         {
  1107.             $field = \App\Field::where(["id" => $action_id])->first();
  1108.         }
  1109.         $header = $this->header('Extrafields','fields');
  1110.         $fields = \App\Field::get();
  1111.         $footer = $this->footer();
  1112.         return view('admin/fields')->with(compact('header','action','field','fields','footer'));
  1113.     }
  1114.     public function support($action = 'list',$action_id = 0){
  1115.         if(isset(request()->send)){
  1116.             // Send E-mail to customer
  1117.             $ticket = \App\Ticket::where(["id" => $action_id])->first();
  1118.             mailing('reply',array('title' => request()->title,'email'=>$ticket->email,'reply'=>nl2br(request()->reply)),request()->title,$ticket->email);
  1119.             notices("success","E-mail has been sent successfully !");
  1120.         }
  1121.         if($action == 'reply'){
  1122.             $ticket = \App\Ticket::where(["id" => $action_id])->first();
  1123.         }
  1124.         if($action == 'delete')
  1125.         {
  1126.             \App\Ticket::where(["id" => $action_id])->delete();
  1127.             notices("success","Ticket has been deleted successfully !");
  1128.         }
  1129.         $header = $this->header('Support','support');
  1130.         $tickets = \App\Ticket::get();
  1131.         $footer = $this->footer();
  1132.         return view('admin/support')->with(compact('header','action','ticket','tickets','footer'));
  1133.     }
  1134.     public function administrators($action = 'list',$action_id = 0){
  1135.         if(isset(request()->add)){
  1136.             $data['name'] = request()->name;
  1137.             $data['email'] = request()->email;
  1138.             $data['password'] = md5(request()->pass);
  1139.             $data['secure'] = md5(time());
  1140.             \App\Administrator::insert($data);
  1141.             notices("success","Admin has been added successfully !");
  1142.         }
  1143.         if(isset(request()->edit)){
  1144.             $data['name'] = request()->name;
  1145.             $data['email'] = request()->email;
  1146.             $data['password'] = md5(request()->pass);
  1147.             \App\Administrator::where(["id" => $action_id])->update($data);
  1148.             notices("success","Admin details has been updated successfully !");
  1149.         }
  1150.         if($action == 'delete')
  1151.         {
  1152.             \App\Administrator::where(["id" => $action_id])->delete();
  1153.             notices("success","Admin has been deleted successfully !");
  1154.         }
  1155.         if($action == 'edit'){
  1156.             $admin = \App\Administrator::where(["id" => $action_id])->first();
  1157.         }
  1158.         $header = $this->header('Administrators','administrators');
  1159.         $admins = \App\Administrator::get();
  1160.         $footer = $this->footer();
  1161.         return view('admin/administrators')->with(compact('header','action','admin','admins','footer'));
  1162.     }
  1163.     public function profile(){
  1164.         if(isset(request()->update)){
  1165.             $user = \App\Administrator::where(["secure" => session('admin')])->first();
  1166.             $data['name'] = request()->name;
  1167.             $data['email'] = request()->email;
  1168.             if (request()->pass != ""){
  1169.                 $data['password'] = md5(request()->pass);
  1170.             } else {
  1171.                 $data['password'] = $user->password;
  1172.             }
  1173.             \App\Administrator::where(["secure" => session('admin')])->update($data);
  1174.             notices("success","Profile has been updated successfully !");
  1175.         }
  1176.         $header = $this->header('Profile','profile');
  1177.         $user = \App\Administrator::where(["secure" => session('admin')])->first();
  1178.         $footer = $this->footer();
  1179.         return view('admin/profile')->with(compact('header','user','footer'));
  1180.     }
  1181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement