Example #1
0
 public function ApiPostsSearchAction()
 {
     if (!$this->IsLoggedIn()) {
         return $this->ResponseNotLoggedIn();
     }
     $Search = $this->Request->GetGET('search');
     if (!is_array($Search) || !isset($Search['keywords'])) {
         return $this->ResponseWrongData();
     }
     $PostCount = $this->Request->GetGET('post-count');
     if ($PostCount == null) {
         $PostCount = 9999999999.0;
     }
     $Query = new \Pvik\Database\Generic\Query('Posts');
     $ConditionString = 'LEFT JOIN PostShares as PostShares ON PostShares.PostId = Posts.PostId';
     $ConditionString .= ' WHERE PostShares.UserId = "%s"';
     $Query->AddParameter($this->GetUserId());
     foreach ($Search['keywords'] as $Keyword) {
         $Query->AddParameter('%' . $Keyword . '%');
         $ConditionString .= ' AND Posts.SearchHelper LIKE "%s"';
     }
     $Query->SetConditions($ConditionString);
     $Query->SetOrderBy('ORDER BY Posts.Updated DESC LIMIT 0,%s');
     $Query->AddParameter($PostCount);
     $Posts = $Query->Select();
     // optimization for tags
     $Posts->LoadList('PostsTags->Tag');
     // optimization for postshare
     $Posts->LoadList('PostShares');
     // optimization for comments
     $Posts->LoadList('Comments');
     $Data = array();
     $Data['posts'] = array();
     foreach ($Posts as $Post) {
         $Data['posts'][] = $Post->ToArray();
     }
     $this->ResponseSuccess($Data);
 }
Example #2
0
 public function SetTags(array $TagTitles)
 {
     $TagTitles = $this->ValidateTagTitles($TagTitles);
     // delete previous tags refering on dashboardpost
     \Pvik\Database\SQL\Manager::GetInstance()->DeleteWithParameters('DELETE FROM PostsTags WHERE PostsTags.PostId = %s', array($this->PostId));
     // CAUTION: DashboardEntrieTags and Tags in the cache maybe still have a reference to this object
     //                // manually delete cache posts
     $CachePostsTags = \Pvik\Database\Generic\ModelTable::Get('PostsTags')->GetCache()->GetAllCacheInstances();
     foreach ($CachePostsTags as $CachePostTag) {
         /* @var $CachePostTag PostsTags */
         if ($CachePostTag->PostId == $this->PostId) {
             \Pvik\Database\Generic\ModelTable::Get('PostsTags')->GetCache()->Delete($CachePostTag);
         }
     }
     // clear all reference keys
     $this->SetFieldData('PostsTags', null);
     $TagModels = array();
     if (count($TagTitles) > 0) {
         // search for existing tags
         $TitleInStateMent = '';
         $First = true;
         $Query = new \Pvik\Database\Generic\Query('Tags');
         foreach ($TagTitles as $TagTitle) {
             if ($First) {
                 $First = false;
             } else {
                 $TitleInStateMent .= ',';
             }
             $TitleInStateMent .= '"%s"';
             $Query->AddParameter($TagTitle);
         }
         $Query->SetConditions('WHERE title IN (' . $TitleInStateMent . ')');
         $TagModels = $Query->Select();
         // get existing tags
         foreach ($TagModels as $TagModel) {
             // delete found tag
             unset($TagTitles[array_search($TagModel->Title, $TagTitles)]);
         }
         // now iterate the non existing tag titles
         foreach ($TagTitles as $TagTitle) {
             // create tag
             $TagModel = new Tag();
             $TagModel->Title = $TagTitle;
             $TagModel->Insert();
             // append to list
             $TagModels->append($TagModel);
         }
     }
     // insert relations tags - dashboardpost
     foreach ($TagModels as $TagModel) {
         $PostsTags = new \Dashbird\Model\Entities\PostsTags();
         $PostsTags->PostId = $this->PostId;
         $PostsTags->TagId = $TagModel->TagId;
         $PostsTags->Insert();
     }
 }