first import
This commit is contained in:
163
sites/all/modules/boxes/plugins/boxes_box.inc
Normal file
163
sites/all/modules/boxes/plugins/boxes_box.inc
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Abstract base class defining a box. A boxes content plugin provides a
|
||||
* form of options for configuring content and renders content for display.
|
||||
*
|
||||
* @see boxes_simple.
|
||||
*/
|
||||
abstract class boxes_box {
|
||||
static $boxes; // Static cache of box objects.
|
||||
public $delta;
|
||||
public $title;
|
||||
public $description;
|
||||
public $options;
|
||||
public $plugin_key;
|
||||
public $new;
|
||||
public $export_type;
|
||||
|
||||
/**
|
||||
* Load existing box by its unique identifier $delta.
|
||||
*/
|
||||
public static function load($delta, $reset = FALSE) {
|
||||
if (!isset(self::$boxes[$delta]) || $reset) {
|
||||
ctools_include('export');
|
||||
$box = ctools_export_load_object('box', 'names', array($delta));
|
||||
if (!empty($box) && $values = array_pop($box)) {
|
||||
self::$boxes[$delta] = self::factory($values->plugin_key, $values);
|
||||
self::$boxes[$delta]->new = FALSE;
|
||||
}
|
||||
}
|
||||
return isset(self::$boxes[$delta]) && get_class(self::$boxes[$delta]) != 'stdClass' ? self::$boxes[$delta] : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate, populate and return a box object.
|
||||
*
|
||||
* @param $plugin_key
|
||||
*
|
||||
* @param $values
|
||||
* An array with at least a plugin_key key identifying the plugin class to
|
||||
* use for instantiating this box.
|
||||
*/
|
||||
public static function factory($plugin_key, $values) {
|
||||
ctools_include('plugins');
|
||||
if ($class = ctools_plugin_load_class('boxes', 'plugins', $plugin_key, 'handler')) {
|
||||
|
||||
// While we actually prefer to get objects, we need to allow for either,
|
||||
// so we convert it all to arrays.
|
||||
if (is_object($values)) {
|
||||
$values = (array) $values;
|
||||
}
|
||||
|
||||
$box = new $class();
|
||||
$box->plugin_key = $plugin_key;
|
||||
|
||||
foreach ($box as $key => $value) {
|
||||
if (isset($values[$key])) {
|
||||
$box->$key = $values[$key];
|
||||
}
|
||||
}
|
||||
foreach ($box->options_defaults() as $key => $value) {
|
||||
if (isset($values[$key])) {
|
||||
$box->options[$key] = $values[$key];
|
||||
}
|
||||
}
|
||||
return $box;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new box.
|
||||
*/
|
||||
protected function __construct() {
|
||||
$this->new = TRUE; // A box is new unless it exists in the DB or in code.
|
||||
$this->options = $this->options_defaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the boxes cache.
|
||||
*
|
||||
* Both ctools and boxes current maintain caches, ctools of the config and
|
||||
* boxes of the loaded box objects. We clear them both.
|
||||
*/
|
||||
public static function reset() {
|
||||
ctools_include('export');
|
||||
ctools_export_load_object_reset('box');
|
||||
self::$boxes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a box.
|
||||
*/
|
||||
public function save() {
|
||||
if (empty($this->delta)) {
|
||||
throw new Exception(t('Cannot save box without a specified delta.'));
|
||||
}
|
||||
self::reset();
|
||||
$existing = boxes_box_load($this->delta);
|
||||
if ($existing && ($existing->export_type & EXPORT_IN_DATABASE)) {
|
||||
drupal_write_record('box', $this, array('delta'));
|
||||
}
|
||||
else {
|
||||
drupal_write_record('box', $this);
|
||||
}
|
||||
$this->new = FALSE;
|
||||
self::reset();
|
||||
module_exists('context') ? context_invalidate_cache() : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a box.
|
||||
*/
|
||||
public function delete() {
|
||||
self::reset();
|
||||
unset(self::$boxes[$this->delta]);
|
||||
db_delete('box')
|
||||
->condition('delta', $this->delta)
|
||||
->execute();
|
||||
module_exists('context') ? context_invalidate_cache() : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare if the box should use a multistep form for the create form.
|
||||
*
|
||||
* This might me necessary for forms that use ajax on the options form.
|
||||
* Currently Context does not load this block correctly and the ajax in the
|
||||
* form will not work. Methinks Context UI Editor needs to be upgraded to
|
||||
* D7 AJAX framework for this to not be required. That said the functionality
|
||||
* is potentially useful even with proper functioning AJAX.
|
||||
*/
|
||||
public function use_multistep_create() {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the block cache settings for this box. Subclasses can override this
|
||||
* to perform more intricate operations around deciding the cache settings of
|
||||
* the specific box instance.
|
||||
*/
|
||||
public function cache_setting() {
|
||||
return DRUPAL_CACHE_CUSTOM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare default options.
|
||||
*/
|
||||
abstract public function options_defaults();
|
||||
|
||||
/**
|
||||
* Provide options to configure content.
|
||||
*/
|
||||
abstract public function options_form(&$form_state);
|
||||
|
||||
/**
|
||||
* Render a block. Must return an array with the keys
|
||||
* 'delta', 'title', 'subject' (same as title) and 'content'.
|
||||
*
|
||||
* title AND subject need to be present to avoid that block module overrides
|
||||
* title.
|
||||
*/
|
||||
abstract public function render();
|
||||
}
|
56
sites/all/modules/boxes/plugins/boxes_simple.inc
Normal file
56
sites/all/modules/boxes/plugins/boxes_simple.inc
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simple custom text box.
|
||||
*/
|
||||
class boxes_simple extends boxes_box {
|
||||
/**
|
||||
* Implementation of boxes_box::options_defaults().
|
||||
*/
|
||||
public function options_defaults() {
|
||||
return array(
|
||||
'body' => array(
|
||||
'value' => '',
|
||||
'format' => filter_default_format(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of boxes_box::options_form().
|
||||
*/
|
||||
public function options_form(&$form_state) {
|
||||
$format = filter_format_load($this->options['body']['format']);
|
||||
|
||||
if (filter_access($format)) {
|
||||
$form = array();
|
||||
$form['body'] = array(
|
||||
'#type' => 'text_format',
|
||||
'#base_type' => 'textarea',
|
||||
'#title' => t('Box body'),
|
||||
'#default_value' => $this->options['body']['value'],
|
||||
'#rows' => 6,
|
||||
'#format' => $this->options['body']['format'] ? $this->options['body']['format'] : NULL,
|
||||
'#description' => t('The content of the block as shown to the user.'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of boxes_box::render().
|
||||
*/
|
||||
public function render() {
|
||||
$content = '';
|
||||
if (!empty($this->options['body']['value']) && isset($this->options['body']['format'])) {
|
||||
$content = check_markup($this->options['body']['value'], $this->options['body']['format'], $langcode = '' /* TODO Set this variable. */, FALSE);
|
||||
}
|
||||
$title = isset($this->title) ? $this->title : NULL;
|
||||
return array(
|
||||
'delta' => $this->delta, // Crucial.
|
||||
'title' => $title,
|
||||
'subject' => check_plain($title),
|
||||
'content' => $content,
|
||||
);
|
||||
}
|
||||
}
|
45
sites/all/modules/boxes/plugins/spaces_controller_boxes.inc
Normal file
45
sites/all/modules/boxes/plugins/spaces_controller_boxes.inc
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Spaces controller for boxes module.
|
||||
*/
|
||||
class spaces_controller_boxes extends spaces_controller {
|
||||
|
||||
// Override of load_original_values
|
||||
protected function load_original_values($id = NULL) {
|
||||
if (empty($this->loaded_all['original'])) {
|
||||
// TODO can we always rely on having export.inc loaded?
|
||||
if (!isset($id)) {
|
||||
$boxes = array();
|
||||
foreach (ctools_export_load_object('box') as $box) {
|
||||
$boxes[$box->delta] = $box;
|
||||
}
|
||||
$this->values['original'] = $boxes;
|
||||
$this->loaded_all['original'] = TRUE;
|
||||
}
|
||||
else if (!isset($this->loaded['original'][$id])) {
|
||||
if ($box = array_pop(ctools_export_load_object('box', 'names', array($id)))) {
|
||||
$this->values['original'][$id] = $box;
|
||||
$this->loaded['original'][$id] = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override of the set() method.
|
||||
*
|
||||
* Remove flags used by to detect state and transflorm to stdClass so that the
|
||||
* box configuration can be loaded regardless of whether the plugin is
|
||||
* available.
|
||||
*/
|
||||
function set($id, $value) {
|
||||
unset($value->new);
|
||||
unset($value->export_type);
|
||||
$box = new stdClass();
|
||||
foreach ($value as $k => $v) {
|
||||
$box->$k = $v;
|
||||
}
|
||||
parent::set($id, $box);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user