title = "SAFE Game" url = "/games2" is_hidden = 0 [renderForm] formCode = "sas-game-signup" redirect = "enter-competition-thankyou" [captcha] == where('story_id', $storyKey) ->update($rowData); //Db::table($table)->insert($rowData); } function mailChimpSendHelper($list, $formData){ $url = 'https://www.safe.org.nz/services/mailchimp/index.php'; $key="123"; $action = "subscribe"; $list = $list; $firstName = $formData['story_contributor_name']; $lastName = $formData['story_contributor_lastname']; $emailAddress = $formData['story_contributor_email']; $mailChimpData = array( 'key' => '1234', 'action' => 'subscribe', 'list' => $list, 'firstname' => $firstName, 'lastname' => $lastName, 'emailaddress' => $emailAddress, ); // use key 'http' even if you send the request to https://... $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($mailChimpData), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result); } function mailChimpSubscribe($formData){ // echo '
';
// print_r($formData);
// die('');
$mailChimpLists = $formData['mailchimp_lists'];
if(stristr($mailChimpLists,'|')){
//More than one list
//split into array
$listOfLists = explode('|', $mailChimpLists);
for ($i=0; $i < count($listOfLists); $i++ ){
$ok = $this->mailChimpSendHelper($listOfLists[$i], $formData);
}
//Look through array, sending sub
} else {
// only one list
$ok = $this->mailChimpSendHelper($mailChimpLists, $formData);
}
}
// Competition form processing
function onFormSubmit(){
// scaffold
$rowData =[];
$rowData['story_contributor_name'] =post('firstname');
$rowData['story_contributor_lastname'] =post('lastname');
$rowData['story_contributor_email'] =post('emailaddress');
$rowData['story_contributor_age'] = post('age');
$rowData['mailchimp_lists'] = post('mailchimp_lists');
//$rowData['story_id'] = post('storykey');
$storyKey = post('storykey');
$ok = $this->mailChimpSubscribe($rowData);
$this->dbUpdate('safe_game_story',$rowData ,$storyKey);
// These variables are available inside the message as Twig
$vars = ['firstname' => post('firstname'), 'lastname' => post('lastname'), 'storyKey' => post('storykey')];
Mail::send('cloudworks.safe.sas::game.signup', $vars, function($message) {
$message->to('alex@safe.org.nz', 'Admin Person');
$message->bcc('support@prema.co.nz', 'Technimagician');
$message->subject('Your game story has been received');
});
}
// TODO Integrate into local onStart
// function onStart(){
// $storyId = uniqid();
// $this['storyId'] = $storyId;
// }
/***** End of competition form ***********/
/* Page helpers */
function removeDirDots($fileList){
$dirArr[]='.';
$dirArr[]='..';
$spriteArr = array_diff($fileList, $dirArr);
return $spriteArr;
}
function getDataSet($sql){
//Select products
$dataSet = DB::connection('mysql')->select($sql);
return $dataSet;
}
/**
* Get data for story (by UUID)
*/
function onRefreshStory(){
$sql = "SELECT * FROM safe_game_pages ORDER BY page_id DESC LIMIT 8";
$this['storypages'] = $this->getDataSet($sql);
}
function onGetStory($storyId){
if(isset($storyId)){
//TODO - deal with bad storykey
$sql = "SELECT * FROM safe_game_pages WHERE story_uuid = '$storyId'";
//Session:put('story.key', $storyId);
$this['storypages'] = $this->getDataSet($sql);
$this['storyName'] = $this['storypages'][0]->story_name;
}
}
function onSaveStory(){
// block save if viewer mode
if (get('viewer')==true){
exit();
}
//die('saving story');
// echo '';
// print_r(json_decode(post('jsonData')));
// die('');
// OK to here Wed 11 October 2017 04:09
//
$pageData = json_decode(post('jsonData'), true);
$dataSet = [];
$row = [];
for($i=0; $i<8; $i++){
//Fetch a row
$pageDataRow = $pageData[$i]; // one row
// echo '';
// print_r($pageData);
// die('');
// Extract data
//$storyId = $pageDataRow['storyid'];
$pageId = $pageDataRow['pageid'];
$pageName = $pageDataRow['pagelabel'];
$pageJson = $pageDataRow['pagejson'];
// echo '';
// print_r($pageDataRow);
// die('');
// echo '';
// print_r($pageJson);
// print_r($pageName);
// print_r($pageId);
// die('');
//Primary Key
$row['page_id'] = $pageId;
// Create page array
// $row['story_uuid'] = $storyId;
// $row['page_name'] = $pageLabel;
// $row['page_json'] = $pageLabel
// $row['notes'] = '';
// Beam 'er up '
Db::table('safe_game_pages')
->where('page_id', $pageId)
->update([
// 'story_uuid' => $storyId,
'page_name' => $pageName,
'page_json' => $pageJson
]);
}
return 'story saved';
}
/**
* Get the number of sprites in each category
*
*/
function getSpriteCount($spriteType){
$spriteStoragePath = storage_path()."/app/media/game/img/$spriteType";
$svgPath = $spriteStoragePath;
// Scan svg path for sprites
$fileList = scandir($svgPath);
// Remove dots
$svgList = $this->removeDirDots($fileList);
return count($svgList);
}
/**
*
* Manage palette page changes
*
*/
function onPalettePageChange(){
// Get post vars
$start = post('start');
$count = post('count');
$spriteType = post('type');
//die($spriteType);
// Set storage path
$spriteStoragePath = storage_path()."/app/media/game/img/$spriteType";
$svgPath = $spriteStoragePath;
// Scan svg path for sprites
$fileList = scandir($svgPath);
// Remove dots
$svgList = $this->removeDirDots($fileList);
// Slice a segment from the list
$svgSegment = array_slice($svgList, $start, $count);
$listName = $spriteType."List"; // eg "bg", "sprite" etc
$this[$listName] = $svgSegment;
}
function onCreateStory(){
//die('create Story');
//Get post vars
$storyName = post('storyname');
$storyUuid = uniqid();
$storyContributor = post('contributorname');
$emailAddress = post('emailaddress');
//$response = $_POST["g-recaptcha-response"];
/*
_____ _ _
| __ \ | | | |
| |__) |___ ___ __ _ _ __ | |_ ___| |__ __ _
| _ // _ \/ __/ _` | '_ \| __/ __| '_ \ / _` |
| | \ \ __/ (_| (_| | |_) | || (__| | | | (_| |
|_| \_\___|\___\__,_| .__/ \__\___|_| |_|\__,_|
| |
|_|
*/
// $url = 'https://www.google.com/recaptcha/api/siteverify';
// $data = array(
// 'secret' => '6Lf0BzUUAAAAAHg3eRhFu-3Gx-4KSf3lX9f_BF6_', // Secret
// 'response' => post("g-recaptcha-response")
// );
// $options = array(
// 'http' => array (
// 'method' => 'POST',
// 'content' => http_build_query($data)
// )
// );
// Create row array
$row['story_id'] = $storyUuid;
$row['story_name'] = $storyName;
$row['story_contributor_name'] = $storyContributor;
$row['story_contributor_email'] = $emailAddress;
Db::table('safe_game_story')->insert($row);
// Session:put('story.name', $storyName);
// Session:put('story.key', $storyUuid);
// Session:put('story.contributor', $storyContributor);
// Session:put('story.emailaddress', $emailAddress);
// Create page set (8 pages)
$this->createPageSet($storyUuid,$storyName);
$storyUrl = $this->currentPageUrl();
$storyLink = $storyUrl.'?story='.$storyUuid;
// add stuff to the data object
$data['storyLink'] = $storyLink;
// Save to twig var
$this['storyLink'] = $storyLink;
return $data;
//echo $storyUuid;
}
function createPageSet($storyUuid, $storyName){
$dataSet = [];
$row = [];
for($i=0; $i<8; $i++){
$row['story_uuid'] = $storyUuid;
$row['story_name'] = $storyName;
$row['page_name'] = '';
$row['page_json'] = '';
$row['notes'] = '';
$dataSet[]= $row;
}
Db::table('safe_game_pages')->insert($dataSet);
// Load the new story
$this->onGetStory($storyUuid);
return 'Ok';
}
function onGameformSubmit(){
// Grab the data
$formData=[];
$formData['contributor_firstname']=post('contributor_firstname');
$formData['contributor_lastname']=post('contributor_lastname');
$formData['contributor_age']=post('contributor_age');
$formData['contributor_email']=post('contributor_email');
$formData['contributor_storykey']=post('contributor_storykey');
$formData['contributor_notes']=post('contributor_notes');
}
function onPageUpdate(){
//print_r(post());
//die();
// Disable save
Db::table('safe_game_pages')
->where('page_id', post('page_id'))
->update(['page_name' => post('page_name'), 'page_json' => post('page_json'),]);
//echo '';
//print_r(post());
//die('');
}
// /** Program **/
function onStart(){
// Load default story Thu 12 October 2017 23:12
// $demoStoryKey = "0366888d-983d-11e7-bd29-06f5b15584a7";
// $this->onGetStory($demoStoryKey);
//
if(null !== get('viewer')){
$this['viewerStoryDataset'] = $this->getDataSet('SELECT * FROM safe_game_story WHERE in_competition = "yes"');
}
// Load story from link
if(null !==(get('story'))){
// "story" in url
$storyKey = get('story'); // Get story key
$this->onGetStory($storyKey); // Grab story
// Write the current story key
$this['storyKey'] = $storyKey;
}else{
$demoStoryKey = "0366888d-983d-11e7-bd29-06f5b15584a7";
$this['storyKey'] = $demoStoryKey;
$this['storyUrl'] = $this->currentPageUrl();
$this->onGetStory($demoStoryKey);
}
// Game Sign up form ??? Still used?
$this['frm_open'] = Form::ajax('onGameformSubmit', ['confirm' => 'Thank you for your message']);
$this['frm_firstname'] = Form::text('contributor_firstname', 'Your first name', ['class'=>'input']);
$this['frm_lastname']= Form::text('contributor_lastname', 'Your last name ',['class'=>'input']);
$this['frm_age']= Form::text('contributor_age', 'Your age',['class'=>'input']);
$this['frm_email']= Form::text('contributor_email', 'Email address',['class'=>'input']);
$this['frm_storykey']= Form::hidden('contributor_storykey','storykey',['class'=>'input']);
$this['frm_submit']= Form::submit('Send message',['class'=>'button text is_right']) ;
$this['frm_close']= Form::close();
// Get sprite counts for each of the categories
$this['bgTotalSprites'] = $this->getSpriteCount('bg');
$this['spriteTotalSprites'] = $this->getSpriteCount('sprite');
$this['bubbleTotalSprites'] = $this->getSpriteCount('bubble');
$this['natureTotalSprites'] = $this->getSpriteCount('nature');
// End of PHP
}
?>
==