Location: PHPKode > projects > Web2.0 community/group web site > web20groups_pre-alpha-2/include/Controllers/BlogController.php
<?php
    /* uncomment for debugging
    require_once('Zend/Loader.php');
     Zend_Loader::registerAutoload();
    */
class BlogController extends BaseObjectControllerAction
{
    const CONTROLLER= 'blog';
    const DATA_TABLE= 'blog_posts';

    public function init()
    {
        parent::init(self::CONTROLLER, self::DATA_TABLE);
    }
        
    public function viewAction()
    {
        $options= $this->preprocessViewParams();

        $this->blogposts= array();

        $options['limit']= $this->user_settings->pp_blogposts;
        $options['order']=  'p.ts_created desc';
        $options['status']= 'L';

        if (strlen($this->param_tag) > 0) {
            $this->blogposts= DatabaseObject_BlogPost::GetPosts($this->db,
                                                                    $options);
            $this->total_objcount= DatabaseObject_BlogPost::GetPosts($this->db,
                                               $this->genCountOptions($options)); 
        }
        else if (strlen($this->param_url) > 0) {
            $blogpost= new DatabaseObject_BlogPost($this->db);
            if ($blogpost->load($this->param_url, 'url')){
                $this->blogposts= array($blogpost);
                $this->total_objcount= 1;
            }
        }
        else if ($this->param_drafts != '') {
            $options['status']= 'D';
            $this->blogposts= DatabaseObject_BlogPost::GetPosts($this->db,$options);
            $this->total_objcount= DatabaseObject_BlogPost::GetPosts($this->db,
                                               $this->genCountOptions($options));

            $this->pagetitle= 'Draft ' . ucwords($this->_pluraltype);
            $this->view->content_title= 'Draft ' . ucwords($this->_pluraltype);

            $this->selected_usermenu_item= 'Draft '. ucwords($this->_pluraltype);
        }
        else if ($this->param_unread != ''){
            $this->blogposts= DatabaseObject_BlogPost::getNewForUser($this->db,
                                                      $this->user_id, $options);
            $this->total_objcount= DatabaseObject_BlogPost::getNewForUser($this->db,
                                  $this->user_id, $this->genCountOptions($options));
        }
        else if($this->param_starred != '') {
            $this->blogposts= DatabaseObject_BlogPost::getStarredByUser($this->db,
                                                      $this->user_id, $options);
            $this->total_objcount= DatabaseObject_BlogPost::getStarredByUser($this->db,
                                  $this->user_id, $this->genCountOptions($options));
        }
        else {
            if (($this->param_month == 0) &&($this->param_year == 0)) {
                $options= DatabaseObject_BlogPost::getLatestParams($this->db, $options);
            
                $ts= strtotime($options['to']);
                $month= date('n', $ts);
                $year= date('Y', $ts);
                $this->currentpage = $this->currentpage . $year . '/' . $month ;
            }

            $this->blogposts= DatabaseObject_BlogPost::GetPosts($this->db,$options); 

            $this->total_objcount= DatabaseObject_BlogPost::GetPosts($this->db,
                                                 $this->genCountOptions($options));

            if (isset($this->submenu1)&&!isset($this->selected_submenu1_item) && (count($this->blogposts)>0)) {
                $this->selected_submenu1_item= $this->submenu1[0]->name;
            }
        }

        if (count($this->blogposts) == 0) {
            //do nothing
        }

        $this->objcount= 0;
        $this->objects= array();
        foreach ($this->blogposts as $post_id => $blogpost) {
            $this->objects[$this->objcount]= $this->fillObjectInfo($blogpost);
            $this->objcount++;
        }

        //Fill action specific view variables
        if (count($this->objects) > 0) {
            $this->view->objects= $this->objects;
        }
        
        //call common functions, independent of action
        $this->addUserMenu();

        $this->generatePageInfo($this->user_settings->pp_blogposts);

        $this->fillViewVariables();
    }

