Example #1
0
 public function action_save()
 {
     if (!$this->user->can('Admin_Item_Save')) {
         throw HTTP_Exception::factory('403', 'Permission denied to view admin item save');
     }
     $values = $this->request->post();
     $cfg = Kohana::$config->load('items.image');
     $this->view = NULL;
     if ($values['id'] == 0) {
         $values['id'] = NULL;
     }
     $id = $values['id'];
     $this->response->headers('Content-Type', 'application/json');
     // get the item
     $item = ORM::factory('Item', $values['id']);
     $file = array('status' => 'empty', 'msg' => '');
     $tmp = NULL;
     $upload = NULL;
     if (isset($_FILES['image'])) {
         $image = $_FILES['image'];
         if (!Upload::valid($image)) {
             // error not valid upload
             $file = array('status' => 'error', 'msg' => 'You did not provide a valid file to upload.');
         } elseif (!Upload::image($image, $cfg['width'], $cfg['height'], TRUE)) {
             // not the right image dimensions
             $file = array('status' => 'error', 'msg' => 'You need to provide a valid image (size: :width x :height.', array(':width' => $cfg['width'], ':height' => $cfg['height']));
         } elseif (!Upload::type($image, $cfg['format'])) {
             // not the right image type
             $file = array('status' => 'error', 'msg' => 'You need to provide a valid image (type: :type).', array(':type' => implode(',', $cfg['format'])));
         } else {
             // check if the temp dir exists
             if (!file_exists($cfg['tmp_dir'])) {
                 mkdir($cfg['tmp_dir']);
             }
             // save it temporarily
             $upload = Image::factory($image['tmp_name'])->save($cfg['tmp_dir'] . $image['name'] . Text::random() . '.png');
             $tmp = array('upload' => $upload, 'name' => $image['name']);
             if ($tmp['upload'] != FALSE) {
                 $file['status'] = 'temp';
             } else {
                 $file = array('status' => 'error', 'msg' => 'There was an error uploading your file.');
             }
         }
     }
     if ($file['status'] == 'temp' or $file['status'] == 'empty') {
         try {
             $data = array();
             $type = ORM::factory('Item_Type', $values['type_id']);
             $base_dir = DOCROOT . 'media' . DIRECTORY_SEPARATOR . 'image' . DIRECTORY_SEPARATOR . 'items' . DIRECTORY_SEPARATOR;
             // if we're just changing the item type we'll have to move it a different dir
             if ($type->id != $item->type_id and $file['status'] == 'empty') {
                 $tmp['upload'] = $base_dir . $item->type->img_dir . $item->img;
                 $tmp['name'] = $item->img;
             }
             // move the file to the correct dir if it's possible
             $new_loc = $base_dir . $type->img_dir . $tmp['name'];
             // check if the dir exists
             if (!file_exists($base_dir . $type->img_dir)) {
                 mkdir($base_dir . $type->img_dir);
             }
             if ($file['status'] == 'empty' and $tmp != NULL and file_exists($new_loc)) {
                 $file = array('status' => 'error', 'msg' => 'That filename already exists');
                 $data['type'] = 'error';
                 $data['errors'] = array();
             } else {
                 // if commands are set parse them
                 if (isset($values['commands'])) {
                     $values['commands'] = Item::parse_commands($values['commands']);
                 }
                 // attempt to save the item
                 if ($tmp != NULL) {
                     $values['image'] = $tmp['name'];
                     $item->values($values, array('name', 'status', 'image', 'description', 'unique', 'transferable', 'type_id', 'commands'));
                     $item->save();
                     // if it's saved move the file to the new location
                     if ($item->saved()) {
                         // move the uploaded file to the correct place with the correct name
                         if ($upload != NULL) {
                             $upload = Image::factory($image['tmp_name'])->save($new_loc);
                         } else {
                             copy($tmp['upload'], $new_loc);
                         }
                         $file['status'] = 'success';
                     }
                 } else {
                     $item->values($values, array('name', 'status', 'description', 'unique', 'transferable', 'type_id', 'commands'));
                     $item->save();
                 }
                 $data['row'] = array($item->img(), $item->name, $item->status, $item->type->name, $item->id);
                 $data['action'] = 'saved';
             }
             $data['type'] = $id == NULL ? 'new' : 'update';
             $data['file'] = $file;
             $this->response->body(json_encode($data));
         } catch (ORM_Validation_Exception $e) {
             $errors = array();
             $list = $e->errors('models');
             foreach ($list as $field => $er) {
                 if (!is_array($er)) {
                     $er = array($er);
                 }
                 $errors[] = array('field' => $field, 'msg' => $er);
             }
             $this->response->body(json_encode(array('action' => 'error', 'errors' => $errors)));
         }
     } else {
         $this->response->body(json_encode(array('action' => 'error', 'errors' => array('upload_file' => array('Error uploading your file')))));
     }
 }