Location: PHPKode > projects > Web2.0 community/group web site > web20groups_pre-alpha-2/include/BaseObjectControllerAction.php
<?php

//This class implements the generic functionality for Video,gallery,blog,poll
class BaseObjectControllerAction extends CommonCustomControllerAction 
{
	public function init($objtype, $plural)
	{
		parent::init();
        $this->_type= $objtype;
        $this->_pluraltype= $plural;
        $this->_dbclass= "DatabaseObject_" . ucwords($this->_type);

    }

    protected function indexAction()
    {
        $this->_redirect($this->getUrl('view'));
    }

    protected function preprocessEditParams()
    {
        $request = $this->getRequest();
        $this->param_my= 'yes';

        $this->createSubmenu();
        $this->param_url= trim($request->getUserParam('url'));
        $this->param_delete= $request->getPost('delete');
        $this->param_save= $request->getPost('save');

        $index= count($this->view->extra_styles);
        if ($index == 0)
            $prevstyles= array();
        else
            $prevstyles= $this->view->extra_styles;
        $new_styles= array(   
                                            '/css/main.css',
                                            '/css/windowstyle.css',
                                            '/css/formstyle.css'
                                        );
        for ($i= 0; $i < count($new_styles); $i++)
            $prevstyles[$index+$i]= $new_styles[$i];
        $this->view->extra_styles= $prevstyles;

        $index= count($this->view->extra_scripts);
        if ($index == 0)
            $prevscripts= array();
        else
            $prevscripts= $this->view->extra_scripts;
        $new_scripts= array( 
                                            '/js/main.js' ,
                                            '/js/mylib.inlineframe.class.js',
                                            '/js/mylib.menu.class.js'
                                         );
        for ($i= 0; $i < count($new_scripts); $i++)
            $prevscripts[$index+$i]= $new_scripts[$i];
        $this->view->extra_scripts= $prevscripts;

    }

    protected function preprocessSaveParams($objtype)
    {
        $request= $this->getRequest();

        if ($request->getPost['edit'] == 'edit') {
            $this->objid= $request->getParam('id');
            $this->_redirect($this->getUrl('edit'). '/?id='. $this->objid);
        }

        $fp = new FormProcessor_ObjMenu($this->db, $this->user_id);

        $validate = $request->isXmlHttpRequest();
        if ($request->isPost()) {
            if ($validate) {
                $fp->validateOnly(true);
                $fp->process($request, $objtype);
            }
            else if ($fp->process($request, $objtype)) {
            }
            $this->tags= $fp->tags;
            $this->objid= $fp->objid;
            $this->tagschanged= $fp->tagschanged;
        }

        if ($validate) {
            //$json = array('errors' => $fp->getErrors());
            //$this->sendJson($json);
        }
        else {
            $this->view->fp = $fp;
        }
    }

