first import
This commit is contained in:
308
sites/all/modules/styles/includes/Styles.inc
Normal file
308
sites/all/modules/styles/includes/Styles.inc
Normal file
@@ -0,0 +1,308 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file Styles.inc
|
||||
* Base class for Styles.
|
||||
*/
|
||||
|
||||
class StylesDefault {
|
||||
// The variables passed from our theme.
|
||||
public $variables = array();
|
||||
|
||||
// The object passed from our theme.
|
||||
public $object = array();
|
||||
|
||||
// Any effects to apply when displaying content matching this style.
|
||||
public $effects = array();
|
||||
|
||||
// The final rendered output for this item.
|
||||
public $output;
|
||||
public $prefix;
|
||||
public $suffix;
|
||||
|
||||
public $wrapperType = 'div';
|
||||
public $classes = array('styles');
|
||||
|
||||
public $id;
|
||||
private $_id = 0;
|
||||
|
||||
public function __construct($object = NULL, $effects = NULL, $variables = NULL) {
|
||||
// @TODO: This is not great IMO, the ->object and ->variables props already have everything
|
||||
// we shouldn't be duplicating it in different methods / properties.
|
||||
if (isset($variables)) {
|
||||
$properties = (array) $variables;
|
||||
$this->magicSet($properties);
|
||||
$this->setVariables($variables);
|
||||
}
|
||||
// If we are passed an array, then set the object properties from its keys.
|
||||
if (isset($object)) {
|
||||
$properties = (array) $object;
|
||||
$this->magicSet($properties);
|
||||
$this->setObject($object);
|
||||
}
|
||||
if (isset($effects)) {
|
||||
$this->setEffects($effects);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string in the form with_underscores to withUnderscores
|
||||
* @param string $str
|
||||
* @return string
|
||||
*/
|
||||
protected function _toCamelCase($str) {
|
||||
$parts = explode("_", $str);
|
||||
$out = array_shift($parts);
|
||||
foreach ($parts as $part) {
|
||||
$out .= ucfirst($part);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an array of k/v pairs calls set$key
|
||||
* @param array $properties
|
||||
*/
|
||||
protected function magicSet($properties) {
|
||||
$methods = get_class_methods($this);
|
||||
foreach ($properties as $key => $value) {
|
||||
$propName = "set_{$key}";
|
||||
$function = $this->_toCamelCase($propName);
|
||||
if (in_array($function, $methods)) {
|
||||
$this->$function($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the rendered output.
|
||||
*
|
||||
* @param boolean $reset
|
||||
* Optional; if TRUE, the rebuild the output.
|
||||
* @return
|
||||
* A fully themed snippet of HTML for output.
|
||||
*/
|
||||
public function display($reset = FALSE) {
|
||||
return $this->render($reset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the output for display.
|
||||
*
|
||||
* @param boolean $reset
|
||||
* Optional; if TRUE, the rebuild the output.
|
||||
* @return
|
||||
* A fully themed snippet of HTML for output.
|
||||
*/
|
||||
public function render($reset = FALSE) {
|
||||
// If we need to reset, then set the output to NULL.
|
||||
if ($reset) {
|
||||
$this->setOutput(NULL);
|
||||
}
|
||||
|
||||
// Have we already rendered the output?
|
||||
$output = $this->getOutput();
|
||||
|
||||
// We need to render the output the first time around, or if reset.
|
||||
if (!isset($output)) {
|
||||
// First, we get the array of callable class methods for this object.
|
||||
// These may each be called by effects called for by the style.
|
||||
// @TODO: Should we have a proper array of allowable effects, rather
|
||||
// than our current lazy method of allowing all class functions?
|
||||
$methods = get_class_methods($this);
|
||||
|
||||
// Loop through and apply each effect.
|
||||
foreach ($this->getEffects() as $effect) {
|
||||
// What is the effect?
|
||||
$effectName = $effect['name'];
|
||||
if ($effectName && in_array($effectName, $methods)) {
|
||||
// Apply the effect with its settings.
|
||||
$this->$effectName($effect['settings']);
|
||||
}
|
||||
else {
|
||||
// Ouch. We have an invalid effect. Flag for bug reporting.
|
||||
$variables = $this->getVariables();
|
||||
$styleName = $variables['style']['name'];
|
||||
watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $styleName, '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
// Now the output will have been fully built.
|
||||
$output = $this->getOutput();
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public function getClassName() {
|
||||
return get_called_class();
|
||||
}
|
||||
|
||||
public function _get($variable) {
|
||||
if (function_exists('get_' . $variable)) {
|
||||
return $this->{'get_' . $variable}();
|
||||
}
|
||||
else {
|
||||
return $this->get($variable);
|
||||
}
|
||||
}
|
||||
public function _set($variable, $value) {
|
||||
if (function_exists('set_' . $variable)) {
|
||||
return $this->{'set_' . $variable}($value);
|
||||
}
|
||||
else {
|
||||
return $this->set($variable, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function arrayPush($variable, $element) {
|
||||
$array = (array) $this->_get($variable);
|
||||
array_push($array, $element);
|
||||
return $this->_set($variable, $array);
|
||||
}
|
||||
public function arrayPop($variable) {
|
||||
$array = (array) $this->_get($variable);
|
||||
array_pop($array);
|
||||
return $this->_set($variable, $array);
|
||||
}
|
||||
public function arrayUnshift($variable, $element) {
|
||||
$array = (array) $this->_get($variable);
|
||||
array_unshift($array, $element);
|
||||
return $this->_set($variable, $array);
|
||||
}
|
||||
public function arrayShift($variable) {
|
||||
$array = (array) $this->_get($variable);
|
||||
array_shift($array);
|
||||
return $this->_set($variable, $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an effect to the end of the array.
|
||||
*
|
||||
* An effect is an array with at least a 'name' key, which corresponds to the
|
||||
* class function to be called during the rendering process. It will usually
|
||||
* also contain an array of 'effects' to apply.
|
||||
*/
|
||||
public function addEffect($effect) {
|
||||
return $this->pushEffect($effect);
|
||||
}
|
||||
public function pushEffect($effect) {
|
||||
$effectName = $effect['name'];
|
||||
if (method_exists($this, $effectName)) {
|
||||
$effects = $this->getEffects();
|
||||
array_push($effects, $effect);
|
||||
return $this->setEffects($effects);
|
||||
}
|
||||
else {
|
||||
$variables = $this->getVariables();
|
||||
$styleName = $variables['style']['label'];
|
||||
watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $styleName, '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
|
||||
}
|
||||
}
|
||||
public function popEffect() {
|
||||
$effects = $this->getEffects();
|
||||
$effect = array_pop($effects);
|
||||
$this->setEffects($effects);
|
||||
return $effect;
|
||||
}
|
||||
public function unshiftEffect($effect) {
|
||||
$effectName = $effect['name'];
|
||||
if (method_exists($this, $effectName)) {
|
||||
$effects = $this->getEffects();
|
||||
array_unshift($effects, $effect);
|
||||
return $this->setEffects($effects);
|
||||
}
|
||||
else {
|
||||
$variables = $this->getVariables();
|
||||
$styleName = $variables['style']['label'];
|
||||
watchdog('styles', 'Effect %effect_name not found for %style_name display formatter style of the %class_name class.', array('%effect_name' => $effectName, '%style_name' => $this->getName(), '%class_name' => $this->getClassName()), WATCHDOG_WARNING);
|
||||
}
|
||||
}
|
||||
public function shiftEffect() {
|
||||
$effects = $this->getEffects();
|
||||
$effect = array_shift($effects);
|
||||
$this->setEffects($effects);
|
||||
return $effect;
|
||||
}
|
||||
|
||||
public function getObject() {
|
||||
return $this->get('object');
|
||||
}
|
||||
public function setObject($value) {
|
||||
return $this->set('object', $value);
|
||||
}
|
||||
|
||||
public function getVariables() {
|
||||
return $this->get('variables');
|
||||
}
|
||||
public function setVariables($value) {
|
||||
return $this->set('variables', $value);
|
||||
}
|
||||
|
||||
public function getEffects() {
|
||||
return $this->get('effects');
|
||||
}
|
||||
public function setEffects($value) {
|
||||
return $this->set('effects', $value);
|
||||
}
|
||||
|
||||
public function getWrapperType() {
|
||||
return $this->get('wrapperType');
|
||||
}
|
||||
public function setWrapperType($value) {
|
||||
return $this->set('wrapperType', $value);
|
||||
}
|
||||
public function getClasses() {
|
||||
$classes = $this->get('classes');
|
||||
return is_array($classes) ? implode(' ', $classes) : $classes;
|
||||
}
|
||||
public function setClasses($value) {
|
||||
return $this->set('classes', $value);
|
||||
}
|
||||
public function getId() {
|
||||
return $this->get('id');
|
||||
}
|
||||
public function setId($value) {
|
||||
return $this->set('id', $value);
|
||||
}
|
||||
public function getOutput() {
|
||||
return $this->get('output');
|
||||
}
|
||||
public function setOutput($value) {
|
||||
return $this->set('output', $value);
|
||||
}
|
||||
public function getPrefix() {
|
||||
$prefix = $this->get('prefix');
|
||||
if (!isset($prefix)) {
|
||||
$wrapperType = $this->getWrapperType();
|
||||
$id = $this->getId();
|
||||
$_id = $this->_id++;
|
||||
$classes = $this->getClasses();
|
||||
$prefix = "<$wrapperType id=\"styles-$id-$_id\" class=\"$classes\">";
|
||||
}
|
||||
|
||||
return $prefix;
|
||||
}
|
||||
public function setPrefix($value) {
|
||||
return $this->set('prefix', $value);
|
||||
}
|
||||
public function getSuffix() {
|
||||
$suffix = $this->get('suffix');
|
||||
if (!isset($suffix)) {
|
||||
$wrapperType = $this->getWrapperType();
|
||||
$suffix = "</$wrapperType>";
|
||||
}
|
||||
|
||||
return $suffix;
|
||||
}
|
||||
public function setSuffix($value) {
|
||||
return $this->set('suffix', $value);
|
||||
}
|
||||
|
||||
public function get($variable) {
|
||||
return isset($this->{$variable}) ? $this->{$variable} : NULL;
|
||||
}
|
||||
public function set($variable, $value) {
|
||||
return $this->{$variable} = $value;
|
||||
}
|
||||
}
|
58
sites/all/modules/styles/includes/php/get_called_class.inc
Normal file
58
sites/all/modules/styles/includes/php/get_called_class.inc
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Add support for get_called_class() to < PHP 5.3.
|
||||
*
|
||||
* @see http://www.php.net/manual/en/function.get-called-class.php#93799
|
||||
*/
|
||||
|
||||
/********************************
|
||||
* Retro-support of get_called_class()
|
||||
* Tested and works in PHP 5.2.4
|
||||
* http://www.sol1.com.au/
|
||||
********************************/
|
||||
if(!function_exists('get_called_class')) {
|
||||
function get_called_class($bt = false,$l = 1) {
|
||||
if (!$bt) $bt = debug_backtrace();
|
||||
if (!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too deep.");
|
||||
if (!isset($bt[$l]['type'])) {
|
||||
throw new Exception ('type not set');
|
||||
}
|
||||
else switch ($bt[$l]['type']) {
|
||||
case '::':
|
||||
$lines = file($bt[$l]['file']);
|
||||
$i = 0;
|
||||
$callerLine = '';
|
||||
do {
|
||||
$i++;
|
||||
$callerLine = $lines[$bt[$l]['line']-$i] . $callerLine;
|
||||
} while (stripos($callerLine,$bt[$l]['function']) === false);
|
||||
preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l]['function'].'/',
|
||||
$callerLine,
|
||||
$matches);
|
||||
if (!isset($matches[1])) {
|
||||
// must be an edge case.
|
||||
throw new Exception ("Could not find caller class: originating method call is obscured.");
|
||||
}
|
||||
switch ($matches[1]) {
|
||||
case 'self':
|
||||
case 'parent':
|
||||
return get_called_class($bt,$l+1);
|
||||
default:
|
||||
return $matches[1];
|
||||
}
|
||||
// won't get here.
|
||||
case '->': switch ($bt[$l]['function']) {
|
||||
case '__get':
|
||||
// edge case -> get class of calling object
|
||||
if (!is_object($bt[$l]['object'])) throw new Exception ("Edge case fail. __get called on non object.");
|
||||
return get_class($bt[$l]['object']);
|
||||
default: return $bt[$l]['class'];
|
||||
}
|
||||
|
||||
default: throw new Exception ("Unknown backtrace method type");
|
||||
}
|
||||
}
|
||||
}
|
62
sites/all/modules/styles/includes/styles.api.php
Normal file
62
sites/all/modules/styles/includes/styles.api.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks available for modules to implement Styles functionality.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Define information about style containers provided by a module.
|
||||
*
|
||||
* This hook enables modules to define style containers provided by this module.
|
||||
*
|
||||
* @return
|
||||
* An array of available style containers.Each container is defined as an
|
||||
* array keyed by the field type, each containing an associative array keyed
|
||||
* on a machine-readable style container name, with the following items:
|
||||
* - "label": The human-readable name of the effect.
|
||||
* - "data": An array of data that each container might require.
|
||||
* - "preview theme": (optional) A theme function to call when previewing
|
||||
* a style during administration.
|
||||
* - "help": (optional) A brief description of the style container that will
|
||||
* be displayed to the administrator when configuring styles.
|
||||
*/
|
||||
function hook_styles_containers() {
|
||||
return array(
|
||||
'media' => array(
|
||||
'image' => array(
|
||||
'label' => t('Image Styles'),
|
||||
'data' => array(
|
||||
'streams' => array(
|
||||
'public://',
|
||||
'private://',
|
||||
),
|
||||
'mimetypes' => array(
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
),
|
||||
),
|
||||
'preview theme' => 'media_styles_image_style_preview',
|
||||
'help' => t('Image Styles will transform images to your choosing, such as by scaling and cropping. You can !manage.', array('!manage' => l(t('manage your image styles here'), 'admin/config/image/image-styles'))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function hook_styles_styles() {
|
||||
$styles = array();
|
||||
foreach (image_styles() as $style_name => $style) {
|
||||
$styles[$style_name] = $style;
|
||||
}
|
||||
return array(
|
||||
'media' => array(
|
||||
'image' => $styles,
|
||||
),
|
||||
);
|
||||
}
|
152
sites/all/modules/styles/includes/styles.variables.inc
Normal file
152
sites/all/modules/styles/includes/styles.variables.inc
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file styles.variables.inc
|
||||
* Variable defaults for Styles.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Define our constants.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is the variable namespace, automatically prepended to module variables.
|
||||
*/
|
||||
define('STYLES_NAMESPACE', 'styles__');
|
||||
|
||||
/**
|
||||
* Styles constant for user styles in the database.
|
||||
*/
|
||||
define('STYLES_STORAGE_NORMAL', 1);
|
||||
|
||||
/**
|
||||
* Styles constant for user styles that override module-defined styles.
|
||||
*/
|
||||
define('STYLES_STORAGE_OVERRIDE', 2);
|
||||
|
||||
/**
|
||||
* Styles constant for module-defined styles in code.
|
||||
*/
|
||||
define('STYLES_STORAGE_DEFAULT', 4);
|
||||
|
||||
/**
|
||||
* Styles constant to represent an editable preset.
|
||||
*/
|
||||
define('STYLES_STORAGE_EDITABLE', STYLES_STORAGE_NORMAL | STYLES_STORAGE_OVERRIDE);
|
||||
|
||||
/**
|
||||
* Styles constant to represent any module-based preset.
|
||||
*/
|
||||
define('STYLES_STORAGE_MODULE', STYLES_STORAGE_OVERRIDE | STYLES_STORAGE_DEFAULT);
|
||||
|
||||
/**
|
||||
* Wrapper for variable_get() using the Styles variable registry.
|
||||
*
|
||||
* @param string $name
|
||||
* The variable name to retrieve. Note that it will be namespaced by
|
||||
* pre-pending STYLES_NAMESPACE, as to avoid variable collisions
|
||||
* with other modules.
|
||||
* @param unknown $default
|
||||
* An optional default variable to return if the variable hasn't been set
|
||||
* yet. Note that within this module, all variables should already be set
|
||||
* in the styles_variable_default() function.
|
||||
* @return unknown
|
||||
* Returns the stored variable or its default.
|
||||
*
|
||||
* @see styles_variable_set()
|
||||
* @see styles_variable_del()
|
||||
* @see styles_variable_default()
|
||||
*/
|
||||
function styles_variable_get($name, $default = NULL) {
|
||||
// Allow for an override of the default.
|
||||
// Useful when a variable is required (like $path), but namespacing is still
|
||||
// desired.
|
||||
if (!isset($default)) {
|
||||
$default = styles_variable_default($name);
|
||||
}
|
||||
// Namespace all variables.
|
||||
$variable_name = STYLES_NAMESPACE . $name;
|
||||
return variable_get($variable_name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for variable_set() using the Styles variable registry.
|
||||
*
|
||||
* @param string $name
|
||||
* The variable name to set. Note that it will be namespaced by
|
||||
* pre-pending STYLES_NAMESPACE, as to avoid variable collisions with
|
||||
* other modules.
|
||||
* @param unknown $value
|
||||
* The value for which to set the variable.
|
||||
* @return unknown
|
||||
* Returns the stored variable after setting.
|
||||
*
|
||||
* @see styles_variable_get()
|
||||
* @see styles_variable_del()
|
||||
* @see styles_variable_default()
|
||||
*/
|
||||
function styles_variable_set($name, $value) {
|
||||
$variable_name = STYLES_NAMESPACE . $name;
|
||||
return variable_set($variable_name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for variable_del() using the Styles variable registry.
|
||||
*
|
||||
* @param string $name
|
||||
* The variable name to delete. Note that it will be namespaced by
|
||||
* pre-pending STYLES_NAMESPACE, as to avoid variable collisions with
|
||||
* other modules.
|
||||
*
|
||||
* @see styles_variable_get()
|
||||
* @see styles_variable_set()
|
||||
* @see styles_variable_default()
|
||||
*/
|
||||
function styles_variable_del($name) {
|
||||
$variable_name = STYLES_NAMESPACE . $name;
|
||||
variable_del($variable_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The default variables within the Styles namespace.
|
||||
*
|
||||
* @param string $name
|
||||
* Optional variable name to retrieve the default. Note that it has not yet
|
||||
* been pre-pended with the STYLES_NAMESPACE namespace at this time.
|
||||
* @return unknown
|
||||
* The default value of this variable, if it's been set, or NULL, unless
|
||||
* $name is NULL, in which case we return an array of all default values.
|
||||
*
|
||||
* @see styles_variable_get()
|
||||
* @see styles_variable_set()
|
||||
* @see styles_variable_del()
|
||||
*/
|
||||
function styles_variable_default($name = NULL) {
|
||||
static $defaults;
|
||||
|
||||
if (!isset($defaults)) {
|
||||
$defaults = array(
|
||||
);
|
||||
}
|
||||
|
||||
if (!isset($name)) {
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
if (isset($defaults[$name])) {
|
||||
return $defaults[$name];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fully namespace variable name.
|
||||
*
|
||||
* @param string $name
|
||||
* The variable name to retrieve the namespaced name.
|
||||
* @return string
|
||||
* The fully namespace variable name, prepended with
|
||||
* STYLES_NAMESPACE.
|
||||
*/
|
||||
function styles_variable_name($name) {
|
||||
return STYLES_NAMESPACE . $name;
|
||||
}
|
Reference in New Issue
Block a user