Ejemplo n.º 1
0
 /**
  * Save author information
  * 
  * @param array  $data  Author information
  * @return boolean 
  */
 protected function saveAuthor($data)
 {
     $module = $this->getModule();
     $modelAuthor = $this->getModel('author');
     $fakeId = $photo = null;
     if (isset($data['id'])) {
         $id = $data['id'];
         unset($data['id']);
     }
     $fakeId = $this->params('fake_id', 0);
     unset($data['photo']);
     if (empty($id)) {
         $rowAuthor = $modelAuthor->createRow($data);
         $rowAuthor->save();
         if (empty($rowAuthor->id)) {
             return false;
         }
         $id = $rowAuthor->id;
     } else {
         $rowAuthor = $modelAuthor->find($id);
         if (empty($rowAuthor)) {
             return false;
         }
         $rowAuthor->assign($data);
         $rowAuthor->save();
     }
     // Save photo
     $session = Media::getUploadSession($module, 'author');
     if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) {
         $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId};
         if ($uploadInfo) {
             $fileName = $rowAuthor->id;
             $pathInfo = pathinfo($uploadInfo['tmp_name']);
             if ($pathInfo['extension']) {
                 $fileName .= '.' . $pathInfo['extension'];
             }
             $fileName = $pathInfo['dirname'] . '/' . $fileName;
             $rowAuthor->photo = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name'];
             $rowAuthor->save();
         }
         unset($session->{$id});
         unset($session->{$fakeId});
     }
     return $id;
 }
Ejemplo n.º 2
0
 /**
  * Remove image
  * 
  * @return ViewModel
  */
 public function removeImageAction()
 {
     Pi::service('log')->mute();
     $id = $this->params('id', 0);
     $fakeId = $this->params('fake_id', 0);
     $affectedRows = 0;
     $module = $this->getModule();
     if ($id) {
         $rowDraft = $this->getModel('draft')->find($id);
         if ($rowDraft && $rowDraft->image) {
             $thumbUrl = Media::getThumbFromOriginal($rowDraft->image);
             if ($rowDraft->article) {
                 $modelArticle = $this->getModel('article');
                 $rowArticle = $modelArticle->find($rowDraft->article);
                 if ($rowArticle && $rowArticle->image != $rowDraft->image) {
                     // Delete file
                     @unlink(Pi::path($rowDraft->image));
                     @unlink(Pi::path($thumbUrl));
                 }
             } else {
                 @unlink(Pi::path($rowDraft->image));
                 @unlink(Pi::path($thumbUrl));
             }
             // Update db
             $rowDraft->image = '';
             $affectedRows = $rowDraft->save();
         }
     } else {
         if ($fakeId) {
             $session = Media::getUploadSession($module, 'feature');
             if (isset($session->{$fakeId})) {
                 $uploadInfo = $session->{$fakeId};
                 $url = Media::getThumbFromOriginal($uploadInfo['tmp_name']);
                 $affectedRows = unlink(Pi::path($uploadInfo['tmp_name']));
                 @unlink(Pi::path($url));
                 unset($session->{$id});
                 unset($session->{$fakeId});
             }
         }
     }
     echo json_encode(array('status' => $affectedRows ? true : false, 'message' => 'ok'));
     exit;
 }
Ejemplo n.º 3
0
 /**
  * Save topic information
  * 
  * @param  array    $data  Topic information
  * @return boolean
  * @throws \Exception 
  */
 protected function saveTopic($data)
 {
     $module = $this->getModule();
     $model = $this->getModel('topic');
     $fakeId = $image = null;
     if (isset($data['id'])) {
         $id = $data['id'];
         unset($data['id']);
     }
     //$data['active'] = 1;
     $fakeId = $this->params('fake_id', 0);
     unset($data['image']);
     if (isset($data['slug']) && empty($data['slug'])) {
         unset($data['slug']);
     }
     if (empty($id)) {
         $row = $model->createRow($data);
         $row->save();
         $id = $row->id;
     } else {
         $row = $model->find($id);
         if (empty($row->id)) {
             return false;
         }
         $row->assign($data);
         $row->save();
     }
     // Save image
     $session = Media::getUploadSession($module, 'topic');
     if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) {
         $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId};
         if ($uploadInfo) {
             $fileName = $row->id;
             $pathInfo = pathinfo($uploadInfo['tmp_name']);
             if ($pathInfo['extension']) {
                 $fileName .= '.' . $pathInfo['extension'];
             }
             $fileName = $pathInfo['dirname'] . '/' . $fileName;
             $row->image = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name'];
             $row->save();
         }
         unset($session->{$id});
         unset($session->{$fakeId});
     }
     return $id;
 }