    protected function preprocessViewParams()
    {
        $this->currentpage= $this->getUrl('view');
        $this->loadViewParams();

        $options= array();

        if ($this->param_my == 'yes') {
            $options['user_id']= array($this->user_id);
            $this->currentpage .= '/my';
        }
        if (strlen($this->param_tag) > 0) {
            $options['tags']= array($this->param_tag);
            $this->currentpage .= '/tag/'.$this->param_tag ;
            $this->view->content_title= "Displaying " . $this->_pluraltype ." with tag '" . $this->param_tag . "'";

            $this->view->extra_styles= array(
                                                '/css/main.css',
                                                '/css/formstyle.css',
                                                '/css/windowstyle.css',
                                                '/css/objectstyle.css'
                                            );
            $this->view->extra_scripts= array(
                                                '/js/main.js',
                                                '/js/mylib.inlineframe.class.js',
                                                '/js/mylib.menu.class.js',
                                                '/js/objectmenu.js'
                                             );
        }
        else if (strlen($this->param_url) > 0) {
            $this->pagetitle=  ucwords($this->_type) ." : " .$this->param_url;
            $this->view->content_title= ucwords($this->_type) ." : ". $this->param_url;
            $this->currentpage .= '/url/'.$this->param_url;
            $this->view->extra_styles= array('/css/main.css','/css/formstyle.css', '/css/objectstyle.css', '/css/windowstyle.css');
            $this->view->extra_scripts= array('/js/objectmenu.js', '/js/main.js', '/js/mylib.menu.class.js', '/js/mylib.inlineframe.class.js');
        }
        else if (strlen($this->param_unread)> 0) {
            $this->pagetitle= 'New ' . ucwords($this->_pluraltype);
            $this->view->content_title= 'New ' . ucwords($this->_pluraltype);
            $this->currentpage .= '/unread';

            $this->view->ajaxrequest= 'yes';
            $this->view->extra_styles= array('/css/main.css', '/css/formstyle.css', '/css/objectstyle.css');
            $this->view->extra_scripts= array( '/js/main.js', '/js/objectmenu.js', '/js/mylib.menu.class.js');
        }
        else if (strlen($this->param_starred)>0) {
            $this->pagetitle= 'Starred ' . ucwords($this->_pluraltype);
            $this->view->content_title= 'Starred '. ucwords($this->_pluraltype);
            $this->currentpage .= '/starred';
            
            $this->view->ajaxrequest= 'yes';
            $this->view->extra_styles= array('/css/main.css','/css/formstyle.css', '/css/objectstyle.css');
            $this->view->extra_scripts= array('/js/objectmenu.js', '/js/main.js', '/js/mylib.menu.class.js');
        }
        else if (strlen($this->param_drafts) >0) {
            $options['user_id']= array($this->user_id);

            $this->currentpage .= '/drafts';
            $this->view->extra_styles= array(
                                                '/css/main.css',
                                                '/css/formstyle.css',
                                                '/css/windowstyle.css',
                                                '/css/objectstyle.css'
                                            );
            $this->view->extra_scripts= array(
                                                '/js/main.js',
                                                '/js/mylib.inlineframe.class.js',
                                                '/js/mylib.menu.class.js',
                                                '/js/objectmenu.js'
                                             );
        }
        else {
            $this->currentpage .= '/archive/';
            if (($this->param_year > 2000) && ($this->param_year <= 2008)) {   //should be current year fixme
                $this->param_month = max(1, min(12, $this->param_month));

                // generate start and finish timestamp for the given month/year
                $from = mktime(0, 0, 0, $this->param_month, 1, $this->param_year);
                $to   = mktime(0, 0, 0, $this->param_month + 1, 1, $this->param_year) - 1;

                // get live posts based on timestamp with newest posts listed first
                $options['from'] = date('Y-m-d H:i:s', $from);
                $options['to'] = date('Y-m-d H:i:s', $to);
                $this->view->content_title= "Displaying ".$this->_pluraltype ." of ". date('M \'y',$from);
                $this->currentpage .= $this->param_year. '/'.$this->param_month;
            }
            else {
                $this->param_year = 0;
                $this->param_month = 0;

                //$this->currentpage .= '/archive/'.$this->param_year. '/'.$this->param_month;

                if ($this->param_my == 'yes') {
                    $this->selected_usermenu_item= 'My ' . ucwords($this->_pluraltype);
                    $this->_my= 'Ur';
                }
                else {
                    $this->selected_commonmenu_item= ucwords($this->_pluraltype);
                    $this->_my= '';
                }
                $this->view->content_title= "Displaying " .$this->_my." latest ". $this->_pluraltype ;
            }
            $this->view->extra_styles= array(
                                                '/css/main.css',
                                                '/css/formstyle.css',
                                                '/css/windowstyle.css',
                                                '/css/objectstyle.css'
                                            );
            $this->view->extra_scripts= array(
                                                '/js/main.js',
                                                '/js/mylib.inlineframe.class.js',
                                                '/js/mylib.menu.class.js',
                                                '/js/objectmenu.js'
                                             );
        }

        if ($this->param_offset != 0){
            $options['offset']= $this->param_offset;
        }
        else {
            $options['offset']= 0;
        }
            
        if ($this->param_my == 'yes') {
            $this->createSubmenu();
        }
        else { //all
            $this->createSubmenu(0,0,false);
        }

        return $options;
    }

    protected function addEditLink()
    {
    }

