Example #1
0
 /**
  * Function to import a report form a row in the CSV file
  * @param array $row
  * @return bool
  */
 function importreport($row)
 {
     // If the date is not in proper date format
     if (!strtotime($row['INCIDENT DATE'])) {
         $this->errors[] = 'Could not parse incident date "' . htmlspecialchars($row['INCIDENT DATE']) . '" on line ' . ($this->rownumber + 1);
     }
     // If a value of Yes or No is NOT set for approval status for the imported row
     if (isset($row["APPROVED"]) and !in_array($row["APPROVED"], array('NO', 'YES'))) {
         $this->errors[] = 'APPROVED must be either YES or NO on line ' . ($this->rownumber + 1);
     }
     // If a value of Yes or No is NOT set for verified status for the imported row
     if (isset($row["VERIFIED"]) and !in_array($row["VERIFIED"], array('NO', 'YES'))) {
         $this->errors[] = 'VERIFIED must be either YES or NO on line ' . ($this->rownumber + 1);
     }
     if (count($this->errors)) {
         return false;
     }
     // STEP 1: SAVE LOCATION
     if (isset($row['LOCATION'])) {
         $location = new Location_Model();
         $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : '';
         // If we have LATITUDE and LONGITUDE use those
         if (isset($row['LATITUDE']) and isset($row['LONGITUDE'])) {
             $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : '';
             $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : '';
             // Geocode reports which don't have LATITUDE and LONGITUDE
         } else {
             $location_geocoded = Geocoder::geocode_location($location->location_name);
             if ($location_geocoded) {
                 $location->latitude = $location_geocoded[1];
                 $location->longitude = $location_geocoded[0];
             }
         }
         $location->location_date = $this->time;
         $location->save();
         $this->locations_added[] = $location->id;
     }
     // STEP 2: SAVE INCIDENT
     $incident = new Incident_Model();
     $incident->location_id = isset($row['LOCATION']) ? $location->id : 0;
     $incident->user_id = 0;
     $incident->incident_title = $row['INCIDENT TITLE'];
     $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : '';
     $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE']));
     $incident->incident_dateadd = $this->time;
     $incident->incident_active = (isset($row['APPROVED']) and $row['APPROVED'] == 'YES') ? 1 : 0;
     $incident->incident_verified = (isset($row['VERIFIED']) and $row['VERIFIED'] == 'YES') ? 1 : 0;
     $incident->save();
     $this->incidents_added[] = $incident->id;
     // STEP 3: SAVE CATEGORIES
     // If CATEGORY column exists
     if (isset($row['CATEGORY'])) {
         $categorynames = explode(',', trim($row['CATEGORY']));
         // Add categories to incident
         foreach ($categorynames as $categoryname) {
             // There seems to be an uppercase convention for categories... Don't know why
             $categoryname = strtoupper(trim($categoryname));
             // For purposes of adding an entry into the incident_category table
             $incident_category = new Incident_Category_Model();
             $incident_category->incident_id = $incident->id;
             // If category name exists, add entry in incident_category table
             if ($row['CATEGORY'] != '') {
                 if ($categoryname != '') {
                     if (!isset($this->category_ids[$categoryname])) {
                         $this->notices[] = 'There exists no category "' . htmlspecialchars($categoryname) . '" in database yet.' . ' Added to database.';
                         $category = new Category_Model();
                         $category->category_title = $categoryname;
                         // We'll just use black for now. Maybe something random?
                         $category->category_color = '000000';
                         // because all current categories are of type '5'
                         $category->category_visible = 1;
                         $category->category_description = $categoryname;
                         $category->save();
                         $this->categories_added[] = $category->id;
                         // Now category_id is known: This time, and for the rest of the import.
                         $this->category_ids[$categoryname] = $category->id;
                     }
                     $incident_category->category_id = $this->category_ids[$categoryname];
                     $incident_category->save();
                     $this->incident_categories_added[] = $incident_category->id;
                 }
             } else {
                 // Unapprove the report
                 $incident_update = ORM::factory('incident', $incident->id);
                 $incident_update->incident_active = 0;
                 $incident_update->save();
                 // Assign reports to special category for uncategorized reports: NONE
                 $incident_category->category_id = '5';
                 $incident_category->save();
             }
         }
     } else {
         // Unapprove the report
         $incident_update = ORM::factory('incident', $incident->id);
         $incident_update->incident_active = 0;
         $incident_update->save();
         // Assign reports to special category for uncategorized reports: NONE
         $incident_category = new Incident_Category_Model();
         $incident_category->incident_id = $incident->id;
         $incident_category->category_id = '5';
         $incident_category->save();
     }
     return true;
 }