    protected function fillObjectInfo(DatabaseObject_BlogPost $blogpost)
    {
        //this must be new options array, used for getting comments,tags etc
        $options= array('order' => 'p.ts_created desc'); 

        //Ideally this should be created from a factory class
        $blog_object= new Utility_Object();

        $blog_object->type= $this->_type;
        $blog_object->author= DatabaseObject_User::
                                    getUserNamefromId($this->db,
                                                $blogpost->user_id);
        
        if ($blogpost->user_id == $this->user_id) {
            $blog_object->owner= true;
        }
        //copy all other members from blog
        $blog_object->status= $blogpost->status;
        $blog_object->ts_created= $blogpost->ts_created;
        $blog_object->ts_last_modified= $blogpost->ts_last_modified;
        $blog_object->name = $blogpost->profile->title;   //there is no name for the poll
        $blog_object->content = $blogpost->profile->content;
        
        $blog_object->url = $blogpost->url;
        $blog_object->tags = $blogpost->getTags();
        $blog_object->objid = $blogpost->getId();

        $options['object_id']= $blogpost->getId();
        $options['order']= 'p.ts_created'; 
        $this->comments = DatabaseObject_Comment::GetComments($this->db,'blog_posts_comments', $options);
        $comment_ids = array_keys($this->comments);
        if (count($comment_ids) != 0 ) {
            foreach ($this->comments as $key=>$value) {
                $this->comments[$key]->username= DatabaseObject_User::
                                                  getUserNamefromId($this->db,
                                                             $value->user_id);
            }
            $blog_object->comments= $this->comments;
        }

        $blog_object->unread= DatabaseObject_Unread::IsExists($this->db,
                                                        $blogpost->getId(),
                                                        $this->user_id,
                                       DatabaseObject_BlogPost::getObjType());
        $blog_object->starred= DatabaseObject_Starred::IsExists($this->db,
                                                        $blogpost->getId(),
                                                        $this->user_id,
                                       DatabaseObject_BlogPost::getObjType());

        return $blog_object;
    }

    public function editAction()
    {
        $this->view->extra_scripts= array(  
                                            '/js/filemanager.js'
                                          );
        $this->view->extra_styles= array(
                                            '/css/blogedit.css'
                                          );

        $this->preprocessEditParams();

        $request = $this->getRequest();

        $post_id= (int) $request->getParam('id');
        if (strlen($this->param_delete) >0) {
            $blogpost= new DatabaseObject_BlogPost($this->db);
            if ($blogpost->load($post_id)){
                if ($blogpost->user_id == $this->user_id) {
                    $flag= $blogpost->delete();
                    //$this->_redirect($this->getUrl('view'));
                    $this->view->actionsuccess= "Deleting Blog post is successful";
                    $this->addUserMenu();
                    $this->fillViewVariables();
                    return;
                }
            }
        }
        else if (strlen($this->param_url) >0) {
            $blogpost= new DatabaseObject_BlogPost($this->db);
            if ($blogpost->load($this->param_url, 'url')){
                if ($blogpost->user_id == $this->user_id) { //todo: need to implement this in some access control mechanism
                    $post_id= $blogpost->getId();
                    $this->selected_usermenu_item= 'Edit Blog';
                }
            }
        }
        else {
            $this->selected_usermenu_item= 'Add Blog';
        }

        $fp = new FormProcessor_BlogPost($this->db, $this->user_id, $post_id);
        $validate = $request->isXmlHttpRequest();

        if ($request->isPost()) {
            if ($validate) {
                $fp->validateOnly(true);
                $fp->process($request);
            }
            else if ($fp->process($request)) {
                $this->view->url = $this->getUrl('view'). '/url/'. $fp->post->url ;
                $this->_redirect($this->getUrl('view'). '/url/'.$fp->post->url) ;
            }
        }

        if ($validate) {
            $json = array('errors' => $fp->getErrors());
            $this->sendJson($json);
        }
        else {
            $this->view->fp = $fp;
        }
        //call common functions, independent of action
        $this->addUserMenu();

        $this->fillViewVariables();
    }

    public function saveAction()
    {
        $this->preprocessSaveParams(DatabaseObject_BlogPost::getObjType());

        $request= $this->getRequest();

        if ($this->objid> 0) {
            if ($this->tagschanged != '') {
                $this->tags= DatabaseObject_BlogPost::updateTags($this->db, $this->objid, $this->tags);
            }
        }

        if ($request->isXmlHttpRequest()) {
            $this->_helper->viewRenderer->setNoRender();
            if ($this->tagschanged != ''){
                echo $this->tags;
            }
        }
        else {
            //redirect
        }
    }

    public function commentAction()
    {
        $request = $this->getRequest();
        $this->param_url= trim($request->getUserParam('url'));

        $blogpost= new DatabaseObject_BlogPost($this->db);
        if ($blogpost->load($this->param_url, 'url')){
            $this->objid= $blogpost->getId();
        }
        
        parent::commentAction();
    }

    protected function addUserMenu()
    {
       $usermenu_index= count($this->usermenu);
       $this->usermenu[$usermenu_index]->controller= $this->_type;
       $this->usermenu[$usermenu_index]->name= 'Draft '. ucwords($this->_pluraltype);
       $this->usermenu[$usermenu_index]->action= 'view';
       $this->usermenu[$usermenu_index]->title= 'View your draft ' . ucwords($this->_pluraltype);
       $this->usermenu[$usermenu_index]->url= $this->getUrl('view', $this->_type) .'/drafts' ;

        parent::addUserMenu();
    }
}

?>
Return current item: Web2.0 community/group web site