Ejemplo n.º 4
0
 /**
  * Save category information
  * 
  * @param  array    $data  Category information
  * @return boolean
  * @throws \Exception 
  */
 protected function saveCategory($data)
 {
     $module = $this->getModule();
     $model = $this->getModel('category');
     $fakeId = $image = null;
     if (isset($data['id'])) {
         $id = $data['id'];
         unset($data['id']);
     }
     $fakeId = $this->params('fake_id', 0);
     $parent = $data['parent'];
     unset($data['parent']);
     unset($data['image']);
     if (isset($data['slug']) && empty($data['slug'])) {
         unset($data['slug']);
     }
     if (empty($id)) {
         $id = $model->add($data, $parent);
         $row = $model->find($id);
     } else {
         $row = $model->find($id);
         if (empty($row)) {
             return $this->jumpTo404(_a('Category is not exists.'));
         }
         $row->assign($data);
         $row->save();
         // Move node position
         $parentNode = $model->getParentNode($id);
         $currentParent = $parentNode['id'];
         if ($currentParent != $parent) {
             $children = $model->getDescendantIds($id);
             if (array_search($parent, $children) !== false) {
                 return $this->jumpTo404(_a('Category cannot be moved to self or a child.'));
             } else {
                 $model->move($id, $parent);
             }
         }
     }
     // Save image
     $session = Media::getUploadSession($module, 'category');
     if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) {
         $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId};
         if ($uploadInfo) {
             $fileName = $row->id;
             $pathInfo = pathinfo($uploadInfo['tmp_name']);
             if ($pathInfo['extension']) {
                 $fileName .= '.' . $pathInfo['extension'];
             }
             $fileName = $pathInfo['dirname'] . '/' . $fileName;
             $row->image = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name'];
             $row->save();
         }
         unset($session->{$id});
         unset($session->{$fakeId});
     }
     return $id;
 }
Ejemplo n.º 5
0
 /**
  * Saving media information
  * 
  * @param  array    $data  Media information
  * @return boolean
  * @throws \Exception 
  */
 protected function saveMedia($data)
 {
     $module = $this->getModule();
     $model = $this->getModel('media');
     $image = null;
     if (isset($data['id'])) {
         $id = $data['id'];
         unset($data['id']);
     }
     $fakeId = $this->params('fake_id', 0);
     unset($data['media']);
     // Getting media info
     $session = Media::getUploadSession($module, 'media');
     if (isset($session->{$id}) || $fakeId && isset($session->{$fakeId})) {
         $uploadInfo = isset($session->{$id}) ? $session->{$id} : $session->{$fakeId};
         if ($uploadInfo) {
             $pathInfo = pathinfo($uploadInfo['tmp_name']);
             if ($pathInfo['extension']) {
                 $data['type'] = strtolower($pathInfo['extension']);
             }
             $data['size'] = filesize(Pi::path($uploadInfo['tmp_name']));
             // Meta
             $metaColumns = array('w', 'h');
             $meta = array();
             foreach ($uploadInfo as $key => $info) {
                 if (in_array($key, $metaColumns)) {
                     $meta[$key] = $info;
                 }
             }
             $data['meta'] = json_encode($meta);
         }
         unset($session->{$id});
         unset($session->{$fakeId});
     }
     // Getting user ID
     if (!isset($data['uid']) || !$data['uid']) {
         $data['uid'] = Pi::user()->getId();
     }
     if (empty($id)) {
         $data['time_upload'] = time();
         $row = $model->createRow($data);
         $row->save();
     } else {
         $data['time_update'] = time();
         $row = $model->find($id);
         if (empty($row)) {
             return false;
         }
         $row->assign($data);
         $row->save();
     }
     // Save image
     if (!empty($uploadInfo)) {
         $fileName = $row->id;
         if ($pathInfo['extension']) {
             $fileName .= '.' . $pathInfo['extension'];
         }
         $fileName = $pathInfo['dirname'] . '/' . $fileName;
         $row->url = rename(Pi::path($uploadInfo['tmp_name']), Pi::path($fileName)) ? $fileName : $uploadInfo['tmp_name'];
         $row->save();
     }
     return $row->id;
 }