/** * Get array of category lang entries * @param int category id */ static function category_langs($category_id = FALSE) { // If we haven't already, grab all category_lang entries at once // We often list all categories at once so its more efficient to just get them all. if (!isset(self::$category_langs)) { $category_langs = ORM::factory('category_lang')->find_all(); self::$category_langs = array(); foreach ($category_langs as $category_lang) { self::$category_langs[$category_lang->category_id][$category_lang->locale]['id'] = $category_lang->id; self::$category_langs[$category_lang->category_id][$category_lang->locale]['category_title'] = $category_lang->category_title; self::$category_langs[$category_lang->category_id][$category_lang->locale]['category_description'] = $category_lang->category_description; } } // Not sure we need to bother with this if ($category_id and isset(self::$category_langs[$category_id])) { return array($category_id => self::$category_langs[$category_id]); } return self::$category_langs; }
function index() { $this->template->content = new View('admin/categories'); $this->template->content->title = Kohana::lang('ui_admin.categories'); // Locale (Language) Array $locales = locale::get_i18n(); // Setup and initialize form field names $form = array ( 'action' => '', 'category_id' => '', 'parent_id' => '', 'category_title' => '', 'category_description' => '', 'category_color' => '', 'category_image' => '', 'category_image_thumb' => '' ); // Add the different language form keys for fields foreach($locales as $lang_key => $lang_name){ $form['category_title_'.$lang_key] = ''; } // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; $parents_array = array(); // 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 Action if ($post->action == 'a') { // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('parent_id','required','numeric'); $post->add_rules('category_title','required', 'length[3,80]'); $post->add_rules('category_description','required'); $post->add_rules('category_color','required', 'length[6,6]'); $post->add_rules('category_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]'); $post->add_callbacks('parent_id', array($this,'parent_id_chk')); // Add the different language form keys for fields foreach($locales as $lang_key => $lang_name){ $post->add_rules('category_title_lang['.$lang_key.']','length[3,80]'); } } // Test to see if things passed the rule checks if ($post->validate()) { $category_id = $post->category_id; $category = new Category_Model($category_id); // Grab languages if they already exist $category_lang = Category_Lang_Model::category_langs($category->id); if(isset($category_lang[$category->id])) { $category_lang = $category_lang[$category->id]; }else{ $category_lang = FALSE; } if( $post->action == 'd' ) { // Delete Action // Delete localizations ORM::factory('category_lang') ->where(array('category_id' => $category_id)) ->delete_all(); // Delete category itself ORM::factory('category') ->where('category_trusted != 1') ->delete($category_id); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.deleted')); } elseif( $post->action == 'v' ) { // Show/Hide Action if ($category->loaded==true) { if ($category->category_visible == 1) { $category->category_visible = 0; } else { $category->category_visible = 1; } $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } elseif( $post->action == 'i' ) { // Delete Image/Icon Action if ($category->loaded==true) { $category_image = $category->category_image; $category_image_thumb = $category->category_image_thumb; if ( ! empty($category_image) AND file_exists(Kohana::config('upload.directory', TRUE).$category_image)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image); } if ( ! empty($category_image_thumb) AND file_exists(Kohana::config('upload.directory', TRUE).$category_image_thumb)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image_thumb); } $category->category_image = null; $category->category_image_thumb = null; $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } elseif( $post->action == 'a' ) { // Save Action $category->parent_id = $post->parent_id; $category->category_title = $post->category_title; $category->category_type = 5; $category->category_description = $post->category_description; $category->category_color = $post->category_color; $category->save(); // Save Localizations foreach($post->category_title_lang as $lang_key => $localized_category_name){ if(isset($category_lang[$lang_key]['id'])) { // Update $cl = ORM::factory('category_lang',$category_lang[$lang_key]['id']); }else{ // Add New $cl = ORM::factory('category_lang'); } $cl->category_title = $localized_category_name; $cl->locale = $lang_key; $cl->category_id = $category->id; $cl->save(); } // Upload Image/Icon $filename = upload::save('category_image'); if ($filename) { $new_filename = "category_".$category->id."_".time(); // Resize Image to 32px if greater Image::factory($filename)->resize(32,32,Image::HEIGHT) ->save(Kohana::config('upload.directory', TRUE) . $new_filename.".png"); // Create a 16x16 version too Image::factory($filename)->resize(16,16,Image::HEIGHT) ->save(Kohana::config('upload.directory', TRUE) . $new_filename."_16x16.png"); // Remove the temporary file unlink($filename); // Delete Old Image $category_old_image = $category->category_image; if ( ! empty($category_old_image) AND file_exists(Kohana::config('upload.directory', TRUE).$category_old_image)) unlink(Kohana::config('upload.directory', TRUE).$category_old_image); // Save $category->category_image = $new_filename.".png"; $category->category_image_thumb = $new_filename."_16x16.png"; $category->save(); } $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.added_edited')); // Empty $form array array_fill_keys($form, ''); } } // No! We have validation errors, we need to show the form again, with the errors else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('category')); $form_error = TRUE; } } // Pagination $pagination = new Pagination(array( 'query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('category') ->where('parent_id','0') ->count_all() )); $categories = ORM::factory('category') ->with('category_lang') ->where('parent_id','0') ->orderby('category_title', 'asc') ->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset); $parents_array = ORM::factory('category') ->where('parent_id','0') ->select_list('id', 'category_title'); // add none to the list $parents_array[0] = "--- Top Level Category ---"; // Put "--- Top Level Category ---" at the top of the list ksort($parents_array); $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->categories = $categories; $this->template->content->parents_array = $parents_array; // Javascript Header $this->template->colorpicker_enabled = TRUE; $this->template->js = new View('admin/categories_js'); $this->template->form_error = $form_error; $this->template->content->locale_array = $locales; $this->template->js->locale_array = $locales; }
/** * Add Edit Categories */ public function index() { $this->template->content = new View('admin/categories'); $this->template->content->title = Kohana::lang('ui_admin.categories'); // Locale (Language) Array $locales = locale::get_i18n(); // Setup and initialize form field names $form = array('action' => '', 'category_id' => '', 'parent_id' => '', 'category_title' => '', 'category_description' => '', 'category_color' => '', 'category_image' => '', 'category_image_thumb' => ''); // Add the different language form keys for fields foreach ($locales as $lang_key => $lang_name) { $form['category_title_' . $lang_key] = ''; } // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; $parents_array = array(); // Check, has the form been submitted, if so, setup validation if ($_POST) { // Fetch the post data $post_data = array_merge($_POST, $_FILES); // Extract category-specific information $category_data = arr::extract($post_data, 'parent_id', 'category_title', 'category_description', 'category_color'); // Extract category image and category languages for independent validation $secondary_data = arr::extract($post_data, 'category_image', 'category_title_lang', 'action'); // Setup validation for the secondary data $post = Validation::factory($secondary_data)->pre_filter('trim', TRUE); // Add validation for the add/edit action if ($post->action == 'a') { $post->add_rules('category_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]'); // Add the different language form keys for fields foreach ($locales as $lang_key => $lang_name) { $post->add_rules('category_title_lang[' . $lang_key . ']', 'length[3,80]'); } } // Category instance for the operation $category = (!empty($_POST['category_id']) and Category_Model::is_valid_category($_POST['category_id'])) ? new Category_Model($_POST['category_id']) : new Category_Model(); // Check the specified action if ($post->action == 'a') { // Test to see if things passed the rule checks if ($category->validate($category_data) and $post->validate()) { // Save the category $category->save(); // Get the category localization $languages = $category->loaded ? Category_Lang_Model::category_langs($category->id) : FALSE; $category_lang = isset($languages[$category->id]) ? $languages[$category->id] : FALSE; // Save localizations foreach ($post->category_title_lang as $lang_key => $localized_category_name) { $cl = isset($category_lang[$lang_key]['id']) ? ORM::factory('category_lang', $category_lang[$lang_key]['id']) : ORM::factory('category_lang'); $cl->category_title = $localized_category_name; $cl->locale = $lang_key; $cl->category_id = $category->id; $cl->save(); } // Upload Image/Icon $filename = upload::save('category_image'); if ($filename) { $new_filename = "category_" . $category->id . "_" . time(); // Resize Image to 32px if greater Image::factory($filename)->resize(32, 32, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".png"); // Create a 16x16 version too Image::factory($filename)->resize(16, 16, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_16x16.png"); // Remove the temporary file unlink($filename); // Delete Old Image $category_old_image = $category->category_image; if (!empty($category_old_image) and file_exists(Kohana::config('upload.directory', TRUE) . $category_old_image)) { unlink(Kohana::config('upload.directory', TRUE) . $category_old_image); } // Save $category->category_image = $new_filename . ".png"; $category->category_image_thumb = $new_filename . "_16x16.png"; $category->save(); } $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.added_edited')); // Empty $form array array_fill_keys($form, ''); } else { // Validation failed // Repopulate the form fields $form = arr::overwrite($form, array_merge($category_data->as_array(), $post->as_array())); // populate the error fields, if any $errors = arr::overwrite($errors, array_merge($category_data->errors('category'), $post->errors('category'))); $form_error = TRUE; } } elseif ($post->action == 'd') { // Delete action if ($category->loaded) { ORM::factory('category_lang')->where(array('category_id' => $category->id))->delete_all(); // @todo Delete the category image // Delete category itself - except if it is trusted ORM::factory('category')->where('category_trusted != 1')->delete($category->id); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.deleted')); } } elseif ($post->action == 'v') { // Show/Hide Action if ($category->loaded) { $category->category_visible = $category->category_visible == 1 ? 0 : 1; $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } elseif ($post->action == 'i') { // Delete Image/Icon Action if ($category->loaded) { $category_image = $category->category_image; $category_image_thumb = $category->category_image_thumb; if (!empty($category_image) and file_exists(Kohana::config('upload.directory', TRUE) . $category_image)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image); } if (!empty($category_image_thumb) and file_exists(Kohana::config('upload.directory', TRUE) . $category_image_thumb)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image_thumb); } $category->category_image = NULL; $category->category_image_thumb = NULL; $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } } // Pagination $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('category')->where('parent_id', '0')->count_all())); $categories = ORM::factory('category')->with('category_lang')->where('parent_id', '0')->orderby('category_position', 'asc')->orderby('category_title', 'asc')->find_all($this->items_per_page, $pagination->sql_offset); $parents_array = ORM::factory('category')->where('parent_id', '0')->select_list('id', 'category_title'); // add none to the list $parents_array[0] = "--- Top Level Category ---"; // Put "--- Top Level Category ---" at the top of the list ksort($parents_array); $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->categories = $categories; $this->template->content->parents_array = $parents_array; // Javascript Header $this->template->colorpicker_enabled = TRUE; $this->template->tablerowsort_enabled = TRUE; $this->template->js = new View('admin/categories_js'); $this->template->form_error = $form_error; $this->template->content->locale_array = $locales; $this->template->js->locale_array = $locales; }
/** * Add Edit Categories */ public function index() { $this->template->content = new View('admin/categories'); $this->template->content->title = Kohana::lang('ui_admin.categories'); // Locale (Language) Array $locales = ush_locale::get_i18n(); // Setup and initialize form field names $form = array('action' => '', 'category_id' => '', 'parent_id' => '', 'category_title' => '', 'category_description' => '', 'category_color' => '', 'category_image' => '', 'category_image_thumb' => '', 'form_auth_token' => ''); // Add the different language form keys for fields foreach ($locales as $lang_key => $lang_name) { $form['category_title_' . $lang_key] = ''; } // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; $parents_array = array(); // Check, has the form been submitted, if so, setup validation if ($_POST) { // Fetch the post data $post_data = array_merge($_POST, $_FILES); // Extract category-specific information $category_data = arr::extract($post_data, 'parent_id', 'category_title', 'category_description', 'category_color'); // Extract category image and category languages for independent validation $secondary_data = arr::extract($post_data, 'category_image', 'category_title_lang', 'action'); // Setup validation for the secondary data $post = Validation::factory($secondary_data)->pre_filter('trim', TRUE); // Add validation for the add/edit action if ($post->action == 'a') { $post->add_rules('category_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]'); // Add the different language form keys for fields foreach ($locales as $lang_key => $lang_name) { $post->add_rules('category_title_lang[' . $lang_key . ']', 'length[3,80]'); } } // Category instance for the operation $category = (!empty($_POST['category_id']) and Category_Model::is_valid_category($_POST['category_id'])) ? new Category_Model($_POST['category_id']) : new Category_Model(); // Check the specified action if ($post->action == 'a') { // Test to see if things passed the rule checks if ($category->validate($category_data) and $post->validate(FALSE)) { // Save the category $category->save(); // Get the category localization $languages = $category->loaded ? Category_Lang_Model::category_langs($category->id) : FALSE; $category_lang = isset($languages[$category->id]) ? $languages[$category->id] : FALSE; // Save localizations foreach ($post->category_title_lang as $lang_key => $localized_category_name) { $cl = isset($category_lang[$lang_key]['id']) ? ORM::factory('category_lang', $category_lang[$lang_key]['id']) : ORM::factory('category_lang'); $cl->category_title = $localized_category_name; $cl->locale = $lang_key; $cl->category_id = $category->id; $cl->save(); } // Upload Image/Icon $filename = upload::save('category_image'); if ($filename) { $new_filename = "category_" . $category->id . "_" . time(); // Name the files for the DB $cat_img_file = $new_filename . ".png"; $cat_img_thumb_file = $new_filename . "_16x16.png"; // Resize Image to 32px if greater Image::factory($filename)->resize(32, 32, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $cat_img_file); // Create a 16x16 version too Image::factory($filename)->resize(16, 16, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $cat_img_thumb_file); // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN if (Kohana::config("cdn.cdn_store_dynamic_content")) { $cat_img_file = cdn::upload($cat_img_file); $cat_img_thumb_file = cdn::upload($cat_img_thumb_file); // We no longer need the files we created on the server. Remove them. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/') . '/'; unlink($local_directory . $new_filename . ".png"); unlink($local_directory . $new_filename . "_16x16.png"); } // Remove the temporary file unlink($filename); // Delete Old Image $category_old_image = $category->category_image; if (!empty($category_old_image)) { if (file_exists(Kohana::config('upload.directory', TRUE) . $category_old_image)) { unlink(Kohana::config('upload.directory', TRUE) . $category_old_image); } elseif (Kohana::config("cdn.cdn_store_dynamic_content") and valid::url($category_old_image)) { cdn::delete($category_old_image); } } // Save $category->category_image = $cat_img_file; $category->category_image_thumb = $cat_img_thumb_file; $category->save(); Event::run('ushahidi_action.category_save', $post); } $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.added_edited')); // Empty $form array array_fill_keys($form, ''); } else { // Validation failed // Repopulate the form fields $form = arr::overwrite($form, array_merge($category_data->as_array(), $post->as_array())); // populate the error fields, if any $errors = arr::overwrite($errors, array_merge($category_data->errors('category'), $post->errors('category'))); $form_error = TRUE; } } elseif ($post->action == 'd' and $post->validate()) { // Delete action if ($category->loaded) { ORM::factory('category_lang')->where(array('category_id' => $category->id))->delete_all(); // Check for all subcategories tied to this category and make them top level $children = ORM::factory('category')->where('parent_id', $category->id)->find_all(); if ($children) { foreach ($children as $child) { $sub_cat = new Category_Model($child->id); $sub_cat->parent_id = 0; $sub_cat->save(); } } // Check for all reports tied to this category to be deleted $result = ORM::factory('incident_category')->where('category_id', $category->id)->find_all(); // If there are reports returned by the query if ($result) { foreach ($result as $orphan) { $orphan_incident_id = $orphan->incident_id; // Check if the report is tied to any other category $count = ORM::factory('incident_category')->where('incident_id', $orphan_incident_id)->count_all(); // If this report is tied to only one category(is uncategorized) if ($count == 1) { // Assign it to the special category for uncategorized reports $orphaned = ORM::factory('incident_category', $orphan->id); $orphaned->category_id = 5; $orphaned->save(); // Deactivate the report so that it's not accessible on the frontend $orphaned_report = ORM::factory('incident', $orphan_incident_id); $orphaned_report->incident_active = 0; $orphaned_report->save(); } else { ORM::factory('incident_category')->delete($orphan->id); } } } // @todo Delete the category image // Delete category itself - except if it is trusted ORM::factory('category')->where('category_trusted != 1')->delete($category->id); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.deleted')); } } elseif ($post->action == 'v' and $post->validate()) { // Show/Hide Action if ($category->loaded) { // Check for all subcategories tied to this category $children = ORM::factory('category')->where('parent_id', $category->id)->find_all(); // Then show/hide subcategories based on status of parent category foreach ($children as $child) { $sub_cat = new Category_Model($child->id); $sub_cat->category_visible = $category->category_visible == 1 ? 0 : 1; $sub_cat->save(); } // Change status of the Parent Category $category->category_visible = $category->category_visible == 1 ? 0 : 1; $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } elseif ($post->action == 'i' and $post->validate()) { // Delete Image/Icon Action if ($category->loaded) { $category_image = $category->category_image; $category_image_thumb = $category->category_image_thumb; // Delete the main image if (!empty($category_image) and file_exists(Kohana::config('upload.directory', TRUE) . $category_image)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image); } // Delete the thumb if (!empty($category_image_thumb) and file_exists(Kohana::config('upload.directory', TRUE) . $category_image_thumb)) { unlink(Kohana::config('upload.directory', TRUE) . $category_image_thumb); } $category->category_image = NULL; $category->category_image_thumb = NULL; $category->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } } // Pagination $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('category')->where('parent_id', '0')->count_all())); $categories = ORM::factory('category')->with('category_lang')->where('parent_id', '0')->orderby('category_title', 'asc')->find_all($this->items_per_page, $pagination->sql_offset); $parents_array = ORM::factory('category')->where('parent_id', '0')->where('category_trusted != 1')->select_list('id', 'category_title'); // add none to the list $parents_array[0] = "--- Top Level Category ---"; // Put "--- Top Level Category ---" at the top of the list ksort($parents_array); $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->categories = $categories; $this->template->content->parents_array = $parents_array; // Javascript Header $this->template->colorpicker_enabled = TRUE; $this->template->tablerowsort_enabled = TRUE; $this->template->js = new View('admin/categories_js'); $this->template->form_error = $form_error; $this->template->content->locale_array = $locales; $this->template->js->locale_array = $locales; }
/** * Get all categories * * @param string response_type - XML or JSON * * @return string */ public function get_categories_by_all() { $items = array(); //will hold the items from the query $data = array(); //items to parse to json $json_categories = array(); //incidents to parse to json $ret_json_or_xml = ''; //will hold the json/xml string to return //find incidents $this->query = "SELECT c.id, c.parent_id, c.category_title as title, c.category_description as description, \n c.category_color as color, c.category_position as position, c.category_image_thumb AS icon\n FROM `" . $this->table_prefix . "category` c\n LEFT JOIN `" . $this->table_prefix . "category` c_parent ON (c.parent_id = c_parent.id) WHERE\n c.category_visible = 1 AND (c_parent.category_visible = 1 OR c.parent_id = 0) ORDER BY c.category_position ASC"; $items = $this->db->query($this->query); $translations = Category_Lang_Model::category_langs(); // Set the no. of records fetched $this->record_count = $items->count(); $i = 0; $this->replar = array(); //assists in proper xml generation $url_prefix = url::base() . Kohana::config('upload.relative_directory') . '/'; foreach ($items as $item) { $item->icon = $item->icon ? $url_prefix . $item->icon : ''; //needs different treatment depending on the output if ($this->response_type == 'json' or $this->response_type == 'jsonp') { $json_categories[] = array("category" => $item, "translations" => isset($translations[$item->id]) ? $translations[$item->id] : array()); } else { $item->translations = array(); if (isset($translations[$item->id])) { foreach ($translations[$item->id] as $lang => $translation) { $translation['lang'] = $lang; $item->translations['translation' . $translation['id']] = array('translation' => $translation); $this->replar[] = 'translation' . $translation['id']; } } $json_categories['category' . $i] = array("category" => $item); $this->replar[] = 'category' . $i; } $i++; } //create the json array $data = array("payload" => array("domain" => $this->domain, "categories" => $json_categories), "error" => $this->api_service->get_error_msg(0)); if ($this->response_type == 'json' or $this->response_type == 'jsonp') { $ret_json_or_xml = $this->array_as_json($data); } else { $ret_json_or_xml = $this->array_as_xml($data, $this->replar); } return $ret_json_or_xml; }
/** * Function to import CSV file referenced by the file handle * @param string $filehandle * @return bool */ function import($file) { // Get contents of CSV file $data = file_get_contents($file); // Normalize new lines, replace ANY unicode new line with \n (should cover Mac OS9, Unix, Windows, etc) $replacedata = preg_replace('/\\R/u', "\n", mb_convert_encoding($data, 'UTF-8')); // Check for preg error, and fall back to original data if (preg_last_error() !== PREG_NO_ERROR) { $replacedata = $data; } // Replace file content file_put_contents($file, $replacedata); if ($filehandle = fopen($_FILES['uploadfile']['tmp_name'], 'r')) { $csvtable = new Csvtable($filehandle); // Set the required columns of the CSV file $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { // If the CSV file is missing any required column, return an error if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = Kohana::lang('import.csv.required_column') . '"' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } // So we can assign category id to incidents, based on category title $this->existing_categories = ORM::factory('category')->select_list('category_title', 'id'); //Since we capitalize the category names from the CSV file, we should also capitlize the //category titles here so we get case insensative behavior. For some reason users don't //always captilize the cateogry names as they enter them in $temp_cat = array(); foreach ($this->existing_categories as $title => $id) { $temp_cat[utf8::strtoupper($title)] = $id; // Add translated titles too $langs = Category_Lang_Model::category_langs($id); if (isset($langs[$id])) { foreach ($langs[$id] as $l) { $temp_cat[utf8::strtoupper($l['category_title'])] = $id; } } } $this->existing_categories = $temp_cat; // So we can check if incident already exists in database $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalreports = count($rows); $this->rownumber = 0; // Loop through CSV rows foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = Kohana::lang('import.incident_exists') . $row['#']; } else { if ($this->import_report($row)) { $this->importedreports++; } else { $this->rollback(); return false; } } } } else { $this->errors[] = Kohana::lang('ui_admin.file_open_error'); } // If we have errors, return FALSE, else TRUE return count($this->errors) === 0; }