Created
August 26, 2013 00:21
-
-
Save anonymous/6337151 to your computer and use it in GitHub Desktop.
DecisionMaking_Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DecisionMaking_Controller extends Controller { | |
public static $allowed_actions = array( | |
'index' => true, | |
'undecided' => true, | |
'decided' => true, | |
'deciding' => true, | |
'factor' => true | |
); | |
public static $_CurrentDecisionID = 0; | |
public function init(){ | |
var_dump($this->request->allParams()); | |
$GetDecisionID = $this->request->getVar('DecisionID'); | |
$PostDecisionID = $this->request->postVar('DecisionID'); | |
if(isset($GetDecisionID)){ | |
self::$_CurrentDecisionID = $GetDecisionID; | |
} | |
if(isset($PostDecisionID)){ | |
self::$_CurrentDecisionID = $PostDecisionID; | |
} | |
self::$_CurrentDecisionID = isset(self::$_CurrentDecisionID) ? self::$_CurrentDecisionID : 0; | |
if(!isset(self::$_CurrentDecisionID) && Session::get('CurrentDecisionID')){ | |
self::$_CurrentDecisionID = Session::get('CurrentDecisionID'); | |
} | |
Session::set('CurrentDecisionID', self::$_CurrentDecisionID); | |
parent::init(); | |
} | |
public function index() { | |
echo 'rendering through index action'; | |
return $this->renderWith(array('Page', 'Page')); | |
} | |
public function deciding() { | |
echo 'calling deciding action'; | |
return $this->renderWith(array('CurrentDecision', 'Page')); | |
} | |
/** | |
* Returns a link to this controller. Overload with your own Link rules if they exist. | |
*/ | |
public function Link() { | |
return '/'; | |
} | |
public function undecided(SS_HTTPRequest $request) { | |
return $this->renderWith("Page","Page"); | |
} | |
public function decided(SS_HTTPRequest $request) { | |
echo 'decided'; | |
print_r($arguments); | |
} | |
public function DecideForm() { | |
$this->WritingDecision($this->request->postVars()); | |
$getVars = ''; | |
if(self::$_CurrentDecisionID){ | |
$getVars .= '&DecisionID=' . self::$_CurrentDecisionID; | |
} | |
$fields = new FieldList(); | |
$fields->push(new CheckboxField('YesOrNo','A Decision Statement must be Yes or No. Is your statement in that form?<br />Are you ready to put to vote?',0)); | |
$fields->push(new CheckboxField('AddFactor','Do you want to <a href="decision/?DecidingFactorView=1' . $getVars .'">evaluate Deciding Factors</a>?')); | |
$fields->push(new CheckboxField('Outcome','Do you want to <a href="decision/?OutcomeView=1' . $getVars .'">posit Outcomes</a>?')); | |
$fields->push(new CheckboxField('LinkBack','<a href="/decision?' . $getVars . '">Just make a decision.</a>')); | |
if($this->request->getVar('DecidingFactorView')){ | |
$fields->push(new TextField('DecidingFactorContent','')); | |
$ActionText = 'Write this Deciding Factor.'; | |
} else if($this->request->getVar('OutcomeView')){ | |
$fields->push(new TextField('OutcomeContent','')); | |
$ActionText = 'Write this Outcome.'; | |
} else { | |
$fields->push(new TextField('DecisionStatement')); | |
$ActionText = 'Write Decision Statement.'; | |
} | |
if(isset(self::$_CurrentDecisionID)){ | |
$fields->push(new HiddenField('DecisionID','', self::$_CurrentDecisionID)); | |
} | |
$actions = new FieldList( | |
FormAction::create("DecideForm")->setTitle($ActionText) | |
); | |
$form = new Form($this, 'DecideForm', $fields, $actions); | |
// Load the form with previously sent data | |
$form->loadDataFrom($this->request->postVars()); | |
return $form; | |
} | |
public function WritingDecision($data){ | |
if(!empty($data['DecisionID']) || self::$_CurrentDecisionID){ | |
$DecisionID = isset(self::$_CurrentDecisionID) ? self::$_CurrentDecisionID : $data['DecisionID']; | |
$Decision = Decision::get_by_id('Decision',$DecisionID); | |
} elseif(isset($data['DecidingFactorContent']) || isset($data['OutcomeContent']) || isset($data['DecisionStatement'])) { | |
$Decision = new Decision(); | |
} | |
if(isset($Decision) && (isset($data['DecisionStatement']) || isset($data['PassFail']))){ | |
$Decision->Content = isset($data['DecisionStatement']) ? Convert::raw2sql($data['DecisionStatement']) : $Decision->Content; | |
$Decision->PassFail = isset($data['PassFail']) ? Convert::raw2sql($data['PassFail']) : $Decision->PassFail; | |
$Decision->write(); | |
self::$_CurrentDecisionID = $Decision->ID; | |
} | |
if(isset($data['DecidingFactorContent'])){ | |
$DecidingFactor = new DecidingFactor(); | |
$DecidingFactor->Content = isset($data['DecidingFactorContent']) ? Convert::raw2sql($data['DecidingFactorContent']) : $DecidingFactor->Content; | |
$DecidingFactor->DecisionID = intval($Decision->ID); | |
$DecidingFactor->write(); | |
} | |
if(isset($data['OutcomeContent'])){ | |
$Outcome = new Outcome(); | |
$Outcome->Content = isset($data['OutcomeContent']) ? Convert::raw2sql($data['OutcomeContent']) : $DecidingFactor->Content; | |
$Outcome->DecisionID = intval($Decision->ID); | |
$Outcome->write(); | |
} | |
return true; | |
} | |
public function getCurrentDecisionID(){ | |
return self::$_CurrentDecisionID; | |
} | |
public function DecisionContent(){ | |
if(self::$_CurrentDecisionID != 0){ | |
$Decision = DataObject::get_by_id('Decision',self::$_CurrentDecisionID); | |
if(isset($Decision)){ | |
return $Decision->Content; | |
} | |
} | |
return false; | |
} | |
public function getDecidingFactors(){ | |
if(self::$_CurrentDecisionID){ | |
return DataObject::get('DecidingFactor')->filter('DecisionID',self::$_CurrentDecisionID); | |
} | |
return false; | |
} | |
public function getOutcomes(){ | |
if(self::$_CurrentDecisionID){ | |
return DataObject::get('Outcome')->filter('DecisionID',self::$_CurrentDecisionID); | |
} | |
return false; | |
} | |
public function MostRecentPendingDecisions($limit = 10){ | |
$Decisions = Decision::get()->sort('Created Desc')->limit($limit); | |
foreach($Decisions as $Decision){ | |
if(Member::currentUserID()){ | |
$Decision->VoteLink = "voter/CastVote/" . $Decision->ID . '/' . Member::CurrentUserID(); | |
} else{ | |
$Decision->VoteLink = "voter/RegisterVoter"; | |
} | |
} | |
return $Decisions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment