Ejemplo n.º 1
0
 public function decayimage_ushahidi_filter_category_save()
 {
     $data = Event::$data;
     // Get the last id that was added to the category table
     $db = new Database();
     $result = $db->query('SELECT MAX(id) id FROM category');
     foreach ($result as $row) {
     }
     // Get the category_id and the image file
     $category = ORM::factory('category')->where('id', $row->id)->find();
     // TODO: the errors below are not used by the calling controller
     $image = Kohana::config('upload.directory', TRUE) . $category->category_image;
     if (!file_exists($image)) {
         $data->add_error('category', 'did_not_find_category_image_for_grayscale');
         return false;
     }
     // TODO: the errors below are not used by the calling controller
     $type = $data['category_image']['type'];
     if (!preg_match('/png/i', $type)) {
         $data->add_error('category', 'invalid_image_type');
         return false;
     }
     $gdimg = imagecreatefrompng($image);
     imagealphablending($gdimg, false);
     imagesavealpha($gdimg, true);
     $new_filename = "decayimage_" . $category->id . "_" . time();
     $new_filename_with_path = Kohana::config('upload.directory', TRUE) . $new_filename . '.png';
     if ($gdimg && imagefilter($gdimg, IMG_FILTER_GRAYSCALE) && imagepng($gdimg, $new_filename_with_path) && imagedestroy($gdimg)) {
         $cat_img_file = $new_filename . ".png";
         $cat_img_thumb_file = $new_filename . "_16x16.png";
         // Also create a thumbnail of the decayimage
         Image::factory($new_filename_with_path)->resize(16, 16, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $cat_img_thumb_file);
         // Delete pre-existing ids of same name
         $cat_id = $category->id;
         ORM::factory('decayimage')->where(array('category_id' => $cat_id))->delete_all();
         // Create the decayimage row
         $decayimage = new Decayimage_Model();
         $decayimage->category_id = $category->id;
         $decayimage->decayimage_image = $cat_img_file;
         $decayimage->decayimage_thumb = $cat_img_thumb_file;
         $decayimage->save();
         Kohana::log('debug', 'sector::decayimage_ushahidi_filter_category_save ' . 'created a decayimage for the added category icon.');
     } else {
         Kohana::log('error', 'sector::decayimage_ushahidi_filter_category_save ' . 'failed to create a decayimage for the added category icon.');
     }
 }
Ejemplo n.º 2
0
 /**
  * Add Edit decayimage 
  */
 public function index()
 {
     // The default decayimage thumb file name
     $default_decayimage_thumb = 'Question_icon_thumb.png';
     $this->template->content = new View('decayimage/settings');
     $this->template->content->title = Kohana::lang('decayimage.decayimage');
     plugin::add_stylesheet('decayimage/css/decayimage');
     // Setup and initialize form field names
     $form = array('action' => '', 'decayimage_id' => '', 'decayimage_image' => '', 'decayimage_file' => '', 'decayimage_thumb' => '', 'category_id' => '');
     // 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) {
         $post = new Validation($_POST);
         $post->pre_filter('trim');
         $post->add_callbacks('category_id', array($this, '_is_valid_category'));
         // if we have an action == 'a' but and a decayimage_id then what we really
         // mean is to perform and edit
         if ($post->action == 'a' && isset($post->category_id)) {
             $post->add_rules('category_id', 'required', 'numeric');
             if ($post->validate() && ($decayimage = ORM::factory('decayimage')->where('category_id', $post->category_id)->find()) && $decayimage->loaded) {
                 $post->decayimage_id = $decayimage->id;
                 $post->action = 'e';
             }
         }
         // Check for action
         if ($post->action == 'a') {
             // Create a new decayimage row
             $decayimage = new Decayimage_Model($post->decayimage_id);
             // Handle the case where we recieve new files
             if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name']) && ($_FILES = Validation::factory($_FILES)->add_rules('decayimage_file', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]')) && $_FILES->validate() && $post->validate()) {
                 // Upload the file and create a thumb
                 $modified_files = $this->_handle_new_decayimage_fileupload(0);
                 if (!$modified_files) {
                     $form_saved = false;
                     $form_error = TRUE;
                     $post->add_error('decayimage', Kohana::lang('decayimage.cant_upload_file'));
                 } else {
                     $decayimage->decayimage_image = $modified_files[0];
                     $decayimage->decayimage_thumb = $modified_files[1];
                     // Update the relevant decayimage from the db
                     $decayimage->category_id = $post->category_id;
                     $decayimage->save();
                     $form_saved = TRUE;
                     $form_action = Kohana::lang('decayimage.added');
                 }
             } else {
                 if ($post->add_rules('decayimage_thumb', 'required', 'length[5,255]') && $post->add_callbacks('decayimage_thumb', array($this, '_is_valid_decayimage_thumb')) && $post->validate()) {
                     // Upload the file and create a thumb
                     $decayimage->decayimage_thumb = $post->decayimage_thumb;
                     // Update the relevant decayimage from the db
                     $decayimage->category_id = $post->category_id;
                     $decayimage->save();
                     $form_saved = TRUE;
                     $form_action = Kohana::lang('decayimage.added');
                 } else {
                     // There was an error in validation
                     $form_error = TRUE;
                     $form = arr::overwrite($form, $post->as_array());
                     $errors = arr::overwrite($errors, $post->errors('decayimage'));
                 }
             }
         } elseif ($post->action == 'e') {
             // Setup validation for new $_FILES
             if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name'])) {
                 $_FILES = Validation::factory($_FILES)->add_rules('decayimage_file', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]');
             } else {
                 $post->add_rules('decayimage_thumb', 'required', 'length[5,255]');
                 $post->add_callbacks('decayimage_thumb', array($this, '_is_valid_decayimage_thumb'));
             }
             // Validate all input
             $post->add_rules('decayimage_id', 'required', 'numeric');
             $post->add_callbacks('decayimage_id', array($this, '_is_valid_decayimage_id'));
             if ($post->validate()) {
                 // Get the relevant decayimage from the db
                 $decayimage = new Decayimage_Model($post->decayimage_id);
                 // If a file was uploaded we will need to convert it to an apropriate icon size
                 if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name']) && $_FILES->validate()) {
                     $modified_files = $this->_handle_new_decayimage_fileupload($post->decayimage_id);
                     if (!$modified_files) {
                         $form_saved = false;
                         $form_error = TRUE;
                         $post->add_error('decayimage', Kohana::lang('decayimage.cant_upload_file'));
                     } else {
                         $decayimage->decayimage_image = $modified_files[0];
                         $decayimage->decayimage_thumb = $modified_files[1];
                     }
                 } else {
                     $decayimage->decayimage_thumb = $post->decayimage_thumb;
                 }
                 // Update the relevant decayimage from the db
                 $decayimage->category_id = $post->category_id;
                 $decayimage->save();
                 $form_saved = TRUE;
                 $form_action = Kohana::lang('decayimage.updated');
             } else {
                 // There were errors
                 $form_error = TRUE;
             }
         } elseif ($post->action == 'd') {
             // TODO: https://github.com/March-hare/decayimage/issues/3
             // Make sure its not the Default entry
             $post->add_rules('decayimage_id', 'required', 'numeric');
             if ($post->validate()) {
                 $decayimage = ORM::factory('decayimage', $post->decayimage_id);
                 if ($decayimage->decayimage_image != 'Question_icon.png') {
                     $decayimage->delete();
                 } else {
                     $form_error = TRUE;
                     $post->add_error('decayimage', Kohana::lang('decayimage.cant_del_default'));
                 }
             } else {
                 $form_error = TRUE;
             }
         } elseif ($post->action == 'r') {
             // TODO: Revert to default decayimage action
             $decayimage = ORM::factory('decayimage')->where('category_id', 0)->find();
             $decayimage->decayimage_image = 'Question_icon.png';
             $decayimage->decayimage_thumb = 'Question_icon_thumb.png';
             $decayimage->save();
         }
         if ($form_error) {
             $form = arr::overwrite($form, $post->as_array());
             $errors = arr::overwrite($errors, $post->errors('decayimage'));
         }
     }
     //get array of categories
     $categories = ORM::factory("category")->where("category_visible", "1")->find_all();
     $cat_array[0] = Kohana::lang('decayimage.default_incident_icon');
     foreach ($categories as $category) {
         $cat_array[$category->id] = $category->category_title;
     }
     //get array of decay images
     $decayimages = ORM::factory("decayimage")->find_all();
     $decayimage_array = array();
     foreach ($decayimages as $decayimage) {
         $decayimage_array[$decayimage->decayimage_thumb] = $decayimage->decayimage_thumb;
     }
     $this->template->content->form_action = $form_action;
     $this->template->content->errors = $errors;
     $this->template->content->cat_array = $cat_array;
     $this->template->content->decayimage_array = $decayimage_array;
     $this->template->content->url_site = url::site();
     $this->template->content->default_decayimage_thumb = $default_decayimage_thumb;
     $this->template->content->decayimages = $decayimages;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->js = new View('decayimage/settings_js');
     $this->template->js->default_decayimage_thumb = $default_decayimage_thumb;
 }