예제 #1
0
 public function postGuestPostsCountry()
 {
     //verify the user input and create account
     $validator = Validator::make(Input::all(), array('Type' => 'required|in:Notice,Eventsq,Job,Offer', 'Title' => 'required|max:40', 'Description' => 'required', 'Name' => 'required|max:120', 'Email' => 'required|email|max:60', 'Pic' => 'image|max:3000', 'Country' => 'required'));
     if ($validator->fails()) {
         return Redirect::route('advanced_squeeb-get')->withErrors($validator)->withInput()->with('global', 'Sorry!! Your Squeeb was not posted, please retry.');
     } else {
         $type = Input::get('Type');
         $title = Input::get('Title');
         $description = Input::get('Description');
         $description = $this->MakeUrls($description);
         $squeeb = substr(strip_tags($description), 0, 300);
         $name = Input::get('Name');
         $email = Input::get('Email');
         $file = Input::file('Pic');
         $package = Input::get('Package');
         $countryid = Input::get('Country');
         $campuses = Branch::whereHas('Institution', function ($query) use($countryid) {
             $query->where('country_id', '=', $countryid);
         })->select('id')->get();
         //check whether the user with this email existed before
         $user = User::where('email', '=', $email);
         $firstname = NULL;
         $lastname = NULL;
         //extract the firstname and last name from the name
         if (preg_match('/\\s/', $name)) {
             $names = explode(" ", $name);
             $firstname = $names[0];
             $lastname = $names[1];
         } else {
             $firstname = $name;
         }
         if ($user->count()) {
             $user = $user->orderBy('id', 'DESC')->first();
         } else {
             //store the user details
             $user = User::create(array('email' => $email, 'firstname' => $firstname, 'lastname' => $lastname));
         }
         //get the model name
         $model = $type;
         $result = $this->postUploadPhoto($type, $file);
         $the_squeeb_array = array();
         foreach ($campuses as $campus) {
             $the_squeeb = Squeeb::create(array('model' => $model, 'views' => 0, 'branch_id' => $campus->id, 'create_day' => date("Y-m-d")));
             array_push($the_squeeb_array, $the_squeeb->id);
             //create the post details
             $squeeb_id = 0;
             $squeeb_id = $this->createSqueeb($model, $the_squeeb->id, $user->id, $title, $description, $squeeb);
             //finally post the squeebe photo
             if ($squeeb_id) {
                 if ($result != FALSE) {
                     //photos validation
                     $modelname = $type;
                     $tablename = lcfirst($modelname) . 's';
                     $modelphoto = $modelname . 'Photo';
                     $savesuccess = $modelphoto::create(array($tablename . '_id' => $squeeb_id, 'name' => $result));
                 }
             }
         }
         if ($user && $the_squeeb) {
             if ($squeeb_id) {
                 $squeeb = Squeeb::where('id', '=', $the_squeeb->id)->first();
                 View::share('squeeb', $squeeb);
                 view::share('model', $type);
                 $the_squeeb_array = implode(",", $the_squeeb_array);
                 View::share('the_squeeb_array', $the_squeeb_array);
                 return View::make('guest.advanced_squeeb_preview');
             }
         }
     }
     return Redirect::route('advanced_squeeb-get')->with('global', 'Some Error Occured, Please try again');
 }