<?php
class PollController extends BaseObjectControllerAction
{
const CONTROLLER= 'poll';
const DATA_TABLE='polls';
public function init()
{
parent::init(self::CONTROLLER, self::DATA_TABLE);
}
public function viewAction()
{
$options= $this->preprocessViewParams();
$this->polls= array();
$options['limit']= $this->user_settings->pp_polls;
if (strlen($this->param_tag) > 0) {
$this->polls= DatabaseObject_Poll::GetPolls($this->db, $options);
$this->total_objcount= DatabaseObject_Poll::GetPolls($this->db, $this->genCountOptions($options));
}
else if (strlen($this->param_url) > 0) {
$poll= new DatabaseObject_Poll($this->db);
if ($poll->load($this->param_url, 'url')){
$this->polls= array($poll);
$this->total_objcount= 1;
}
}
else if ($this->param_unread != '') {
$this->polls= DatabaseObject_Poll::getNewForUser($this->db, $this->user_id, $options);
$this->total_objcount= DatabaseObject_Poll::
getNewForUser($this->db,
$this->user_id,
$this->genCountOptions($options));
}
else if($this->param_starred != '') {
$this->polls= DatabaseObject_Poll::
getStarredByUser($this->db, $this->user_id, $options);
$this->total_objcount= DatabaseObject_Poll::
getStarredByUser($this->db,
$this->user_id,
$this->genCountOptions($options));
$options['liveonly']= false;
}
else if($this->param_drafts != '') {
$present_year = date('Y'); // current year
$present_month = date('n');// current month
$present_day= date('j'); //current day
$present_time= mktime(0, 0, 0, $present_month, $present_day+1, $present_year);
$options['from']= date('Y-m-d H:i:s', $present_time);
$options['liveonly']= false;
$this->polls= DatabaseObject_Poll::GetPolls($this->db,$options);
$this->total_objcount= DatabaseObject_Poll::
GetPolls($this->db,
$this->genCountOptions($options));
$this->pagetitle= 'Not yet started ' . ucwords($this->_pluraltype);
$this->view->content_title= 'Not yet started ' . ucwords($this->_pluraltype);
$this->selected_usermenu_item= 'Notyet Started '. ucwords($this->_pluraltype);
}
else {
if (($this->param_month == 0) &&($this->param_year == 0)) {
$options= DatabaseObject_Poll::getLatestParams($this->db, $options);
if (array_key_exists('to', $options)) {
$ts= strtotime($options['to']);
$month= date('n', $ts);
$year= date('Y', $ts);
$this->currentpage = $this->currentpage . $year . '/' . $month ;
}
}
$this->polls= DatabaseObject_Poll::GetPolls($this->db,$options);
$this->total_objcount= DatabaseObject_Poll::
GetPolls($this->db,
$this->genCountOptions($options));
if (isset($this->submenu1)&& !isset($this->selected_submenu1_item) &&(count($this->polls)>0)) {
$this->selected_submenu1_item= $this->submenu1[0]->name;
}
}
$this->objcount= 0;
$this->objects= array();
foreach ($this->polls as $poll_id => $poll) {
$this->objects[$this->objcount]= $this->fillObjectInfo($poll);
$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_polls);
$this->fillViewVariables();
$this->view->nocommenteditor= true;
$this->view->nomarkasread= true;
}
protected function fillObjectInfo(DatabaseObject_Poll $poll)
{
//todo: Ideally this should be created from a factory class
$poll_object= new Utility_Object();
$poll_object->type= $this->_type;
$poll_object->author= DatabaseObject_User::
getUserNamefromId($this->db,
$poll->user_id);
if ($poll->user_id == $this->user_id) {
$poll_object->owner= true;
}
//copy all other members from poll
$poll_object->ts_created= $poll->ts_created;
$poll_object->ts_last_modified= $poll->ts_last_modified;
$poll_object->name = $poll->polldesc; //there is no name for the poll
$poll_object->content['options']= $poll->poll_options;
$poll_object->content['results']= $poll->poll_results;
$poll_object->content['startdate']= $poll->ts_startdate;
$poll_object->content['enddate']= $poll->ts_enddate;
$poll_object->content['votedfor']= $poll->getVotedOption($this->user_id);
$poll_object->content['status']= $poll->getStatus();
$poll_object->content['totalvotes']= 0;
foreach ($poll->poll_results as $key=>$value)
$poll_object->content['totalvotes'] += $value;
// $poll_object->desc= 'description of the poll'; // there is no desc for poll, as it is used in place of name
$poll_object->url = $poll->url;
$poll_object->tags = $poll->getTags();
$poll_object->objid = $poll->getId();
$this->comments = $poll->getComments();
$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);
}
$poll_object->comments= $this->comments;
}
$poll_object->unread= $poll->isNewForUser($this->user_id);
$poll_object->starred= $poll->isStarredByUser($this->user_id);
return $poll_object;
}
public function editAction()
{
$this->view->extra_scripts= array('/js/poll.class.js');
$this->preprocessEditParams();
$request = $this->getRequest();
$poll_id= (int) $request->getParam('id');
if (strlen($this->param_delete) >0) {
$poll= new DatabaseObject_Poll($this->db);
if($poll->load($poll_id)) {
if ($poll->user_id == $this->user_id) {
$flag= $poll->delete();
$this->view->actionsuccess= "Deleting poll is successful";
$this->addUserMenu();
$this->fillViewVariables();
return;
}
}
}
else if (strlen($this->param_url) >0) {
$poll= new DatabaseObject_Poll($this->db);
if($poll->load($this->param_url, 'url')) {
if ($poll->user_id == $this->user_id) {
$poll_id= $poll->getId();
$this->selected_usermenu_item= 'Edit Poll';
}
}
}
else {
$this->selected_usermenu_item= 'Add Poll';
}
$fp = new FormProcessor_NewPoll($this->db, $this->user_id, $poll_id);
$validate = $request->isXmlHttpRequest();
if ($request->isPost()) {
if ($validate) {
$fp->validateOnly(true);
$fp->process($request);
}
else if ($fp->process($request)) {
if ($fp->dovalidate) {
$this->view->url = $this->getUrl('view'). '/url/'. $fp->poll->url ;
$this->_redirect($this->getUrl('view'). '/url/'.$fp->poll->url) ;
}
}
}
if ($validate) {
$json = array('errors' => $fp->getErrors());
if (!$fp->dovalidate) {
$json['action']= 'updateoptions';
$templater= new Templater();
$templater->fp= $fp;
$json['html']= $templater->render('poll/poll-options.tpl');
}
else {
$json['action']= 'submit';
}
$this->sendJson($json);
}
else {
$this->view->fp = $fp;
}
//call common functions, independent of action
$this->addUserMenu();
$this->fillViewVariables();
}
public function voteAction()
{
$request= $this->getRequest();
$option_id= trim($request->getPost('polloption'));
$poll_url= trim($request->getPost('poll-url'));
$widget_mode= $request->getPost('widgetmode');
$votingstatus= false;
$poll = new DatabaseObject_Poll($this->db);
if ((strlen($option_id)> 0) &&(strlen($poll_url)>0)) {
if ($poll->load($poll_url, 'url')) {
$votingstatus= $poll->voteFor($this->user_id, $option_id);
}
}
if (strtolower($widget_mode) == 'yes') {
if ($votingstatus) {
$session = new Zend_Session_Namespace('prevpollid');
$session->pollid= $poll->getId();
}
else {
//todo: display resaon for voting failure
}
$this->_redirect($this->getUrl('widget'));
}
else {
$rd= $this->_helper->getHelper('Redirector');
if ($request->getParam('mode') == 'iframe') {
$rd->setGoto("unread","view", "poll", array('mode'=>'iframe'));
$rd->redirectAndExit();
}
else {
$this->_redirect($this->getUrl('view'));
}
}
}
public function widgetAction()
{
$request= $this->getRequest();
$ajaxreq= $request->isXmlHttpRequest();
$this->view->nextpollexists= '';
$options= array();
$options['limit'] = 1;
$this->polls= DatabaseObject_Poll::getNewForUser($this->db, $this->user_id, $options);
$session = new Zend_Session_Namespace('prevpollid');
if ($session->pollid) {
$poll = new DatabaseObject_Poll($this->db);
if ($poll->load($session->pollid)) {
if (count($this->polls)>0) {
$this->view->nextpollexists= 'yes';
}
$this->polls= array($poll);
}
else {
//continue with the polls list got earlier
}
unset($session->pollid);
}
if (count($this->polls) >0) {
$poll_ids= array_keys($this->polls);
$this->view->pollobject=$this->fillObjectInfo($this->polls[$poll_ids[0]]);
}
else {
$options= DatabaseObject_Poll::getLatestParams($this->db, $options);
$this->polls= DatabaseObject_Poll::GetPolls($this->db, $options);
if (count($this->polls) >0) {
$poll_ids= array_keys($this->polls);
$this->view->pollobject=$this->fillObjectInfo($this->polls[$poll_ids[0]]);
}
}
$this->view->widget_mode= 'yes';
$this->view->extra_scripts= array('/js/vote.js');
$this->view->extra_styles= array(
'/css/widgetstyle.css',
'/css/formstyle.css'
);
}
public function saveAction()
{
$this->preprocessSaveParams(DatabaseObject_Poll::getObjType());
$request= $this->getRequest();
if ($this->objid> 0) {
if ($this->tagschanged != '') {
$this->tags= DatabaseObject_Poll::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'));
$poll= new DatabaseObject_Poll($this->db);
if ($poll->load($this->param_url, 'url')) {
$this->objid= $poll->getId();
}
parent::commentAction();
}
protected function addUserMenu()
{
$usermenu_index= count($this->usermenu);
$this->usermenu[$usermenu_index]->controller= $this->_type;
$this->usermenu[$usermenu_index]->name= 'Notyet Started '. ucwords($this->_pluraltype);
$this->usermenu[$usermenu_index]->action= 'view';
$this->usermenu[$usermenu_index]->title= 'View not yet started ' . ucwords($this->_pluraltype);
$this->usermenu[$usermenu_index]->url= $this->getUrl('view', $this->_type) .'/drafts' ;
parent::addUserMenu();
}
}
?>