Skip to content

Instantly share code, notes, and snippets.

@e2thex
Created June 19, 2012 17:48
Show Gist options
  • Select an option

  • Save e2thex/2955522 to your computer and use it in GitHub Desktop.

Select an option

Save e2thex/2955522 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\sps;
interface OverrideInterface {
/**
* construct an array of override arrays
*
* @return an array of override arrays
*/
public function getOverrides();
}
<?php
namespace Drupal\sps
/**
* a helper class for the PreviewForm
* this wrap up the methods around each condition
*/
class Condition {
protected $widget;
protected $override;
protected $title;
public function __constuct($config) {
$override = $config['override'];
$widget = $config['widget'];
$this->widget = $config['widget'];
$this->override = $config['override'];
$this->title = $config['title'];
}
public getWidget() {
$this->getPlugin("widget", $this->widget);
}
public getOverride() {
$this->getPlugin("override", $this->override);
}
protected Plugin($type, $name) {
return "cool stuff"
}
}
/**
* PreviewForm is used for constructing and processing the form used to set the site state
*/
class PreviewForm {
protected $conditions;
protected $override;
/**
* Set the configuration for the PreviewForm
*
* @param $config
* an array of the following form
* array (
* 'conditions' => array(
* 'collection' => array(
* 'title' => 'Collection',
* 'widget' => 'collection_select', (widget plugin)
* 'override' => 'view_collection_override', (override plugin)
* ),
* 'date' => array(
* 'title' => 'Live Date',
* 'widget' => 'live_date', (widget plugin)
* 'override' => 'view_live_date_override', (override plugin)
* ),
* ),
* );
*/
public function setConfig($config) {
$this->conditions = array_map(function($data) { return new Condition($data);}, $config['conditions']);
/**
* Construct the Form using the conditions
*
* @param $form
* the form structure array passed in by drupal_get_form().
* @param $form_state
* the array of state data for the form.
*
* @return
* a FAPI render array
*/
function getPreviewForm($form, $form_state) {
return $form;
}
/**
* Validates the preview form.
*
* @param $form
* The FAPI form to validate
* @param $form_state
* The state for the form
*
* @return
* self
*/
function validatePreviewForm($form, $form_state) {
return $this;
}
/**
* Extracts the values for the conditions from the FormAPI array.
*
* @param $form
* The FAPI form to validate
* @param $form_state
* The state for the form
* @return
* self
*/
function sumbitPreviewForm($form, $form_state) {
return $this;
}
/**
* retrieve the Conditions override object
*
* @return a \Drupal\sps\Override object
*/
function getOverride() {
return $this->override;
}
}
<?php
namespace Drupal\sps
interface PreviewFormInterface {
/**
* Set the configuration for the PreviewForm
*
* @param $config
* an array of the following form
* array (
* 'conditions' => array(
* 'collection' => array(
* 'title' => 'Collection',
* 'widget' => 'collection_select', (widget plugin)
* 'override' => 'view_collection_override', (override plugin)
* ),
* 'date' => array(
* 'title' => 'Live Date',
* 'widget' => 'live_date', (widget plugin)
* 'override' => 'view_live_date_override', (override plugin)
* ),
* ),
* );
*/
public function setConfig($config);
/**
* Get the FormAPI preview form for the condition.
*
* @param $form
* the form structure array passed in by drupal_get_form().
* @param $form_state
* the array of state data for the form.
*
* @return
* a FAPI render array
*/
function getPreviewForm($form, $form_state);
/**
* retrieve the Conditions override object
*
* @return a \Drupal\sps\Override object
*/
public function getOverride();
/**
* Validates the preview form.
*
* @param $form
* The FAPI form to validate
* @param $form_state
* The state for the form
*
* @return
* self
*/
public function validatePreviewForm($form, $form_state);
/**
* Extracts the values for the conditions from the FormAPI array.
*
* @param $form
* The FAPI form to validate
* @param $form_state
* The state for the form
* @return
* self
*/
public function sumbitPreviewForm($form, $form_state);
}
<?php
interface StorageController {}
public function save($table);
public function getRevisionID($id);
public function getMap();
}
class SiteState {
protected $cacheController;
protected $conditions = array();
/**
*
*/
public function __constuct($cacheController) {
$this->$cacheController = $cacheController
}
/**
* SiteState::addCondition
*
*/
public addCondition($condition) {
if( $condition instanceof ConditionInterface) {
$this->$conditions[] = $condition;
}
else {
throw ....
}
}
/*
* SiteState:getOverrides
*
* RETURN array of assoc arrays
*/
public function getOverrides() {
if(!$this->cacheController->hasCache()) {
$this->buildOverrides();
}
return $this->cacheController->getMap();
}
/*
* SiteState:buildOverrides()
*
* Construct and Cache Overrides for the current state;
*/
protected function buildOverrides() {
$ss_of_overrides = array();
foreach($this->conditions as $condition) {
$set_of_overrides[] = $condition->setter()->getOverrides();
}
$overrides = $this->combineOverrides($set_of_overrides);
$this->save($overrides);
}
/**
* SiteState::combineOverrides
*
* PARAM set_of_overrides: an array of override arrays
* RETURN : an override array
*/
protected function combineOverrides($setOfOverrides) {
var $final_overrides = array();
foreach(array_reverse($setOfOverrides) as $overrides) {
foreach($overrides as $override) {
var $key = "{$override->type}.{$override->id}";
$final_overrides[$key] = $overrides;
}
}
return array_values($final_overrides);
}
}
<?php
namespace Drupal\sps\Condition;
interface WidgetInterface {
/**
* Get the FormAPI preview form for the condition.
*
* @param $form
* the form structure array passed in by drupal_get_form().
* @param $form_state
* the array of state data for the form.
*
* @return
* a FAPI render array
*/
function getPreviewForm($form, $form_state, $element);
/**
* Validates the preview form.
*
* @param $form
* The FAPI form to validate
* @param $form_state
* The state for the form
*
* @return
* self
*/
function validatePreviewForm($form, $form_state, $element);
/**
* Extracts the values for the conditions from the FormAPI array.
*
* @param $values
* The values key from the $form_state of a submitted condition form
*
* @return
* An associative array of values
*/
function extractValues($values, $form, $form_state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment