diff --git a/oss/joomla/trunk/components/com_apoll/apoll.php b/oss/joomla/trunk/components/com_apoll/apoll.php new file mode 100644 index 00000000..68778aaf --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/apoll.php @@ -0,0 +1,49 @@ +registerTask( 'results', 'display' ); + +// Perform the Request task +$controller->execute(JRequest::getCmd('task')); + +// Redirect if set by the controller +$controller->redirect(); + +?> \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/controller.php b/oss/joomla/trunk/components/com_apoll/controller.php new file mode 100644 index 00000000..cdc1d56b --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/controller.php @@ -0,0 +1,100 @@ + + * @package Joomla + * @subpackage Poll + * @since 1.5 + */ +class ApollController extends JController +{ + function display() + { + + //Set the default view, just in case + $view = JRequest::getCmd('view'); + if(empty($view)) { + JRequest::setVar('view', 'apolls'); + }; + parent::display(); + } + + /** + * Add a vote to an option + */ + function vote() + { + global $mainframe; + + // Check for request forgeries + JRequest::checkToken() or jexit( 'Invalid Token' ); + + $db =& JFactory::getDBO(); + $apoll_id = JRequest::getVar( 'id', 0, '', 'int' ); + $option_id = JRequest::getVar( 'voteid', 0, 'post', 'int' ); + $apoll =& JTable::getInstance('apoll','Table'); + if (!$apoll->load( $apoll_id ) || $apoll->published != 1) { + JError::raiseWarning( 404, JText::_('ALERTNOTAUTH') ); + return; + } + + $cookieName = JUtility::getHash( $mainframe->getName() . 'apoll' . $apoll_id ); + $voted = JRequest::getVar( $cookieName, '0', 'COOKIE', 'INT'); + + if ($voted || !$option_id ) + { + if($voted) { + $msg = JText::_('You already voted for this poll!'); + $tom = "error"; + } + + if(!$option_id){ + $msg = JText::_('WARNSELECT'); + $tom = "error"; + } + } + else + { + require_once(JPATH_COMPONENT.DS.'models'.DS.'apoll.php'); + $model = new ApollModelApoll(); + if ( $model->vote( $apoll_id, $option_id )) { + //Set cookie showing that user has voted + setcookie( $cookieName, '1', time() + 60*$apoll->lag ); + } + + $msg = JText::_( 'Thanks for your vote!' ); + $tom = ""; + } + + // set Itemid id for links + $menu = &JSite::getMenu(); + $items = $menu->getItems('link', 'index.php?option=com_apoll&view=apoll'); + + $itemid = isset($items[0]) ? '&Itemid='.$items[0]->id : ''; + + $this->setRedirect( JRoute::_('index.php?option=com_apoll&view=apoll&id='. $apoll_id.':'.$apoll->alias.$itemid, false), $msg, $tom); + } +} +?> \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/index.html b/oss/joomla/trunk/components/com_apoll/index.html new file mode 100644 index 00000000..fa6d84e8 --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/index.html @@ -0,0 +1 @@ +
\ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/models/ajaxvote.php b/oss/joomla/trunk/components/com_apoll/models/ajaxvote.php new file mode 100644 index 00000000..50c719e5 --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/models/ajaxvote.php @@ -0,0 +1,113 @@ +load( $apoll_id ) || $apoll->published != 1) { + JError::raiseWarning( 404, JText::_('ALERTNOTAUTH') ); + return; + } + + $cookieName = JUtility::getHash( $mainframe->getName() . 'apoll' . $apoll_id ); + $voted = JRequest::getVar( $cookieName, '0', 'COOKIE', 'INT'); + + if ($voted || !$option_id ) + { + if($voted) { + $msg = JText::_('You already voted for this poll!'); + $tom = "error"; + } + if(!$option_id){ + $msg = JText::_('WARNSELECT'); + $tom = "error"; + } + $this->_voted = 0; + } + else + { + require_once(JPATH_COMPONENT.DS.'models'.DS.'apoll.php'); + $model = new ApollModelApoll(); + if($model->vote( $apoll_id, $option_id )) { + $this->_voted = 1; + //Set cookie showing that user has voted + setcookie( $cookieName, '1', time() + 60*$apoll->lag ); + } else { + $this->_voted = 0; + } + } + return $this->_voted = 1; + } + + function getData() + { + // Lets load the content if it doesn't already exist + if (empty($this->_data)) + { + $query = $this->_buildQuery(); + $this->_data = $this->_getList($query); + } + return $this->_data; + } + + function getTotal() + { + if (empty($this->_total)) + { + $db =& JFactory::getDBO(); + $query = $this->_buildQuery(); + $this->_total = $this->_getListCount($query); + } + return $this->_total; + } + + function _buildQuery() + { + $db =& JFactory::getDBO(); + $apoll_id = JRequest::getVar( 'id', 0, 'POST', 'int' ); + $query = "SELECT o.id, o.text, o.color, COUNT(v.id) AS votes" + ." FROM ".$db->nameQuote('#__apoll_options')." AS o " + ." LEFT JOIN ".$db->nameQuote('#__apoll_votes')." AS v " + ." ON o.id = v.option_id " + ." WHERE o.apoll_id=".(int)$apoll_id + ." GROUP BY o.id " + ; + return $query; + } +} +?> diff --git a/oss/joomla/trunk/components/com_apoll/models/apoll.php b/oss/joomla/trunk/components/com_apoll/models/apoll.php new file mode 100644 index 00000000..556673ef --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/models/apoll.php @@ -0,0 +1,101 @@ +getDBO(); + $apoll_id = (int) $apoll_id; + $option_id = (int) $option_id; + + $date = &JFactory::getDate(); + $date->setOffset($mainframe->getCfg('offset')); + $ip = $_SERVER['REMOTE_ADDR']; + $user =& JFactory::getUser(); + + $query = "INSERT INTO ".$db->nameQuote('#__apoll_votes')." + SET date = ".$db->Quote($date->toFormat()).", + option_id = ".(int) $option_id.", + apoll_id = ".(int) $apoll_id.", + ip = INET_ATON('".$ip."')".", + user_id =".$user->id; + + //Save the vote to the database + $db->setQuery( $query ); + if(!$db->query()) { + $msg = $db->stderr(); + $tom = "error"; + } + return true; + } + function getOptions() { + $db = $this->getDBO(); + $apoll_id = JRequest::getInt('id', 0); + + $query = "SELECT o.*, COUNT(v.id) AS hits, + (SELECT COUNT(id) FROM ".$db->nameQuote('#__apoll_votes')." WHERE apoll_id=".$apoll_id.") AS voters" + ." FROM ".$db->nameQuote('#__apoll_options')." AS o" + ." LEFT JOIN ".$db->nameQuote('#__apoll_votes')." AS v" + ." ON (o.id = v.option_id AND v.apoll_id = " . $apoll_id . ")" + ." WHERE o.apoll_id = ". $apoll_id + ." AND o.text <> ''" + ." GROUP BY o.id " + ." ORDER BY o.ordering " + ; + + $db->setQuery( $query ); + if($votes = $db->loadObjectList()) { + return $votes; + } else { + return $db->stderr(); + } + + } + + function getApolls() { + $db = $this->getDBO(); + + $query = "SELECT id, title, CASE WHEN CHAR_LENGTH(alias) THEN CONCAT_WS(':', id, alias) ELSE id END AS slug + FROM ".$db->nameQuote('#__apoll_polls')." + WHERE published = 1 + ORDER BY id"; + + $db->setQuery( $query ); + + if($pList = $db->loadObjectList()) { + return $pList; + } else { + return $db->stderr(); + } + } +} +?> \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/models/apolls.php b/oss/joomla/trunk/components/com_apoll/models/apolls.php new file mode 100644 index 00000000..c6eb84ae --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/models/apolls.php @@ -0,0 +1,177 @@ +setState('limit', $mainframe->getUserStateFromRequest('com_apoll.limit', 'limit', $config->getValue('config.list_limit'), 'int')); + $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int')); + + // In case limit has been changed, adjust limitstart accordingly + $this->setState('limitstart', ($this->getState('limit') != 0 ? (floor($this->getState('limitstart') / $this->getState('limit')) * $this->getState('limit')) : 0)); + + // Get the filter request variables + $this->setState('filter_order', JRequest::getCmd('filter_order', 'ordering')); + $this->setState('filter_order_dir', JRequest::getCmd('filter_order_Dir', 'ASC')); + + } + /** + * Method to get poll item data + * + * @access public + * @return array + */ + function getData() + { + // Lets load the content if it doesn't already exist + if (empty($this->_data)) + { + $query = $this->_buildQuery(); + $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); + } + + return $this->_data; + } + /** + * Method to get the total number of weblink items + * + * @access public + * @return integer + */ + function getTotal() + { + // Lets load the content if it doesn't already exist + if (empty($this->_total)) + { + $query = $this->_buildQuery(); + $this->_total = $this->_getListCount($query); + } + + return $this->_total; + } + + /** + * Method to get a pagination object for the weblinks + * + * @access public + * @return integer + */ + function getPagination() + { + // Lets load the content if it doesn't already exist + if (empty($this->_pagination)) + { + jimport('joomla.html.pagination'); + $this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') ); + } + + return $this->_pagination; + } + function _buildQuery() + { + $where = $this->_buildContentWhere(); + $orderby = $this->_buildContentOrderBy(); + $db =& JFactory::getDBO(); + $query = "SELECT m.*, + CASE WHEN CHAR_LENGTH(m.alias) THEN CONCAT_WS(':', m.id, m.alias) ELSE m.id END as slug, + CASE WHEN publish_down>NOW() THEN 'active' ELSE 'ended' END AS status," + . " (SELECT COUNT(v.id) FROM ".$db->nameQuote('#__apoll_votes')." AS v WHERE v.apoll_id = m.id) AS voters," . " COUNT(o.id) AS numoptions " + . " FROM ".$db->nameQuote('#__apoll_polls') ." AS m " + . " LEFT JOIN ".$db->nameQuote('#__apoll_options')." AS o " + . " ON o.apoll_id = m.id " + . " WHERE m.published=1 AND o.text <> ''" + . $where + . " GROUP BY m.id" + . $orderby + ; + + return $query; + } + function _buildContentOrderBy() + { + global $mainframe, $option; + + $filter_order = $mainframe->getUserStateFromRequest( "$option.filter_order.apolls", 'filter_order', 'm.id', 'cmd' ); + $filter_order_Dir = $mainframe->getUserStateFromRequest( "$option.filter_order_Dir.apolls", 'filter_order_Dir', '', 'word' ); + + $orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir; + + return $orderby; + } + + function _buildContentWhere() + { + global $mainframe, $option; + $db =& JFactory::getDBO(); + $filter_order = $mainframe->getUserStateFromRequest( "$option.filter_order.apolls", 'filter_order', 'm.id', 'cmd' ); + $filter_order_Dir = $mainframe->getUserStateFromRequest( "$option.filter_order_Dir.apolls", 'filter_order_Dir', '', 'word' ); + $search = $mainframe->getUserStateFromRequest( "$option.search.apolls",'search','','string'); + + $where = array(); + + if ($search) + { + $search = JString::strtolower( $search ); + $where[] = 'LOWER(m.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ); + } + + $where = ( count( $where ) ? ' AND '. implode( ' AND ', $where ) : '' ); + + return $where; + } +} +?> diff --git a/oss/joomla/trunk/components/com_apoll/models/index.html b/oss/joomla/trunk/components/com_apoll/models/index.html new file mode 100644 index 00000000..fa6d84e8 --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/models/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/router.php b/oss/joomla/trunk/components/com_apoll/router.php new file mode 100644 index 00000000..9a2fa7ca --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/router.php @@ -0,0 +1,148 @@ +getItems('componentid', $component->id); + } + + // Search for an appropriate menu item. + // First we will search for menu item linking to this poll, + // if we can't find we will look for a menu item linking to the apolls view. + + if (is_array($items)) + { + // If only the option and itemid are specified in the query, return that item. + if (!isset($query['view']) && !isset($query['id']) && isset($query['Itemid'])) { + $itemid = (int) $query['Itemid']; + } + + // Search for a menu that links to the same view and id + if (!$itemid) { + foreach ($items as $item) + { + // Check if this menu item links to this view. + // menu-view must be equal to the query-view + // also menu-id must be equal to the query-id + if (isset($item->query['view']) && isset($query['view']) + && $item->query['view'] == $query['view'] + && isset($item->query['id']) + && $item->query['id'] == $query['id']) + { + $itemid = $item->id; + + } + } + } + + // If no specific link has been found, search for a general one. + // Search for a menu linking to apolls view + if (!$itemid) { + foreach ($items as $item) + { + if (isset($query['view']) && $query['view'] == 'apoll' && isset($item->query['view']) && $item->query['view'] == 'apolls') + { + // Check for an undealt with poll id. + if (isset($query['id'])) + { + // This menu item links to the apolls view but we need to append the poll id to it. + $itemid = $item->id; + $segments[] = isset($query['alias']) ? $query['id'].':'.$query['alias'] : $query['id']; + break; + } + } + } + } + } + + // Check if the router found an appropriate itemid. + if (!$itemid) + { + //if item id was not found that means that there is no link to either apolls view or apoll view + // Check if a id was specified. + if (isset($query['id'])) + { + if (isset($query['alias'])) { + $query['id'] .= ':'.$query['alias']; + } + + // Push the id onto the stack. + $segments[] = 'apoll'; + $segments[] = $query['id']; + + unset($query['id']); + unset($query['alias']); + } + unset($query['view']); + } + else + { + $query['Itemid'] = $itemid; + + // Remove the unnecessary URL segments. + unset($query['view']); + unset($query['id']); + unset($query['alias']); + } + //print_r($segments); exit; + return $segments; +} + +/** + * @param array + * @return array + */ +function ApollParseRoute( $segments ) +{ + $vars = array(); + + //Get the active menu item if there is a menu link to apolls view + $menu =& JSite::getMenu(); + $item =& $menu->getActive(); + // Count route segments + $count = count( $segments ); + + //if item is not set, there is no menu linking to this view - apoll + if(!isset($item)) + { + $vars['view'] = 'apoll'; + $vars['id'] = $segments[$count - 1]; + return $vars; + } + //if there is a menu to apolls view -> + $vars['view'] = 'apoll'; + $vars['id'] = $segments[$count-1]; + + return $vars; +} \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/views/apoll/index.html b/oss/joomla/trunk/components/com_apoll/views/apoll/index.html new file mode 100644 index 00000000..fa6d84e8 --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/views/apoll/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/oss/joomla/trunk/components/com_apoll/views/apoll/metadata.xml b/oss/joomla/trunk/components/com_apoll/views/apoll/metadata.xml new file mode 100644 index 00000000..8deb646d --- /dev/null +++ b/oss/joomla/trunk/components/com_apoll/views/apoll/metadata.xml @@ -0,0 +1,9 @@ + +".JText::_($this->msg)."
"; + endif; + endif; ?> + +params->get('show_dropdown')) : ?> + + +| + + | ++ : + options[0])) echo $this->options[0]->voters; ?> + | +
| + + | ++ : + apoll->publish_up; ?> + | +
| + + | ++ : + apoll->publish_down; ?> + | +
| + text; ?> + | +|
| + params->get('show_hits')) { ?> + hits; ?> + + | +
+
+ percent; ?>%
+ |
+