Esempio n. 1
0
 /**
  * Edit a job
  * @param bool|int $id The id no. of the report
  * @param bool|string $saved
  */
 function edit($id = false, $saved = false)
 {
     $this->template->content = new View('admin/jobs_edit');
     $this->template->content->title = 'Create A Job';
     // setup and initialize form field names
     $form = array('location_id' => '', 'locale' => '', 'job_title' => '', 'job_description' => '', 'job_hour' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'job_category' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'job_active' => '', 'job_verified' => '', 'job_source' => '', 'job_information' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     if ($saved == 'saved') {
         $form_saved = TRUE;
     } else {
         $form_saved = FALSE;
     }
     // Initialize Default Values
     $form['locale'] = Kohana::config('locale.language');
     //$form['latitude'] = Kohana::config('settings.default_lat');
     //$form['longitude'] = Kohana::config('settings.default_lon');
     $form['country_id'] = Kohana::config('settings.default_country');
     $form['job_date'] = date("m/d/Y", time());
     $form['job_hour'] = date('g');
     $form['job_minute'] = date('i');
     $form['job_ampm'] = date('a');
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Create Categories
     $this->template->content->job_categories = $this->_get_job_categories();
     $this->template->content->new_job_categories_form = $this->_new_job_categories_form_arr();
     // Get Countries
     $countries = array();
     foreach (ORM::factory('country')->orderby('country')->find_all() as $country) {
         // Create a list of all categories
         $this_country = $country->country;
         if (strlen($this_country) > 35) {
             $this_country = substr($this_country, 0, 35) . "...";
         }
         $countries[$country->id] = $this_country;
     }
     $this->template->content->countries = $countries;
     // Retrieve thumbnail photos (if edit);
     //XXX: fix _get_thumbnails
     $this->template->content->job = $this->_get_thumbnails($id);
     // Are we creating this report from SMS/Email/Twitter?
     // If so retrieve message
     if (isset($_GET['mid']) && !empty($_GET['mid'])) {
         $message_id = $_GET['mid'];
         $service_id = "";
         $message = ORM::factory('message', $message_id);
         if ($message->loaded == true && $message->message_type == 1) {
             $service_id = $message->reporter->service_id;
             // Has a report already been created for this Message?
             if ($message->job_id != 0) {
                 // Redirect to report
                 url::redirect('admin/jobs/edit/' . $message->job_id);
             }
             $this->template->content->show_messages = true;
             $job_description = $message->message;
             if (!empty($message->message_detail)) {
                 $job_description .= "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" . $message->message_detail;
             }
             $form['job_description'] = $job_description;
             $form['job_hour'] = date('h', strtotime($message->message_date));
             $form['job_minute'] = date('i', strtotime($message->message_date));
             $form['job_ampm'] = date('a', strtotime($message->message_date));
             $form['person_first'] = $message->reporter->reporter_first;
             $form['person_last'] = $message->reporter->reporter_last;
             // Retrieve Last 5 Messages From this account
             $this->template->content->all_messages = ORM::factory('message')->where('reporter_id', $message->reporter_id)->orderby('message_date', 'desc')->limit(5)->find_all();
         } else {
             $message_id = "";
             $this->template->content->show_messages = false;
         }
     } else {
         $this->template->content->show_messages = false;
     }
     // Are we creating this report from a Newsfeed?
     if (isset($_GET['fid']) && !empty($_GET['fid'])) {
         $feed_item_id = $_GET['fid'];
         $feed_item = ORM::factory('feed_item', $feed_item_id);
         if ($feed_item->loaded == true) {
             // Has a report already been created for this Feed item?
             if ($feed_item->job_id != 0) {
                 // Redirect to report
                 url::redirect('admin/jobs/edit/' . $feed_item->job_id);
             }
             $form['job_title'] = $feed_item->item_title;
             $form['job_description'] = $feed_item->item_description;
             $form['job_hour'] = date('h', strtotime($feed_item->item_date));
             $form['job_ampm'] = date('a', strtotime($feed_item->item_date));
             // News Link
             $form['job_news'][0] = $feed_item->item_link;
             // Does this newsfeed have a geolocation?
             if ($feed_item->location_id) {
                 $form['location_id'] = $feed_item->location_id;
                 $form['latitude'] = $feed_item->location->latitude;
                 $form['longitude'] = $feed_item->location->longitude;
                 $form['location_name'] = $feed_item->location->location_name;
             }
         } else {
             $feed_item_id = "";
         }
     }
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         // $post->add_rules('locale','required','alpha_dash','length[5]');
         $post->add_rules('location_id', 'numeric');
         $post->add_rules('message_id', 'numeric');
         $post->add_rules('job_title', 'required', 'length[3,200]');
         $post->add_rules('job_description', 'required');
         $post->add_rules('latitude', 'required', 'between[-90,90]');
         // Validate for maximum and minimum latitude values
         $post->add_rules('longitude', 'required', 'between[-180,180]');
         // Validate for maximum and minimum longitude values
         $post->add_rules('location_name', 'required', 'length[3,200]');
         $post->add_rules('person_first', 'required', 'length[3,100]');
         $post->add_rules('person_last', 'required', 'length[3,100]');
         $post->add_rules('person_email', 'required', 'email', 'length[3,100]');
         //XXX: Hack to validate for no checkboxes checked
         if (!isset($_POST['job_category'])) {
             $post->job_category = "";
             $post->add_error('job_category', 'required');
         } else {
             $post->add_rules('job_category.*', 'required', 'numeric');
         }
         // Validate only the fields that are filled in
         if (!empty($_POST['job_news'])) {
             foreach ($_POST['job_news'] as $key => $url) {
                 if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
                     $post->add_error('job_news', 'url');
                 }
             }
         }
         // Validate Personal Information
         if (!empty($_POST['person_first'])) {
             $post->add_rules('person_first', 'length[3,100]');
         }
         if (!empty($_POST['person_last'])) {
             $post->add_rules('person_last', 'length[3,100]');
         }
         if (!empty($_POST['person_email'])) {
             $post->add_rules('person_email', 'email', 'length[3,100]');
         }
         $post->add_rules('job_active', 'required', 'between[0,1]');
         $post->add_rules('job_verified', 'required', 'length[0,1]');
         $post->add_rules('job_source', 'alpha', 'length[1,1]');
         $post->add_rules('job_information', 'numeric', 'length[1,1]');
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Yes! everything is valid
             $location_id = $post->location_id;
             // STEP 1: SAVE LOCATION
             $location = new Location_Model($location_id);
             $location->location_name = $post->location_name;
             $location->latitude = $post->latitude;
             $location->longitude = $post->longitude;
             $location->location_date = date("Y-m-d H:i:s", time());
             $location->save();
             // STEP 2: SAVE job
             $job = new Job_Model($id);
             $job->location_id = $location->id;
             $job->user_id = $_SESSION['auth_user']->id;
             $job->job_title = $post->job_title;
             $job->job_description = $post->job_description;
             // Is this new or edit?
             if ($id) {
                 $job->job_datemodify = date("Y-m-d H:i:s", time());
             } else {
                 $job->job_dateadd = date("Y-m-d H:i:s", time());
             }
             // Is this an Email, SMS, Twitter submitted report?
             //XXX: We may get rid of job_mode altogether... ???
             //$_POST
             if (!empty($service_id)) {
                 if ($service_id == 1) {
                     // SMS
                     $job->job_mode = 2;
                 } elseif ($service_id == 2) {
                     // Email
                     $job->job_mode = 3;
                 } elseif ($service_id == 3) {
                     // Twitter
                     $job->job_mode = 4;
                 } elseif ($service_id == 4) {
                     // Laconica
                     $job->job_mode = 5;
                 }
             }
             // job Evaluation Info
             $job->job_active = $post->job_active;
             $job->job_verified = $post->job_verified;
             //Save
             $job->save();
             // Record Approval/Verification Action
             $verify = new Job_Verify_Model();
             $verify->job_id = $job->id;
             $verify->user_id = $_SESSION['auth_user']->id;
             // Record 'Verified By' Action
             $verify->job_verified_date = date("Y-m-d H:i:s", time());
             if ($post->job_active == 1) {
                 $verify->job_verified_status = '1';
             } elseif ($post->job_verified == 1) {
                 $verify->job_verified_status = '2';
             } elseif ($post->job_active == 1 && $post->job_verified == 1) {
                 $verify->job_verified_status = '3';
             } else {
                 $verify->job_verified_status = '0';
             }
             $verify->save();
             // STEP 3: SAVE CATEGORIES
             ORM::factory('Job_Category')->where('job_id', $job->id)->delete_all();
             // Delete Previous Entries
             foreach ($post->job_category as $item) {
                 $job_category = new Job_Category_Model();
                 $job_category->job_id = $job->id;
                 $job_category->j_category_id = $item;
                 $job_category->save();
             }
             // STEP 5: SAVE PERSONAL INFORMATION
             ORM::factory('Job_Person')->where('job_id', $job->id)->delete_all();
             // Delete Previous Entries
             $person = new Job_Person_Model();
             $person->location_id = $location->id;
             $person->job_id = $job->id;
             $person->person_first = $post->person_first;
             $person->person_last = $post->person_last;
             $person->person_email = $post->person_email;
             $person->person_date = date("Y-m-d H:i:s", time());
             $person->save();
             // SAVE AND CLOSE?
             if ($post->save == 1) {
                 url::redirect('admin/jobs/edit/' . $job->id . '/saved');
             } else {
                 url::redirect('admin/jobs/');
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('report'));
             $form_error = TRUE;
         }
     } else {
         if ($id) {
             // Retrieve Current job
             $job = ORM::factory('job', $id);
             if ($job->loaded == true) {
                 // Retrieve Categories
                 $job_category = array();
                 foreach ($job->job_category as $category) {
                     $job_category[] = $category->j_category_id;
                 }
                 // Retrieve Media
                 $job_news = array();
                 // Combine Everything
                 $job_arr = array('location_id' => $job->location->id, 'locale' => $job->locale, 'job_title' => $job->job_title, 'job_description' => $job->job_description, 'latitude' => $job->location->latitude, 'longitude' => $job->location->longitude, 'location_name' => $job->location->location_name, 'country_id' => $job->location->country_id, 'job_category' => $job_category, 'person_first' => $job->job_person->person_first, 'person_last' => $job->job_person->person_last, 'person_email' => $job->job_person->person_email, 'job_active' => $job->job_active, 'job_verified' => $job->job_verified);
                 // Merge To Form Array For Display
                 $form = arr::overwrite($form, $job_arr);
             } else {
                 // Redirect
                 url::redirect('admin/jobs/');
             }
         }
     }
     $this->template->content->id = $id;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     // Retrieve Previous & Next Records
     $previous = ORM::factory('job')->where('id < ', $id)->orderby('id', 'desc')->find();
     $previous_url = $previous->loaded ? url::base() . 'admin/jobs/edit/' . $previous->id : url::base() . 'admin/jobs/';
     $next = ORM::factory('job')->where('id > ', $id)->orderby('id', 'desc')->find();
     $next_url = $next->loaded ? url::base() . 'admin/jobs/edit/' . $next->id : url::base() . 'admin/jobs/';
     $this->template->content->previous_url = $previous_url;
     $this->template->content->next_url = $next_url;
     // Javascript Header
     $this->template->map_enabled = TRUE;
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/jobs_edit_js');
     $this->template->js->default_map = Kohana::config('settings.default_map');
     $this->template->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] || !$form['latitude']) {
         $this->template->js->latitude = Kohana::config('settings.default_lat');
         $this->template->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->template->js->latitude = $form['latitude'];
         $this->template->js->longitude = $form['longitude'];
     }
     // Inline Javascript
     $this->template->content->date_picker_js = $this->_date_picker_js();
     $this->template->content->color_picker_js = $this->_color_picker_js();
     $this->template->content->new_category_toggle_js = $this->_new_category_toggle_js();
 }
