From 2b6f73038151024ef53dfbbbc04ef8ea4044f20f Mon Sep 17 00:00:00 2001 From: Ashwin Date Date: Wed, 20 Feb 2013 20:39:38 +0530 Subject: [PATCH 1/2] Added random assign feature and email winners feature --- .../components/com_giveaway/config.xml | 11 +++ .../com_giveaway/controllers/swaglist.php | 87 +++++++++++++++++++ .../views/attendeelist/tmpl/default.php | 3 +- .../views/swaglist/tmpl/default.php | 29 ++++++- 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 administrator/components/com_giveaway/config.xml diff --git a/administrator/components/com_giveaway/config.xml b/administrator/components/com_giveaway/config.xml new file mode 100644 index 0000000..8ceeb9c --- /dev/null +++ b/administrator/components/com_giveaway/config.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/administrator/components/com_giveaway/controllers/swaglist.php b/administrator/components/com_giveaway/controllers/swaglist.php index 26ff25e..ebe7327 100644 --- a/administrator/components/com_giveaway/controllers/swaglist.php +++ b/administrator/components/com_giveaway/controllers/swaglist.php @@ -2,6 +2,8 @@ defined( '_JEXEC' ) or die( 'Restricted access' ); jimport('joomla.application.component.controller'); +//error_reporting(E_ALL); +//ini_set('display_errors', 'On'); class GiveawayControllerSwaglist extends JController { @@ -69,6 +71,91 @@ public function import() $this->setRedirect('index.php?option=com_giveaway&view=swaglist', 'Swag Imported'); } + function simulate() { + $email = JRequest::getVar('simulateemail'); + $this->sendswagmail($email); + } + + function sendemail() { + $this->sendswagmail(); + } + + function sendswagmail($winneremail = null) { + $params = &JComponentHelper::getParams( 'com_giveaway' ); + $mainframe = JFactory::getApplication(); + + $subject = $params->get('email_subject'); + $body = $params->get('email_message'); + $fromname = $params->get('email_fromname', $mainframe->getCfg('fromname')); + $fronemail = $params->get('email_fromemail', $mainframe->getCfg('mailfrom')); + + $db = JFactory::getDBO(); + $qry = 'SELECT s.swag_id, s.name as swagname, a.attendee_id, a.name AS attendee_name, a.email + FROM #__giveaway_swag AS s + LEFT JOIN #__giveaway_attendee AS a ON s.attendee_id=a.attendee_id + '; + $db->setQuery($qry); + $swagrows = $db->loadAssocList(); + + echo ''; + echo ''; + foreach ($swagrows as $swagrow) { + $recipient = $winneremail ? $winneremail : $swagrow['email']; + //$recipient = $winneremail ? $winneremail : 'parth.lawate@tekdi.net'; + $text = $body; + foreach ($swagrow as $k=>$v) { + $text = str_replace('{'.$k.'}', $v, $text); + } + + //JUtility::sendmail($fronemail, $fromname, $recipient, $subject, $text); + $done = JUtility::sendmail($fronemail, $fromname, $recipient, $subject, $text); + $status = $done ? 'Mail Sent' : 'Mail not sent'; + echo ''; + echo ''; + } + echo '
Attendee NameAttendee EmailSwagStatus
'.$swagrow['attendee_name'].''.$recipient.''.$swagrow['swagname'].''.$status.'
'; + + } + + function randomassign() { + $db = JFactory::getDBO(); + + $qry = "SELECT COUNT(-1) FROM #__giveaway_swag"; + $swags = $db->setQuery($qry); + $swag_count = $db->loadResult(); + + if (!$swag_count) { + $this->setRedirect('index.php?option=com_giveaway&view=swaglist', 'No Swags'); + } + + $qry = "SELECT attendee_id FROM #__giveaway_attendee LIMIT {$swag_count}"; + $db->setQuery($qry); + $winners = $db->loadResultArray(); + + $done = 0; + $tmp_array = array(); + for ($i=0; $i<$swag_count; $i++) { + + // Populate tmp array if its empty + // This is to ensure all attendees get at least 1 swag + if (!count($tmp_array)) { + $tmp_array = $winners; + } + + $key = array_rand($tmp_array); + $winner = $tmp_array[$key]; + $qry = "UPDATE #__giveaway_swag SET attendee_id = {$winner} WHERE (attendee_id = 0 OR attendee_id IS NULL) LIMIT 1"; + $db->setQuery($qry); + if ($db->query()) { + $tmp_array[$key]; + $done++; + } + } + + $this->setRedirect('index.php?option=com_giveaway&view=swaglist', 'Swags randomly assigned to attendees'); + + } + function display() { $view = JRequest::getVar('view', ''); diff --git a/administrator/components/com_giveaway/views/attendeelist/tmpl/default.php b/administrator/components/com_giveaway/views/attendeelist/tmpl/default.php index eef76f2..79a3f7e 100644 --- a/administrator/components/com_giveaway/views/attendeelist/tmpl/default.php +++ b/administrator/components/com_giveaway/views/attendeelist/tmpl/default.php @@ -6,6 +6,7 @@ JToolBarHelper::deleteList(); JToolBarHelper::editList(); JToolBarHelper::addNew(); +JToolBarHelper::preferences('com_giveaway', 400, 600); ?>
@@ -71,4 +72,4 @@ -
\ No newline at end of file + diff --git a/administrator/components/com_giveaway/views/swaglist/tmpl/default.php b/administrator/components/com_giveaway/views/swaglist/tmpl/default.php index 5cdd0b0..e12f751 100644 --- a/administrator/components/com_giveaway/views/swaglist/tmpl/default.php +++ b/administrator/components/com_giveaway/views/swaglist/tmpl/default.php @@ -3,9 +3,13 @@ JToolBarHelper::title( 'Swag List' ); +JToolBarHelper::custom('simulate', 'assign', 'assign', 'Simulate Email', false); +JToolBarHelper::custom('sendemail', 'assign', 'assign', 'Send Gifts!', false); +JToolBarHelper::custom('randomassign', 'assign', 'assign', 'Random Assign', false); JToolBarHelper::deleteList(); JToolBarHelper::editList(); JToolBarHelper::addNew(); +JToolBarHelper::preferences('com_giveaway', 400, 600); $document = JFactory::getDocument(); $document->addScript(JURI::base() . 'components/com_giveaway/views/swaglist/tmpl/default.js'); @@ -13,6 +17,28 @@ ?> +

