2014-02-07 14:05:13 -06:00
< ? php
if ( ! defined ( 'APPLICATION' ))
exit ();
2014-02-06 14:44:21 -06:00
/* Copyright 2013 Zachary Doll */
/**
* Manage the yaga application including configuration and import / export
*
* @ since 1.0
* @ package Yaga
*/
class YagaController extends DashboardController {
/**
* @ var array These objects will be created on instantiation and available via
* $this -> ObjectName
*/
public $Uses = array ( 'Form' );
/**
* Make this look like a dashboard page and add the resources
*
* @ since 1.0
* @ access public
*/
public function Initialize () {
parent :: Initialize ();
$this -> Application = 'Yaga' ;
Gdn_Theme :: Section ( 'Dashboard' );
if ( $this -> Menu ) {
$this -> Menu -> HighlightRoute ( '/yaga' );
}
2014-02-06 15:08:29 -06:00
$this -> AddSideMenu ( 'yaga/settings' );
2014-02-07 14:05:13 -06:00
2014-02-06 14:44:21 -06:00
$this -> AddCssFile ( 'yaga.css' );
}
2014-02-07 14:05:13 -06:00
2014-02-06 14:44:21 -06:00
/**
* Redirect to settings by default
*/
public function Index () {
$this -> Settings ();
}
/**
* This handles all the core settings for the gamification application .
*
*/
public function Settings () {
$this -> Permission ( 'Garden.Settings.Manage' );
$this -> Title ( T ( 'Yaga.Settings' ));
// Get list of actions from the model and pass to the view
$ConfigModule = new ConfigurationModule ( $this );
$ConfigModule -> Initialize ( array (
2014-02-07 14:05:13 -06:00
'Yaga.Reactions.Enabled' => array (
'LabelCode' => 'Use Reactions' ,
'Control' => 'Checkbox'
),
'Yaga.Badges.Enabled' => array (
'LabelCode' => 'Use Badges' ,
'Control' => 'Checkbox'
),
'Yaga.Ranks.Enabled' => array (
'LabelCode' => 'Use Ranks' ,
'Control' => 'Checkbox'
),
'Yaga.LeaderBoard.Enabled' => array (
'LabelCode' => 'Show leaderboard on activity page' ,
'Control' => 'Checkbox'
),
'Yaga.LeaderBoard.Limit' => array (
'LabelCode' => 'Maximum number of leaders to show' ,
'Control' => 'Textbox' ,
'Options' => array (
'Size' => 45 ,
'class' => 'SmallInput'
)
2014-02-06 14:44:21 -06:00
)
));
$this -> ConfigurationModule = $ConfigModule ;
2014-02-06 15:35:35 -06:00
$this -> Render ( 'settings' );
2014-02-06 14:44:21 -06:00
}
public function Import () {
2014-02-06 15:08:29 -06:00
$this -> Title ( T ( 'Yaga.Import' ));
2014-02-06 14:44:21 -06:00
// Todo: Implement
2014-02-06 15:08:29 -06:00
$this -> Render ( 'transport' );
2014-02-06 14:44:21 -06:00
}
2014-02-07 14:05:13 -06:00
2014-02-06 14:44:21 -06:00
public function Export () {
2014-02-06 15:08:29 -06:00
$this -> Title ( T ( 'Yaga.Export' ));
2014-02-07 16:03:50 -06:00
if ( class_exists ( 'ZipArchive' )) {
$this -> _ExportAuto ();
2014-02-07 14:05:13 -06:00
}
else {
2014-02-07 16:03:50 -06:00
$this -> Form -> AddError ( 'You do not seem to have the minimum requirements to export your Yaga configuration automatically. Please reference manual_export.md for more information.' );
2014-02-07 14:05:13 -06:00
}
2014-02-07 16:03:50 -06:00
$this -> Render ( 'transport' );
}
2014-02-07 16:35:31 -06:00
protected function _ExportAuto ( $Path = NULL , $Exclude = array ()) {
2014-02-07 16:03:50 -06:00
$StartTime = microtime ( TRUE );
2014-02-07 16:35:31 -06:00
$Info = new stdClass ();
$Info -> Version = C ( 'Yaga.Version' , '?.?' );
$Info -> StartDate = date ( 'Y-m-d H:i:s' );
2014-02-07 16:03:50 -06:00
if ( is_null ( $Path )) {
$Path = PATH_UPLOADS . DS . 'export' . date ( 'Y-m-d His' ) . '.yaga.zip' ;
}
$FH = new ZipArchive ();
2014-02-07 16:35:31 -06:00
$Images = array ();
$Hashes = array ();
2014-02-07 16:03:50 -06:00
if ( $FH -> open ( $Path , ZipArchive :: CREATE ) != TRUE ) {
2014-02-07 16:35:31 -06:00
$this -> Form -> AddError ( 'Unable to create archive: ' . $FH -> getStatusString ());
2014-02-07 16:03:50 -06:00
return FALSE ;
}
2014-02-07 16:35:31 -06:00
// Add actions
if ( $Exclude [ 'actions' ] == FALSE ) {
$Info -> Actions = 'actions.yaga' ;
$Actions = Yaga :: ActionModel () -> Get ( 'Sort' , 'asc' );
$ActionData = serialize ( $Actions );
$FH -> addFromString ( 'actions.yaga' , $ActionData );
array_push ( $Hashes , md5 ( $ActionData ));
}
// Add ranks and associated image
if ( $Exclude [ 'ranks' ] == FALSE ) {
$Info -> Ranks = 'ranks.yaga' ;
$Ranks = Yaga :: RankModel () -> Get ( 'Level' , 'asc' );
$RankData = serialize ( $Ranks );
$FH -> addFromString ( 'ranks.yaga' , $RankData );
array_push ( $Images , C ( 'Yaga.Ranks.Photo' ), NULL );
array_push ( $Hashes , md5 ( $RankData ));
}
// Add badges and associated images
if ( $Exclude [ 'badges' ] == FALSE ) {
$Info -> Badges = 'badges.yaga' ;
$Badges = Yaga :: BadgeModel () -> Get ();
$BadgeData = serialize ( $Badges );
$FH -> addFromString ( 'badges.yaga' , $BadgeData );
aray_push ( $Hashes , md5 ( $BadgeData ));
foreach ( $Badges as $Badge ) {
array_push ( $Images , $Badge -> Photo );
}
2014-02-07 16:03:50 -06:00
}
2014-02-07 16:35:31 -06:00
// Add in any images
2014-02-07 16:03:50 -06:00
foreach ( $Images as $Image ) {
if ( ! is_null ( $Image )) {
if ( $FH -> addFile ( PATH_UPLOADS . DS . $Image , 'images' . DS . $Image ) != TRUE ) {
2014-02-07 16:35:31 -06:00
$this -> Form -> AddError ( 'Unable to add file: ' . $FH -> getStatusString ());
2014-02-07 16:03:50 -06:00
return FALSE ;
}
2014-02-07 16:35:31 -06:00
array_push ( $Hashes , md5 ( $Image ));
2014-02-07 16:03:50 -06:00
}
}
2014-02-07 16:35:31 -06:00
// Save all the hashes
sort ( $Hashes );
$Info -> MD5 = md5 ( implode ( ',' , $Hashes ));
$Info -> EndDate = date ( 'Y-m-d H:i:s' );
2014-02-07 16:03:50 -06:00
2014-02-07 14:05:13 -06:00
$EndTime = microtime ( TRUE );
$TotalTime = $EndTime - $StartTime ;
$m = floor ( $TotalTime / 60 );
$s = $TotalTime - ( $m * 60 );
2014-02-07 16:35:31 -06:00
$Info -> ElapsedTime = sprintf ( '%02d:%02.2f' , $m , $s );
2014-02-07 16:03:50 -06:00
2014-02-07 16:35:31 -06:00
$FH -> setArchiveComment ( serialize ( $Info ));
2014-02-07 16:03:50 -06:00
if ( $FH -> close ()) {
return TRUE ;
2014-02-07 14:05:13 -06:00
}
else {
2014-02-07 16:03:50 -06:00
$this -> Form -> AddError ( 'Unable to save archive: ' . $FH -> getStatusString ());
return FALSE ;
2014-02-07 14:05:13 -06:00
}
2014-02-06 14:44:21 -06:00
}
}