コード例 #1
0
ファイル: CounterTask.php プロジェクト: skybird/phalcon
    public function persistAction($params)
    {
        $counterRank = new CounterRankUtil();
        $counterRank = $counterRank->getCounterRank('posts');
        $post = new Post();
        $count = 0;
        $tableName = $post->getSource();
        foreach ($counterRank->getIterator(100, CounterIterator::PERSIST_WITH_DELETING) as $items) {
            $values = '';
            $count += count($items);
            $ids = '';
            foreach ($items as $post_id => $heat) {
                if ($ids != '') {
                    $ids .= ',';
                }
                $ids .= $post_id;
                $values .= " WHEN id={$post_id} THEN `count`+{$heat} ";
                //                    $values .= "({$post_id}, {$heat}, '', 'private', '', 0)";
            }
            $sql = <<<SQL
UPDATE {$tableName} SET `count` = CASE
    {$values}
    ELSE `count`
END
WHERE `id` IN({$ids})
SQL;
            $post->getWriteConnection()->execute($sql);
        }
        $this->output->writelnComment('Done! Persist ' . $count . ' items;');
    }
コード例 #2
0
ファイル: PostIterator.php プロジェクト: skybird/phalcon
    /**
     *
     * @param int $perSize 每次迭代的数据条数
     * @param bool $withRelations 是否带上关联查询
     * @param int $limit 总条数限制
     */
    public function __construct($perSize = 100, $withRelations = true, $limit = 0)
    {
        $this->post = new Post();
        $this->perSize = $perSize;
        $this->total_items = $this->post->count();
        if ($limit > 0) {
            $this->total_items = max($this->total_items, $limit);
        }
        $this->postsTableName = $this->post->getSource();
        $this->dbConnection = $this->post->getReadConnection();
        $this->textsTableName = with(new Texts())->getSource();
        $this->votesTableName = with(new Votes())->getSource();
        $this->tagsPostsTableName = with(new TagsPosts())->getSource();
        $this->tagsTableName = with(new Tags())->getSource();
        $this->categoriesTableName = with(new CategoriesPosts())->getSource();
        $this->endPosition = floor($this->total_items / $this->perSize);
        if ($withRelations) {
            $this->sql = <<<SQL
SELECT
\tpost.*,
\tvote.upVote,
\tvote.downVote,
\t(SELECT GROUP_CONCAT(`categoryId`) FROM `{$this->categoriesTableName}` WHERE postId=post.id) as categoryIds,
\t(SELECT GROUP_CONCAT(`tagName`) FROM  `{$this->tagsTableName}`  WHERE id IN (SELECT `tagId` FROM `{$this->tagsPostsTableName}` WHERE `postId`=post.id)) as tagNames,
    text.content
FROM `{$this->postsTableName}` as post
LEFT JOIN `{$this->textsTableName}` as text
\tON text.postId=post.id
LEFT JOIN `{$this->votesTableName}` as vote
  ON vote.postId=post.id
SQL;
        } else {
            $this->sql = "SELECT * FROM {$this->postsTableName}";
        }
    }
コード例 #3
0
ファイル: NewsController.php プロジェクト: skybird/phalcon
 public function indexAction()
 {
     $limit = $this->request->getQuery('limit', 'int', 25);
     $limit = $limit > 100 ?: $limit;
     $limit = $limit < 10 ?: $limit;
     $order = $this->request->getQuery('order', 'string', '-created_at');
     $query = array('q' => $this->request->getQuery('q', 'string'), 'status' => 'published', 'tid' => $this->request->getQuery('tid', 'int'), 'uid' => $this->request->getQuery('uid', 'int'), 'cid' => $this->request->getQuery('cid', 'int'), 'username' => $this->request->getQuery('username', 'string'), 'order' => $order, 'limit' => $limit, 'page' => $this->request->getQuery('page', 'int', 1));
     if ($query['cid']) {
         $this->view->setVar('category', Category::findFirst($query['cid']));
     }
     if ($query['uid']) {
         $this->view->setVar('author', UserManager::findFirst($query['uid']));
     }
     if ($query['tid']) {
         $this->view->setVar('tag', Tag::findFirst($query['tid']));
     }
     $post = new Post();
     $posts = $post->findPosts($query);
     $paginator = new \Eva\EvaEngine\Paginator(array("builder" => $posts, "limit" => $limit, "page" => $query['page']));
     $paginator->setQuery($query);
     $pager = $paginator->getPaginate();
     $this->view->setVar('pager', $pager);
     $this->view->setVar('query', $query);
     $tag = new Tag();
     $tags = $tag->getPopularTags(6);
     $this->view->setVar('tags', $tags);
 }
