예제 #1
0
 public function put_index($id, $name, $description, $base64img = null)
 {
     // First, get category to update
     $category = Model_Categories::getById($id);
     // Update main picture
     $category->updateMainPicture($base64img, false);
     // Update category properties
     $category->setProps(['name' => $name, 'description' => $description]);
     Model_Categories::update($category);
     $this->get_index($id);
 }
 public function get_index($id_category = 0, $page = 1)
 {
     if ($id_category === 0) {
         $this->displayList();
     } else {
         $category = Model_Categories::getById($id_category);
         if (empty($category)) {
             $this->response->error('La catégorie demandée est introuvable.', 404);
         } else {
             $this->displayDetails($category, $page);
         }
     }
 }
예제 #3
0
 public function put_index($id, $title, $introduction, $content, $id_category, $base64img = null)
 {
     // First, get article to update
     $article = Model_Articles::getById($id);
     // Get all images from the last version
     $old_images = array_filter($article->getContentImagesUrl(), function ($link) {
         // Keep only images stored in our own server
         return strpos($link, STATIC_URL) !== false;
     });
     // Update main picture
     $article->updateMainPicture($base64img);
     // Update all other properties
     $article->setProps(['title' => $title, 'introduction' => $introduction, 'content' => $content, 'category' => Model_Categories::getById($id_category), 'date_last_update' => $_SERVER['REQUEST_TIME']]);
     // Then get images after this update
     $new_images = $article->getContentImagesUrl();
     // Old images not in new images have to be removed
     $images_to_remove = array_diff($old_images, $new_images);
     foreach ($images_to_remove as $url) {
         $article->deletePicture($url);
     }
     // New images not stored in our server have to be downloaded
     $images_to_download = array_filter($new_images, function ($link) {
         return strpos($link, STATIC_URL) === false;
     });
     $urls_to_replace = [];
     foreach ($images_to_download as $url) {
         $public_url = $article->downloadPicture($url);
         if (empty($public_url)) {
             continue;
         }
         // Stored URL from our server where picture has been downloaded
         $urls_to_replace[$url] = $public_url;
     }
     // Replace all occurences of downloaded images with the new public URLs
     $content = strtr($content, $urls_to_replace);
     $article->prop('content', $content);
     // Don't forget to load author to not erase them!
     $article->load('author');
     Model_Articles::update($article);
     $this->get_index($id);
 }
예제 #4
0
 public function post_index($title, $id_category)
 {
     $newArticle = new Model_Articles(['title' => $title, 'category' => Model_Categories::getById($id_category), 'author' => $this->_currentUser]);
     $newArticle = Model_Articles::add($newArticle);
     $this->response->redirect('articles/edit?id=' . $newArticle->getId(), 201);
 }