Esempio n. 2
0
 /**
  * Submits a new report.
  */
 public function submit($id = false, $saved = false)
 {
     $this->template->header->this_page = 'jobs_submit';
     $this->template->content = new View('jobs_submit');
     // setup and initialize form field names
     $form = array('job_title' => '', 'job_description' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'job_category' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '');
     //	copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     if ($saved == 'saved') {
         $form_saved = TRUE;
     } else {
         $form_saved = FALSE;
     }
     // Initialize Default Values
     /*$form['job_date'] = date("m/d/Y",time());
     		$form['job_hour'] = "12";
     		$form['job_minute'] = "00";
             $form['job_ampm'] = "pm";
             // initialize custom field array
     		$form['custom_field'] = $this->_get_custom_form_fields($id,'',true);
                     //GET custom forms
     		$forms = array();
     		foreach (ORM::factory('form')->find_all() as $custom_forms)
     		{
     			$forms[$custom_forms->id] = $custom_forms->form_title;
     		}
     		$this->template->content->forms = $forms;*/
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         $post->add_rules('job_title', 'required', 'length[3,200]');
         $post->add_rules('job_description', 'required');
         // Validate for maximum and minimum latitude values
         $post->add_rules('latitude', 'required', 'between[-90,90]');
         $post->add_rules('longitude', 'required', 'between[-180,180]');
         $post->add_rules('location_name', 'required', 'length[3,200]');
         $post->add_rules('person_first', 'required', 'length[3,100]');
         $post->add_rules('person_last', 'required', 'length[3,100]');
         $post->add_rules('person_email', 'required', 'email', 'length[3,100]');
         //XXX: Hack to validate for no checkboxes checked
         if (!isset($_POST['job_category'])) {
             $post->job_category = "";
             $post->add_error('job_category', 'required');
         } else {
             $post->add_rules('job_category.*', 'required', 'numeric');
         }
         // Validate Personal Information
         if (!empty($_POST['person_first'])) {
             $post->add_rules('person_first', 'length[3,100]');
         }
         if (!empty($_POST['person_last'])) {
             $post->add_rules('person_last', 'length[3,100]');
         }
         if (!empty($_POST['person_email'])) {
             $post->add_rules('person_email', 'email', 'length[3,100]');
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // STEP 1: SAVE LOCATION
             $location = new Location_Model();
             $location->location_name = $post->location_name;
             $location->latitude = $post->latitude;
             $location->longitude = $post->longitude;
             $location->location_date = date("Y-m-d H:i:s", time());
             $location->save();
             // STEP 2: SAVE job
             $job = new Job_Model();
             $job->location_id = $location->id;
             $job->user_id = 0;
             $job->job_title = $post->job_title;
             $job->job_description = $post->job_description;
             $job->job_dateadd = date("Y-m-d H:i:s", time());
             $job->save();
             // STEP 3: SAVE CATEGORIES
             foreach ($post->job_category as $item) {
                 $job_category = new Job_Category_Model();
                 $job_category->job_id = $job->id;
                 $job_category->j_category_id = $item;
                 $job_category->save();
             }
             // STEP 5: SAVE PERSONAL INFORMATION
             $person = new Job_Person_Model();
             $person->location_id = $location->id;
             $person->job_id = $job->id;
             $person->person_first = $post->person_first;
             $person->person_last = $post->person_last;
             $person->person_email = $post->person_email;
             $person->person_date = date("Y-m-d H:i:s", time());
             $person->save();
             url::redirect('jobs/thanks');
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('report'));
             $form_error = TRUE;
         }
     }
     // Retrieve Country Cities
     $default_country = Kohana::config('settings.default_country');
     $this->template->content->cities = $this->_get_cities($default_country);
     $this->template->content->multi_country = Kohana::config('settings.multi_country');
     $this->template->content->id = $id;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->categories = $this->_get_categories($form['job_category']);
     // Javascript Header
     $this->template->header->map_enabled = TRUE;
     $this->template->header->datepicker_enabled = TRUE;
     $this->template->header->js = new View('reports_submit_js');
     $this->template->header->js->default_map = Kohana::config('settings.default_map');
     $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] || !$form['latitude']) {
         $this->template->header->js->latitude = Kohana::config('settings.default_lat');
         $this->template->header->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->template->header->js->latitude = $form['latitude'];
         $this->template->header->js->longitude = $form['longitude'];
     }
     //include footer form js file
     $footerjs = new View('footer_form_js');
     // Pack the javascript using the javascriptpacker helper
     $myPacker = new javascriptpacker($footerjs, 'Normal', false, false);
     $footerjs = $myPacker->pack();
     $this->template->header->js .= $footerjs;
 }