コード例 #4
0
ファイル: Tag.php プロジェクト: skybird/phalcon
    public function getRelatedPosts($postId, $limit = 10)
    {
        $phql = <<<QUERY
SELECT B.postId, B.tagId, SUM( LOG( 100 / C.count ) ) AS weight
FROM Eva\\EvaBlog\\Entities\\TagsPosts AS A
LEFT JOIN Eva\\EvaBlog\\Entities\\TagsPosts AS B ON A.tagId = B.tagId
LEFT JOIN Eva\\EvaBlog\\Entities\\Tags AS C ON B.tagId = C.id
WHERE A.postId = {$postId}
AND B.postId != {$postId}
GROUP BY B.postId
ORDER BY weight DESC
LIMIT {$limit}
QUERY;
        $manager = $this->getModelsManager();
        $query = $manager->createQuery($phql);
        $results = $query->execute();
        $posts = null;
        $idArray = array();
        if ($results->count() > 0) {
            foreach ($results as $result) {
                $idArray[] = $result->postId;
            }
            $postModel = new Post();
            $postsQueryBuilder = $postModel->findPosts(array('id' => implode(',', $idArray)));
            $posts = $postsQueryBuilder->getQuery()->execute();
        }
        return $posts;
    }
コード例 #5
0
ファイル: PostController.php プロジェクト: skybird/phalcon
 public function listAction()
 {
     $limit = $this->dispatcher->getParam('limit');
     $limit = $limit ? $limit : 25;
     /** @noinspection PhpDuplicateArrayKeysInspection */
     $query = array('q' => $this->dispatcher->getParam('q'), 'status' => $this->dispatcher->getParam('status'), 'uid' => $this->dispatcher->getParam('uid'), 'cid' => $this->dispatcher->getParam('cid'), 'tid' => $this->dispatcher->getParam('tid'), 'has_image' => $this->dispatcher->getParam('has_image', 'int'), 'min_created_at' => $this->dispatcher->getParam('min_created_at', 'int'), 'username' => $this->dispatcher->getParam('username'), 'order' => $this->dispatcher->getParam('order'), 'limit' => $limit, 'page' => $this->dispatcher->getParam('page'));
     $post = new Models\Post();
     $posts = $post->findPosts($query);
     $paginator = new \Eva\EvaEngine\Paginator(array("builder" => $posts, "limit" => $query['limit'], "page" => $query['page']));
     $paginator->setQuery($query);
     $pager = $paginator->getPaginate();
     return $pager;
 }
コード例 #6
0
ファイル: PostController.php プロジェクト: skybird/phalcon
 /**
  * @operationName("Edit Post")
  * @operationDescription("Edit Post")
  */
 public function editAction()
 {
     $this->view->changeRender('admin/post/create');
     $post = Models\Post::findFirst($this->dispatcher->getParam('id'));
     if (!$post) {
         throw new Exception\ResourceNotFoundException('ERR_BLOG_POST_NOT_FOUND');
     }
     $form = new Forms\PostForm();
     $form->setModel($post);
     $form->addForm('text', 'Eva\\EvaBlog\\Forms\\TextForm');
     $this->view->setVar('form', $form);
     $this->view->setVar('item', $post);
     if (!$this->request->isPost()) {
         return false;
     }
     $data = $this->request->getPost();
     if (!$form->isFullValid($data)) {
         return $this->showInvalidMessages($form);
     }
     try {
         $form->save('updatePost');
     } catch (\Exception $e) {
         return $this->showException($e, $form->getModel()->getMessages());
     }
     $this->flashSession->success('SUCCESS_POST_UPDATED');
     return $this->redirectHandler('/admin/post/edit/' . $post->id);
 }
