Example #1
0
 /**
  * @param $id
  */
 public static function delete($id)
 {
     $post = Posts::findByPK($id);
     if (!Request::is_authenticated()) {
         Response::redirect('');
     } else {
         if (Request::user()->id !== $post['id_account']) {
             Session::push('flash-message', 'You does not have permission to delete the other Member\'s post!');
             Response::redirect('');
         }
     }
     # perform the post deletion
     Posts::delete($id);
     # redirect to main page
     Response::redirect('');
 }
Example #2
0
 /**
  * @inheritdoc
  * @return Result parsing group.
  */
 public static function parseAllSources()
 {
     $query = Sources::find();
     $sources = $query->active()->all();
     $i = 0;
     foreach ($sources as $source) {
         if ($source->ownerId) {
             $param['ownerId'] = $source->ownerId;
         } else {
             $param['ownerId'] = NULL;
         }
         $results = Yii::$app->vkApi->createRequest()->setUrl('wall.get')->setData(['owner_id' => $param['ownerId'], 'domain' => $source->domain, 'offset' => $source->postOffset, 'count' => $source->postCount, 'filter' => 'owner'])->send();
         if ($results->isOk) {
             $responses = $results->data['response'];
             foreach ($responses as $response) {
                 if (is_array($response)) {
                     $post = new Posts();
                     $post->recordId = $response['id'];
                     $post->sources_id = $source->id;
                     $prepare_content = preg_replace("/([^&]#[a-zA-zа-яА-я0-9]*)/", "", stripcslashes($response['text']));
                     $prepare_content = preg_replace("/\\[(.+?)\\]/", "", $prepare_content);
                     $post->content = $prepare_content;
                     $title = mb_substr($post->content, 0, mb_stripos($post->content, "<br"));
                     if (!$title) {
                         $title = $post->content;
                     }
                     $post->title = $title ? $title : NULL;
                     $post->slug = Inflector::slug($post->title);
                     if ($response['post_type'] != 'post') {
                         break;
                     }
                     try {
                         if ($post->save()) {
                             foreach ($response['attachments'] as $attachment) {
                                 if ($attachment['type'] == 'photo') {
                                     $photo = new Photos();
                                     $photo->id_posts = $post->id;
                                     $photo->text = $attachment['photo']['text'];
                                     if (isset($attachment['photo']['src'])) {
                                         $photo->link = $attachment['photo']['src'];
                                     }
                                     if (isset($attachment['photo']['src_small'])) {
                                         $photo->link = $attachment['photo']['src_small'];
                                     }
                                     if (isset($attachment['photo']['src_big'])) {
                                         $photo->link = $attachment['photo']['src_big'];
                                     }
                                     if (isset($attachment['photo']['src_xbig'])) {
                                         $photo->link = $attachment['photo']['src_xbig'];
                                     }
                                     if (isset($attachment['photo']['src_xxbig'])) {
                                         $photo->link = $attachment['photo']['src_xxbig'];
                                     }
                                     if (isset($attachment['photo']['src_xxxbig'])) {
                                         $photo->link = $attachment['photo']['src_xxxbig'];
                                     }
                                     $photo->save();
                                 } else {
                                     $post->delete();
                                     $i--;
                                 }
                             }
                             $i++;
                         }
                         $post->slug = $post->id . "-" . $post->slug;
                         $post->save();
                     } catch (yii\db\IntegrityException $e) {
                         //nothing
                     }
                 }
             }
         }
         sleep(5);
     }
     if ($i < 0) {
         $i = 0;
     }
     return Yii::t("app", '{n, plural, =0{Not added new records} =1{# record added} few{# records added} other{# added records}}', ['n' => $i]);
 }