	protected function loadViewParams()
	{
		$request= $this->getRequest();

        if ($request->getParam('mode') == 'iframe') {
            $this->view->ajaxrequest = true;
        }

		$this->param_my= $request->getParam('my');

        $this->param_url= $request->getUserParam('url');

		$this->param_month= (int) (trim($request->getUserParam('month')));
		$this->param_year= (int) (trim($request->getUserParam('year')));
		$this->param_offset= (int) (trim($request->getUserParam('offset')));

		$this->param_tag= $request->getParam('tag');

        $this->param_unread= $request->getParam('unread');

        $this->param_starred= $request->getParam('starred');

        $this->param_drafts= $request->getParam('drafts');

	}

    protected function addUserMenu()
    {
        $usermenu_index= count($this->usermenu);
        $this->usermenu[$usermenu_index]->controller= $this->_type;
        $this->usermenu[$usermenu_index]->name= 'Add '. ucwords($this->_type);
        $this->usermenu[$usermenu_index]->action= 'edit';
        $this->usermenu[$usermenu_index]->title= 'Add a new ' . ucwords($this->_type);
        $this->usermenu[$usermenu_index]->url= $this->getUrl('edit') ;
        $usermenu_index+= 1;

        $this->usermenu[$usermenu_index]->controller= $this->_type;
        $this->usermenu[$usermenu_index]->name= 'My ' . ucwords($this->_pluraltype);
        $this->usermenu[$usermenu_index]->action= 'view';
        $this->usermenu[$usermenu_index]->title= 'Manage Ur '. ucwords($this->_pluraltype);
        $this->usermenu[$usermenu_index]->url= $this->getUrl('view') . '/my' ;

        parent::addUserMenu();
    }

    protected function createSubmenu($year= 0, $tagoffset=0, $my=true)
	{
        $options= array();

		if ($my) {
		    $options['user_id']= $this->user_id;
			$mystr= '/my';
	    }
		else{
			$mystr= '';
	    }
		
        //get the year specific objects
        //fixme -- dirty hack for now
        if ($this->_type == 'video') 
    		$monthlysummary= DatabaseObject_Video::GetMonthlySummary($this->db, $options);
        else if ($this->_type == 'blog')
            $monthlysummary= DatabaseObject_BlogPost::GetMonthlySummary($this->db, $options);
        else if ($this->_type == 'poll')
            $monthlysummary= DatabaseObject_Poll::GetMonthlySummary($this->db, $options);
        else if ($this->_type == 'gallery')
            $monthlysummary= DatabaseObject_Gallery::GetMonthlySummary($this->db, $options);

		$i= 0;
		foreach($monthlysummary as $month=>$count) {
			$yearmonth = explode("-",$month);

			$this->submenu1[$i]->name= date('M-y',mktime(0,0,0,$yearmonth[1],1,$yearmonth[0])) . '(' . $count . ')'; 
			if ( $count > 0) {
				$this->submenu1[$i]->title="view archive for". $this->submenu1[$i]->name;
				$this->submenu1[$i]->url= $this->getUrl('view') . $mystr . '/archive/' .$yearmonth[0] . '/'. $yearmonth[1]; ;
			}
			else {
				$this->submenu1[$i]->title= '';
				$this->submenu1[$i]->url= '';
			}
            if(isset($this->param_year)){
                if (($this->param_year == $yearmonth[0]) && ($this->param_month == $yearmonth[1])){
                    $this->selected_submenu1_item= $this->submenu1[$i]->name;
                }
            }
			$i++;
		}
			
		//get the unique tags offsetting from $tagoffset
        //fixme -- dirty hack for now
        if ($this->_type == 'video')
    		$alltags= DatabaseObject_Video::getAllTags($this->db);
        else if ($this->_type == 'blog')
            $alltags= DatabaseObject_BlogPost::getAllTags($this->db);
        else if ($this->_type == 'poll')
            $alltags= DatabaseObject_Poll::getAllTags($this->db);
        else if ($this->_type == 'gallery')
            $alltags= DatabaseObject_Gallery::getAllTags($this->db);

		$i= 0;
		foreach ($alltags as $key=>$value) {
            $this->tagspace[$i]->name= $value ;
            $this->tagspace[$i]->title= "view " . $this->_pluraltype . " with tag" . $value;
            $this->tagspace[$i]->url= $this->getUrl('view'). "/tag/". $value;

            if (isset($this->param_tag) && ($this->param_tag == $value)) {
                $this->selected_tag= $this->tagspace[$i]->name;
            }

            $i++;
        }

        $this->submenu1_title= ucwords($this->_type) ." Archive";
        $this->tagspace_title= ucwords($this->_type) ." Tag Space";
    }

