Example #1
0
 /**
  * Import videos from youtube
  *
  * @param Request $request
  * @param $match
  * @return string
  */
 public function import(Request $request, $match)
 {
     if ($request->isMethod('post')) {
         $items = [];
         $query = ['part' => 'snippet,id', 'channelId' => $request->get('channel')];
         // Fetch videos
         /** @var Google $sm */
         $sm = NCService::load('SocialMedia.Google');
         while ($response = $sm->request('https://www.googleapis.com/youtube/v3/search', $query)) {
             $response = json_decode($response, true);
             $items = array_merge($items, $response['items']);
             // Move to next page or break
             if (isset($response['nextPageToken']) && $response['nextPageToken']) {
                 $query['pageToken'] = $response['nextPageToken'];
             } else {
                 break;
             }
         }
         // Save videos
         foreach ($items as $video) {
             $id = $video['id']['videoId'];
             $url = 'https://www.youtube.com/watch?v=' . $id;
             $title = $video['snippet']['title'];
             $description = $video['snippet']['description'];
             $iframe = '<iframe class="youtube" width="560" height="315" src="https://www.youtube.com/embed/' . $id . '" frameborder="0" allowfullscreen></iframe>';
             $created_at = strtotime($video['snippet']['publishedAt']);
             // Get image url
             $preview_img = end(array_values($video['snippet']['thumbnails']))['url'];
             // Render import template
             $body = $this->view->render('@assets/templates/youtube.import.twig', ['title' => $title, 'description' => $description, 'player' => $iframe, 'url' => $url, 'short' => '#split#', 'preview' => $preview_img]);
             $entity = new \Post(['author_id' => $this->user->id, 'title' => $title, 'content' => str_replace('#split#', '<!-- split -->', $body), 'moderate' => 1, 'category_id' => $request->get('category'), 'created_at' => $created_at, 'updated_at' => $created_at]);
             $entity->save();
         }
         $this->view->assign('message', $this->lang->translate('post.youtube.done', count($items)));
     }
     return $this->view->render('posts/import.twig', ['title' => $this->lang->translate('post.import'), 'categories' => \PostCategory::as_array(\PostCategory::all())]);
 }
Example #2
0
 /**
  * @param Request $request
  * @param null $matches
  * @return mixed|string
  */
 public function create(Request $request, $matches = null)
 {
     $this->authenticated_only();
     if (!$this->user->can('publicate')) {
         return $this->error403($request);
     }
     $title = $this->lang->translate('post.create');
     // Get post for updating
     $id = intval($matches->get('id', $request->get('id')));
     if ($id > 0) {
         $post = \Post::find(['conditions' => ['id = ? AND author_id = ?', $id, $this->user->id]]);
         $title = $this->lang->translate('post.editing', $post->title);
     } else {
         $last_category = \PostCategory::last();
         if ($last_category) {
             $last_category = $last_category->to_array();
         } else {
             $last_category = null;
         }
         $post = ['title' => $this->lang->translate('post.name'), 'content' => '', 'category' => $last_category];
     }
     // Create or update page
     if ($request->isMethod('post')) {
         if ($post instanceof \Post) {
             $post->title = $request->get('title');
             $post->content = $request->get('content');
             $post->category_id = $request->get('category');
             $post->keywords = $request->get('keywords');
             $post->slug = $request->get('slug');
             $post->moderate = $this->user->can('premoderate_publ');
         } else {
             $post = new \Post(['title' => $request->get('title'), 'content' => $request->get('content'), 'category_id' => $request->get('category'), 'keywords' => $request->get('keywords'), 'slug' => $request->get('slug'), 'author_id' => $this->user->id, 'moderate' => $this->user->can('premoderate_publ')]);
         }
         // Updating instance
         $post->save();
         if (!$post->moderate) {
             // Exporting to social
             static::export($post, $this->view);
             // Ping sitemap
             NCService::load('SocialMedia.Ping');
         }
         return static::json_response(['success' => true, 'message' => $this->lang->translate('form.saved')]);
     }
     if ($post instanceof \Post) {
         $post = $post->to_array();
     }
     return $this->view->render('posts/create.twig', ['post' => $post, 'title' => $title, 'categories' => \PostCategory::as_array(\PostCategory::all())]);
 }