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

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

        $this->galleries= array();

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

        if (strlen($this->param_tag) > 0) {
            $this->galleries= DatabaseObject_Gallery::GetGalleries($this->db,
                                                  $options);
            $this->total_objcount= DatabaseObject_Gallery::GetGalleries($this->db,
                                                 $this->genCountOptions($options));
        }
        else if (strlen($this->param_url) > 0) {
            $gallery= new DatabaseObject_Gallery($this->db);
            if ($gallery->load($this->param_url, 'url')){
                $this->galleries= array($gallery);
                $this->total_objcount= 1;
            }
        }
        else if ($this->param_unread != '') {
            $this->galleries= DatabaseObject_Gallery::getNewForUser($this->db,
                                                       $this->user_id, $options);
            $this->total_objcount= DatabaseObject_Gallery::getNewForUser($this->db,
                                                        $this->user_id,
                                                        $this->genCountOptions($options));
        }
        else if ($this->param_starred != '') {
            $this->galleries= DatabaseObject_Gallery::getStarredByUser($this->db,
                                                       $this->user_id, $options);
            $this->total_objcount= DatabaseObject_Gallery::getStarredByUser($this->db,
                                                        $this->user_id,
                                                        $this->genCountOptions($options));
        }
        else {
            if (($this->param_month == 0) &&($this->param_year == 0)) {
                $options= DatabaseObject_Gallery::getLatestParams($this->db, $options);
            $ts= strtotime($options['to']);
            $month= date('n', $ts);
            $year= date('Y', $ts);
            $this->currentpage = $this->currentpage . $year . '/' . $month ;
            }

            $this->galleries= DatabaseObject_Gallery::GetGalleries($this->db,$options); 
            $this->total_objcount= DatabaseObject_Gallery::GetGalleries($this->db,
                                                        $this->genCountOptions($options));

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

        $this->objcount= 0;
        $this->objects= array();
        foreach ($this->galleries as $gallery_id => $gallery) {
            $this->objects[$this->objcount]= $this->fillObjectInfo($gallery);
            $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_galleries);

        $this->fillViewVariables();
    }

    protected function fillObjectInfo(DatabaseObject_Gallery $gallery)
    {
        $options= array('order' => 'p.ts_created desc'); 

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

        $gallery_object->type= $this->_type;
        $gallery_object->author= DatabaseObject_User::
                                          getUserNamefromId($this->db,
                                                     $gallery->user_id);
        
        if ($gallery->user_id == $this->user_id) {
            $gallery_object->owner= true;
        }
        //copy all other members from gallery
        $gallery_object->ts_created= $gallery->ts_created;
        $gallery_object->ts_last_modified= $gallery->ts_last_modified;
        $gallery_object->name= $gallery->galleryname; 
        $gallery_object->desc= $gallery->gallerydesc;
        $gallery_object->content = $gallery->embedcode;
        $gallery_object->url = $gallery->url;
        $gallery_object->tags = $gallery->getTags();
        $gallery_object->objid = $gallery->getId();

        $options['object_id']= $gallery->getId() ;
        $options['order']= 'p.ts_created'; 
        $this->comments = DatabaseObject_Comment::GetComments($this->db,'galleries_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);
            }
            $gallery_object->comments= $this->comments;
        }
        $gallery_object->unread= DatabaseObject_Unread::IsExists($this->db,
                                        $gallery->getId(), $this->user_id,
                                    DatabaseObject_Gallery::getObjType());
        $gallery_object->starred= DatabaseObject_Starred::IsExists($this->db,
                                        $gallery->getId(), $this->user_id,
                                    DatabaseObject_Gallery::getObjType());

        return $gallery_object;
    }

    public function editAction()
    {
        $this->preprocessEditParams();

        $request = $this->getRequest();

        $gallery_id= (int) $request->getParam('id');
        if (strlen($this->param_delete) >0) {
            $gallery= new DatabaseObject_Gallery($this->db);
            if($gallery->load($gallery_id)){
                if ($gallery->user_id == $this->user_id) {
                    $flag= $gallery->delete();
                    $this->view->actionsuccess= "Deleting Gallery is successful";
                    $this->addUserMenu();
                    $this->fillViewVariables();
                    return;
                }
            }
        }
        else if (strlen($this->param_url) >0) {
            $gallery= new DatabaseObject_Gallery($this->db);
            if($gallery->load($this->param_url, 'url')) {
                if ($gallery->user_id == $this->user_id) {
                    $gallery_id= $gallery->getId();
                    $this->selected_usermenu_item= 'Edit Gallery';
                }
            }
        }
        else {
            $this->selected_usermenu_item= 'Add Gallery';
        }

        $fp = new FormProcessor_NewGallery($this->db, $this->user_id, $gallery_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->gallery->url ;
                $this->_redirect($this->getUrl('view'). '/url/'.$fp->gallery->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_Gallery::getObjType());
        $request= $this->getRequest();

        if ($this->objid> 0) {
            if ($this->tagschanged != '') {
                $this->tags= DatabaseObject_Gallery::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 = $request->getUserParam('url');

        $gallery= new DatabaseObject_Gallery($this->db);
        if ($gallery->load($this->param_url, 'url')) {
            $this->objid= $gallery->getId();
        }

        parent::commentAction();
    }
}

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