コード例 #7
0
ファイル: PostUrl.php プロジェクト: skybird/phalcon
 public function __invoke($post)
 {
     if ($post instanceof Posts) {
         return $post->getUrl();
     }
     return Post::getUrlByPostArr($post);
 }
コード例 #8
0
ファイル: NewsController.php プロジェクト: skybird/phalcon
 public function listAction()
 {
     $limit = $this->request->getQuery('limit', 'int', 25);
     $limit = $limit > 100 ? 100 : $limit;
     $limit = $limit < 10 ? 10 : $limit;
     $order = $this->request->getQuery('order', 'string', '-created_at');
     $query = array('q' => $this->request->getQuery('q', 'string'), 'status' => $this->request->getQuery('status', 'string', 'published'), 'uid' => $this->request->getQuery('uid', 'int'), 'cid' => $this->request->getQuery('cid', 'int'), 'username' => $this->request->getQuery('username', 'string'), 'order' => $order, 'limit' => $limit, 'page' => $this->request->getQuery('page', 'int', 1));
     if ($query['cid']) {
         $this->view->setVar('category', Category::findFirst($query['cid']));
     }
     $post = new Post();
     $posts = $post->findPosts($query);
     $paginator = new \Eva\EvaEngine\Paginator(array("builder" => $posts, "limit" => $limit, "page" => $query['page']));
     $paginator->setQuery($query);
     $pager = $paginator->getPaginate();
     $this->view->setVar('pager', $pager);
     return $paginator;
 }
コード例 #9
0
ファイル: IndexController.php プロジェクト: skybird/phalcon
 public function tutorialAction()
 {
     $id = $this->dispatcher->getParam('id');
     $id = $id ? $id : 'huangjingainian';
     $post = Post::findFirst(array('conditions' => "slug = :slug:", 'bind' => array('slug' => $id)));
     if (!$post) {
         throw new Exception\ResourceNotFoundException('Request post not found');
     }
     $this->view->setVar('post', $post);
 }
コード例 #10
0
ファイル: Threads.php プロジェクト: skybird/phalcon
 public function getTitle()
 {
     $postId = str_replace('post_', '', $this->uniqueKey);
     //        p($postId);
     $post = Post::findFirst($postId);
     //        p($post);
     if ($post) {
         return $post->title;
     }
     return $this->title;
 }
コード例 #11
0
ファイル: NewsController.php プロジェクト: skybird/phalcon
 public function newsAction()
 {
     $id = $this->dispatcher->getParam('id');
     if (is_numeric($id)) {
         $post = Post::findFirst($id);
     } else {
         $post = Post::findFirstBySlug($id);
     }
     if (!$post || $post->status != 'published') {
         throw new Exception\ResourceNotFoundException('Request post not found');
     }
     $this->view->setVar('post', $post);
 }
コード例 #12
0
ファイル: BlogListener.php プロジェクト: skybird/phalcon
 public function afterSave($event, Post $post)
 {
     if (!$post->id) {
         return;
     }
     $postArr = $post->toArray();
     $postArr['tagNames'] = $post->getTagString();
     $categoryIds = '';
     $postArr['content'] = strip_tags($post->text->content);
     $postArr['summary'] = strip_tags($post->summary);
     if ($post->categories) {
         foreach ($post->categories as $category) {
             if ($categoryIds != '') {
                 $categoryIds .= ',';
             }
             $categoryIds .= $category->id;
         }
     }
     $postArr['categoryIds'] = $categoryIds;
     $postArr['upVote'] = $postArr['upVote'] ? $postArr['upVote'] : 0;
     $es = new ElasticsearchUtil();
     $es->index('wallstreetcn', 'article', $postArr, $post->id);
     //        (json_encode($post->categories));
 }
