Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added random assignment feature and possibility of sending emails from component #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions administrator/components/com_giveaway/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<params>

<param name="email_subject" size="30" type="text" default="" label="Email Subject" description="Subject of the email that is sent to winners" />
<param name="email_message" type="textarea" rows="10" cols="40" default="" label="Email Text" description="Content of the email that is sent to winners" />
<param name="email_fromname" size="30" type="text" default="" label="From Name" description="" />
<param name="email_fromemail" size="30" type="text" default="" label="From Email" description="" />

</params>
</config>
86 changes: 86 additions & 0 deletions administrator/components/com_giveaway/controllers/swaglist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');
//error_reporting(E_ALL);
//ini_set('display_errors', 'On');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this commented code.


class GiveawayControllerSwaglist extends JController
{
Expand Down Expand Up @@ -69,6 +71,90 @@ 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 '<table width="100%">';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this markup being buffered and sent in the email body, or is this being displayed on screen? If on screen, this should be in a view.

echo '<tr><th align="left">Attendee Name</th><th align="left">Attendee Email</th><th align="left">Swag</th><th align="left">Status</th></tr>';
foreach ($swagrows as $swagrow) {
$text = $body;
$recipient = $winneremail ? $winneremail : $swagrow['email'];

foreach ($swagrow as $k=>$v) {
$text = str_replace('{'.$k.'}', $v, $text);
}

$done = JUtility::sendmail($fronemail, $fromname, $recipient, $subject, $text);
$status = $done ? JText::_('Mail Sent') : JText::_('Mail not sent');
echo '<tr><td>'.$swagrow['attendee_name'].'</td><td>'.$recipient.'</td>';
echo '<td>'.$swagrow['swagname'].'</td><td>'.$status.'</td></tr>';
}
echo '</table>';

}

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', '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
JToolBarHelper::deleteList();
JToolBarHelper::editList();
JToolBarHelper::addNew();
JToolBarHelper::preferences('com_giveaway', 400, 600);

?>
<form action="index.php" method="post" name="adminForm">
Expand Down Expand Up @@ -71,4 +72,4 @@
<?php echo JHTML::_( 'form.token' ); ?>
</fieldset>

</form>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,42 @@

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');
$document->addStyleSheet(JURI::base() . 'components/com_giveaway/views/swaglist/tmpl/default.css');


?>
<script>
function submitbutton(task) {
if (task == 'simulate') {
var simulate_email = prompt('This will send all the gift emails to a single email address.\nPlease enter email to which send the simulated emails');
if (simulate_email == null) {
alert('Need an email address to simulate');
return false;
}

document.forms[0].simulateemail.value = simulate_email;
}

if (task == 'sendemail') {
var confirmed = confirm('Send email to all winners? \nYou cannot call back a sent email.');
if (!confirmed) {
return false;
}
}

submitform( task );
}
</script>
<p><a href="index.php?option=com_giveaway&amp;view=swaglist&amp;format=raw">Download CSV</a></p>
<form action="index.php" method="post" name="adminForm">
<table class="adminlist">
Expand Down Expand Up @@ -61,6 +87,7 @@
<input type="hidden" name="option" value="com_giveaway" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="simulateemail" value="" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
<p>&nbsp;</p>
Expand All @@ -80,4 +107,4 @@
<?php echo JHTML::_( 'form.token' ); ?>
</fieldset>

</form>
</form>