Ejemplo n.º 1
0
 public function add_homeimage()
 {
     $this->_checkPermission();
     $data = $_POST['homeimage'];
     Flash::set('postdata', $data);
     $image = $_POST['upload'];
     $path = str_replace('..', '', $image['path']);
     $path2 = str_replace('..', '', $image['path2']);
     $overwrite = false;
     // verification
     if (empty($data['title'])) {
         Flash::set('error', __('You have to specify a homeimage title!'));
         redirect(get_url('homeimage/create'));
     }
     // if (empty($data['url'])){
     // 	Flash::set('error', __('You have to specify the URL!'));
     // 	redirect(get_url('homeimage/create'));
     // }
     if (isset($_FILES)) {
         // no image file selected
         if (empty($_FILES['upload_file']['name'])) {
             Flash::set('error', __('You have to select a image to be uploaded!'));
             redirect(get_url('homeimage/create'));
         }
     } else {
         Flash::set('error', __('You have to select a image to be uploaded!'));
         redirect(get_url('homeimage/create'));
     }
     $homeimage = new HomeImage($data);
     $homeimage->created_by_id = AuthUser::getId();
     $homeimage->created_on = date('Y-m-d H:i:s');
     if (!$homeimage->save()) {
         Flash::set('error', __('Home Image is not added!'));
         redirect(get_url('homeimage/create'));
     } else {
         if (isset($_FILES)) {
             $homeimage_id = $homeimage->lastInsertId();
             $file = $this->upload_file($_FILES['upload_file']['name'], FILES_DIR . '/homeimage/images/', $_FILES['upload_file']['tmp_name'], $overwrite, $homeimage_id);
             $file_hover = $this->upload_file($_FILES['upload_file_hover']['name'], FILES_DIR . '/homeimage/images/', $_FILES['upload_file_hover']['tmp_name'], $overwrite, $homeimage_id);
             if ($file === false) {
                 Flash::set('error', __('Home Main Image has not been uploaded!'));
             }
             if ($file_hover === false) {
                 Flash::set('error', __('Home Hover Image has not been uploaded!'));
             }
             //Add Image to database
             $data = $_POST['homeimage'];
             Flash::set('post_data', (object) $data);
             $homeimage = Record::findByIdFrom('HomeImage', $homeimage_id);
             if (!$homeimage->update('HomeImage', array('filename' => $file), 'id=' . $homeimage_id)) {
                 Flash::set('error', __('Image has not been updated.'));
             } else {
                 if (!$homeimage->update('HomeImage', array('filename_hover' => $file_hover), 'id=' . $homeimage_id)) {
                     Flash::set('error', __('Image has not been updated.'));
                 } else {
                     Flash::set('success', __('Home Image has been updated!'));
                     if (isset($_POST['commit'])) {
                         redirect(get_url('homeimage'));
                     } else {
                         redirect(get_url('homeimage/view/' . $homeimage->id));
                     }
                 }
             }
         }
         Flash::set('success', __('Home Image has been added!'));
     }
     redirect(get_url('homeimage'));
 }