    protected function commentAction()
    {
	    $request = $this->getRequest();

		if ($this->param_url == '') {
			$this->_redirect($this->getUrl('view'));
		}

		$fp = new FormProcessor_NewComment($this->db, $this->_pluraltype .'_comments');
		$validate = $request->isXmlHttpRequest();

		if ($request->isPost()) {
			/*if ($validate) {
				$fp->validateOnly(true);
				$fp->process($request);
			}
			else */if ($fp->process($request, $this->objid)) {
			}
		}

		if ($validate) {
            if ($fp->hasError()){
    			$json = array('errors' => $fp->getErrors());
			    $this->sendJson($json);
            }
            else {
                $this->_helper->viewRenderer->setNoRender();
                $templater = new Templater();
                $fp->comment->username= DatabaseObject_User::
                                        getUserNamefromId($this->db,
                                            $fp->comment->user_id);
                $templater->comment= $fp->comment;
                $templater->index= DatabaseObject_Comment::
                                    getCommentCount($this->db, 
                                            $this->_pluraltype .'_comments',
                                    array('object_id'=>array($this->objid)))-1;
                $body = $templater->render('lib/comment-display.tpl');
                echo $body;
            }
		}
		else {
			$this->view->fp= $fp;
			if ($fp->redirect2 == '') {
				$this->_redirect($this->getUrl('view'). '/'. 'url/'.$this->param_url) ;
			}
			else {
				$this->_redirect($fp->redirect2);
			}

    		//call common functions, independent of action
	    	$this->addUserMenu();
            $this->fillViewVariables();
		}
    }

    protected function genCountOptions($options)
    {
        $options['count']= 'yes';
        $options['limit']= 0;
        $options['offset']= 0;

        return $options;
    }

    protected function generatePageInfo($pp_objcount)
    {
        $this->view->pages= array();

        if ($this->total_objcount - $pp_objcount <1)
            return;

        $active_pageno= ($this->param_offset/$pp_objcount)+1;
        $total_objcount= $this->total_objcount;

        for ($pageno= 1; $total_objcount > 0; $pageno++) {
            $pages[$pageno]['title']= 'goto ' . $pageno . ' page';
            $pages[$pageno]['link']= $this->currentpage . '/' . ($pageno-1) * $pp_objcount;
            $total_objcount -= $pp_objcount;
            $lastpage= $pageno;
        }
        
        if ($pageno >= 2) {
            if ($active_pageno != $lastpage) {
                $nextpage['title']= 'goto next('.($active_pageno+1) .') page';
                $nextpage['link']= $this->currentpage . '/' . ($active_pageno* $pp_objcount);
                $this->view->nextpage= $nextpage;
                $endobj_num=$active_pageno * $pp_objcount;
            }
            else {
                $endobj_num= $this->total_objcount;
            }

            if ($active_pageno != 1) {
                $prevpage['title']= 'goto previous ('.($active_pageno-1) .') page';
                $prevpage['link']= $this->currentpage . '/'. (($active_pageno -2) *$pp_objcount);
                $this->view->prevpage= $prevpage;
            }
        }
        $startobj_num= ($active_pageno -1)* $pp_objcount +1;

        //todo: move this to some utility function
        if ($endobj_num == $startobj_num) {
            if ($startobj_num % 10 == 1) {
                if ($startobj_num % 100 != 11)
                    $midstr= $startobj_num .'st';
                else
                    $midstr= $startobj_num .'th';
            }
            else if ($startobj_num % 10 == 2) {
                if ($startobj_num % 100 != 12)
                    $midstr= $startobj_num .'nd';
                else
                    $midstr= $startobj_num .'th';
            }
            else if ($startobj_num % 10 == 3){
                if ($startobj_num % 100 != 13)
                    $midstr= $startobj_num .'rd';
                else 
                    $midstr= $startobj_num .'th';
            }
            else
                $midstr= $startobj_num .'th';
        }
        else {
            $midstr= $startobj_num ."-" .$endobj_num;
        }

        $this->view->pagenav_title= "Displaying ". $midstr ." of " .$this->total_objcount . " ". $this->_pluraltype;
        $this->view->pages= $pages;
        $this->view->activepage= $active_pageno;
    }
}

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