예제 #1
0
 /**
  * Delete Photo
  * @param int $id The unique id of the photo to be deleted
  */
 public static function delete_photo($id)
 {
     $photo = ORM::factory('media', $id);
     $photo_large = $photo->media_link;
     $photo_medium = $photo->media_medium;
     $photo_thumb = $photo->media_thumb;
     if (file_exists(Kohana::config('upload.directory', TRUE) . $photo_large)) {
         unlink(Kohana::config('upload.directory', TRUE) . $photo_large);
     } elseif (Kohana::config("cdn.cdn_store_dynamic_content") and valid::url($photo_large)) {
         cdn::delete($photo_large);
     }
     if (file_exists(Kohana::config('upload.directory', TRUE) . $photo_medium)) {
         unlink(Kohana::config('upload.directory', TRUE) . $photo_medium);
     } elseif (Kohana::config("cdn.cdn_store_dynamic_content") and valid::url($photo_medium)) {
         cdn::delete($photo_medium);
     }
     if (file_exists(Kohana::config('upload.directory', TRUE) . $photo_thumb)) {
         unlink(Kohana::config('upload.directory', TRUE) . $photo_thumb);
     } elseif (Kohana::config("cdn.cdn_store_dynamic_content") and valid::url($photo_thumb)) {
         cdn::delete($photo_thumb);
     }
     // Finally Remove from DB
     $photo->delete();
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 private function _handle_new_decayimage_fileupload($id)
 {
     $filename = upload::save('decayimage_file');
     if ($filename) {
         $new_filename = "decayimage_" . $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);
     } else {
         Kohana::log('error', 'we were not able to save the file upload');
         return false;
     }
     // 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
     if (file_exists($filename)) {
         unlink($filename);
     }
     // Delete Old Image, unless its the default image
     $decayimage = ORM::factory('decayimage')->where('id', $id);
     if ($decayimage && $id != 0) {
         $category_old_image = $decayimage->decayimage_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);
             }
         }
     }
     return array($cat_img_file, $cat_img_thumb_file);
 }