コード例 #13
0
ファイル: ProcessController.php プロジェクト: skybird/phalcon
 /**
  * @operationName("Check post slug unique")
  * @operationDescription("Check post slug unique")
  */
 public function slugAction()
 {
     $slug = $this->request->get('slug');
     $exclude = $this->request->get('exclude');
     if ($slug) {
         $conditions = array("columns" => array('id'), "conditions" => 'slug = :slug:', "bind" => array('slug' => $slug));
         if ($exclude) {
             $conditions['conditions'] .= ' AND id != :id:';
             $conditions['bind']['id'] = $exclude;
         }
         $post = Models\Post::findFirst($conditions);
     } else {
         $post = array();
     }
     if ($post) {
         $this->response->setStatusCode('409', 'Post Already Exists');
     }
     return $this->response->setJsonContent(array('exist' => $post ? true : false, 'id' => $post ? $post->id : 0));
 }
コード例 #14
0
ファイル: PostSearcher.php プロジェクト: skybird/phalcon
 public function initialize()
 {
     parent::initialize();
     $this->es_config = $this->getDI()->getConfig()->EvaSearch->elasticsearch->toArray();
     $this->es_client = new Client(array('hosts' => $this->es_config['servers']));
 }
コード例 #15
0
ファイル: PostsController.php プロジェクト: skybird/phalcon
 /**
  *
  * @SWG\Api(
  *   path="/admin/posts/{postId}",
  *   description="Post related api",
  *   produces="['application/json']",
  *   @SWG\Operations(
  *     @SWG\Operation(
  *       method="DELETE",
  *       summary="Delete post by ID",
  *       notes="Returns deleted post",
  *       @SWG\Parameters(
  *         @SWG\Parameter(
  *           name="postId",
  *           description="ID of post",
  *           paramType="path",
  *           required=true,
  *           type="integer"
  *         )
  *       )
  *     )
  *   )
  * )
  * @operationName("删除文章")
  * @operationDescription("删除文章")
  */
 public function deleteAction()
 {
     $id = $this->dispatcher->getParam('id');
     $post = Models\Post::findFirst($id);
     if (!$post) {
         throw new Exception\ResourceNotFoundException('Request post not exist');
     }
     $postinfo = $post->dump(Models\Post::$defaultDump);
     try {
         $post->removePost($id);
         return $this->response->setJsonContent($postinfo);
     } catch (\Exception $e) {
         return $this->showExceptionAsJson($e, $post->getMessages());
     }
 }
コード例 #16
0
ファイル: PostsController.php プロジェクト: skybird/phalcon
 /**
  *
  * @SWG\Api(
  *   path="/posts/{postId}",
  *   description="Posts Manage api",
  *   produces="['application/json']",
  *   @SWG\Operations(
  *     @SWG\Operation(
  *       method="GET",
  *       summary="Find post by ID",
  *       notes="Returns a post based on ID",
  *       @SWG\Parameters(
  *         @SWG\Parameter(
  *           name="postId",
  *           description="ID of post",
  *           paramType="path",
  *           required=true,
  *           type="integer"
  *         )
  *       )
  *     )
  *   )
  * )
  */
 public function getAction()
 {
     $id = $this->dispatcher->getParam('id');
     $postModel = new Models\Post();
     $post = $postModel->findFirst($id);
     if (!$post || $post->status != 'published') {
         throw new Exception\ResourceNotFoundException('Request post not exist');
     }
     $post = $post->dump(Models\Post::$defaultDump);
     return $this->response->setJsonContent($post);
 }