Download CSV

@@ -61,6 +87,7 @@ +

 

@@ -80,4 +107,4 @@ - \ No newline at end of file + From 59dc1030ef86a9173402c9484f9c93c80ff71c72 Mon Sep 17 00:00:00 2001 From: Ashwin Date Date: Mon, 1 Apr 2013 19:43:14 +0530 Subject: [PATCH 2/2] Removed hardcoded email, added JText --- .../components/com_giveaway/controllers/swaglist.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/administrator/components/com_giveaway/controllers/swaglist.php b/administrator/components/com_giveaway/controllers/swaglist.php index ebe7327..67d068f 100644 --- a/administrator/components/com_giveaway/controllers/swaglist.php +++ b/administrator/components/com_giveaway/controllers/swaglist.php @@ -100,16 +100,15 @@ function sendswagmail($winneremail = null) { echo '
'; echo ''; foreach ($swagrows as $swagrow) { - $recipient = $winneremail ? $winneremail : $swagrow['email']; - //$recipient = $winneremail ? $winneremail : 'parth.lawate@tekdi.net'; $text = $body; + $recipient = $winneremail ? $winneremail : $swagrow['email']; + foreach ($swagrow as $k=>$v) { $text = str_replace('{'.$k.'}', $v, $text); } - //JUtility::sendmail($fronemail, $fromname, $recipient, $subject, $text); $done = JUtility::sendmail($fronemail, $fromname, $recipient, $subject, $text); - $status = $done ? 'Mail Sent' : 'Mail not sent'; + $status = $done ? JText::_('Mail Sent') : JText::_('Mail not sent'); echo ''; echo ''; }
Attendee NameAttendee EmailSwagStatus
'.$swagrow['attendee_name'].''.$recipient.''.$swagrow['swagname'].''.$status.'