Commit bc29d62d authored by JacobSingh's avatar JacobSingh

Updated code to work with new entity module (sadly a required mod now). Also...

Updated code to work with new entity module (sadly a required mod now). Also got type filter working (required jQuery 1.4)
parent ddf9d8d0
This diff is collapsed.
<?php
// $Id$
/**
* @file Extendable Object Faces API. Provided by the faces module.
*/
if (!interface_exists('FacesExtendableInterface', FALSE)) {
/**
* Interface for extendable objects.
*/
interface FacesExtendableInterface {
/**
* Extend the object by a class to implement the given interfaces.
*
* @param $interface
* The interface name or an array of interface names.
* @param $class
* The extender class, which has to implement the FacesExtenderInterface.
* @param $include
* An optional array describing the file to include before invoking the
* class. The array entries known are 'type', 'module', and 'name'
* matching the parameters of module_load_include(). Only 'module' is
* required as 'type' defaults to 'inc' and 'name' to NULL.
*/
public function extendByClass($interface, $class, array $include = array());
/**
* Extend the object by the given functions to implement the given
* interface. There has to be an implementation function for each method of
* the interface.
*
* @param $interface
* The interface name.
* @param $methods
* An array, where the keys are methods of the given interface and the
* values the callback functions to use.
* @param $includes
* An optional array to describe files to include before invoking the
* callbacks. You may pass a single array describing one include for all
* callbacks or an array of arrays, keyed by the method names. Look at the
* extendByClass() $include parameter for more details about how to
* describe a single file.
*/
public function extend($interface, array $methods = array(), array $includes = array());
/**
* Override the implementation of an extended method.
*
* @param $methods
* An array of methods of the interface, that should be overriden, where
* the keys are methods to override and the values the callback functions
* to use.
* @param $includes
* An optional array to describe files to include before invoking the
* callbacks. You may pass a single array describing one include for all
* callbacks or an array of arrays, keyed by the method names. Look at the
* extendByClass() $include parameter for more details about how to
* describe a single file.
*/
public function override(array $methods = array(), array $includes = array());
/**
* Returns whether the object can face as the given interface, thus it returns
* TRUE if this oject has been extended by an appropriate implementation.
*
* @param $interface
* Optional. A interface to test for. If it's omitted, all interfaces that
* the object can be faced as are returned.
* @return
* Whether the object can face as the interface or an array of interface names.
*/
public function facesAs($interface = NULL);
}
/**
* Interface for extenders.
*/
interface FacesExtenderInterface {
/**
* Returns an array of interface names the extender implements dynamically.
*/
function implementsFaces();
}
/**
* The Exception thrown by the FacesExtendable.
*/
class FacesExtendableException extends ErrorException {}
}
if (!class_exists('FacesRoot', FALSE)) {
/**
* Provides a common ancestor that makes it possible for extenders to access
* protected object properties and methods of the extendable object.
*/
abstract class FacesRoot {
/**
* Returns any property.
*/
protected function &property($name) {
return $this->$name;
}
/**
* Invokes any method.
*
* This also allows to pass arguments by reference, so it may be used to
* pass arguments by reference to dynamically extended methods.
*
* @param $name
* The method name.
* @param $arguments
* An array of arguments to pass to the method.
*/
public function call($name, array $args = array()) {
if (method_exists($this, $name)) {
return call_user_func_array(array($this, $name), $args);
}
return $this->__call($name, $args);
}
}
}
if (!class_exists('FacesExtender', FALSE)) {
/**
* A common base class for FacesExtenders. Extenders may access protected
* methods and properties of the extendable using the property() and call()
* methods.
*/
abstract class FacesExtender extends FacesRoot implements FacesExtenderInterface {}
}
if (!class_exists('FacesExtendable', FALSE)) {
/**
* Class providing an implementation of FacesExtendableInterface.
*/
abstract class FacesExtendable extends FacesRoot implements FacesExtendableInterface {
protected $facesMethods = array();
protected $faces = array();
protected $facesIncludes = array();
static protected $facesIncluded = array();
/**
* Wraps calls to module_load_include() to prevent multiple inclusions.
*
* @see module_load_include()
*/
protected static function load_include($args) {
$args += array('type' => 'inc', 'module' => '', 'name' => NULL);
$key = implode(':', $args);
if (!isset(self::$facesIncluded[$key])) {
self::$facesIncluded[$key] = TRUE;
module_load_include($args['type'], $args['module'], $args['name']);
}
}
/**
* Magic method: Invoke the dynamically implemented methods.
*/
function __call($name, $arguments = array()) {
if (isset($this->facesMethods[$name])) {
$method = $this->facesMethods[$name];
// Include code, if necessary.
if (isset($this->facesIncludes[$name])) {
self::load_include($this->facesIncludes[$name]);
$this->facesIncludes[$name] = NULL;
}
// We always pass the object reference and the name of the invoked method.
array_push($arguments, $this);
array_push($arguments, $name);
// Invoke the callback or extender class.
$callback = isset($method[0]) ? $method[0] : array($method[1], $name);
return call_user_func_array($callback, $arguments);
}
$class = check_plain(get_class($this));
throw new FacesExtendableException("There is no method $name for this instance of the class $class.");
}
/**
* Implements FacesExtendableInterface.
*/
public function facesAs($interface = NULL) {
if (!isset($interface)) {
return array_values($this->faces);
}
return in_array($interface, $this->faces) || $this instanceof $interface;
}
/**
* Implements FacesExtendableInterface.
*/
public function extendByClass($interface, $className, array $includes = array()) {
if (!in_array('FacesExtenderInterface', class_implements($className))) {
throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the FacesExtenderInterface.");
}
$faces = call_user_func(array($className, 'implementsFaces'));
$interfaces = is_array($interface) ? $interface : array($interface);
foreach ($interfaces as $interface) {
if (!in_array($interface, $faces)) {
throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the interface " . check_plain($interface) . ".");
}
$this->faces[$interface] = $interface;
$this->faces += class_implements($interface);
$face_methods = get_class_methods($interface);
$this->addIncludes($face_methods, $includes);
foreach ($face_methods as $method) {
$this->facesMethods[$method] = array(1 => $className);
}
}
}
/**
* Implements FacesExtendableInterface.
*/
public function extend($interface, array $callbacks = array(), array $includes = array()) {
$face_methods = get_class_methods($interface);
if (array_diff($face_methods, array_keys($callbacks))) {
throw new FacesExtendableException("Missing methods for implementing the interface " . check_plain($interface) . ".");
}
$this->faces[$interface] = $interface;
$this->faces += class_implements($interface);
$this->addIncludes($face_methods, $includes);
foreach ($face_methods as $method) {
$this->facesMethods[$method] = array(0 => $callbacks[$method]);
}
}
/**
* Implements FacesExtendableInterface.
*/
public function override(array $callbacks = array(), array $includes = array()) {
if (array_diff_key($callbacks, $this->facesMethods)) {
throw new FacesExtendableException("A not implemented method is to be overridden.");
}
$this->addIncludes(array_keys($callbacks), $includes);
foreach ($callbacks as $method => $callback) {
$this->facesMethods[$method] = array(0 => $callback);
}
}
/**
* Adds in include files for the given methods while removing any old files.
* If a single include file is described, it's added for all methods.
*/
protected function addIncludes($methods, $includes) {
$includes = isset($includes['module']) && is_string($includes['module']) ? array_fill_keys($methods, $includes) : $includes;
$this->facesIncludes = $includes + array_diff_key($this->facesIncludes, array_flip($methods));
}
/**
* Only serialize what is really necessary.
*/
public function __sleep() {
return array('facesMethods', 'faces', 'facesIncludes');
}
}
}
\ No newline at end of file
......@@ -13,61 +13,4 @@ function namespace() {
}
}
return o;
};
/**
* Stolen from jQuery 1.4
*/
jQuery.recursiveParam = function ( a ) {
var s = [],
param_traditional = jQuery.param.traditional;
function add( key, value ){
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
// If an array was passed in, assume that it is an array
// of form elements
if ( jQuery.isArray(a) || a.jquery )
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
});
else
// Encode parameters from object, recursively. If
// jQuery.param.traditional is set, encode the "old" way
// (the way 1.3.2 or older did it)
jQuery.each( a, function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) )
jQuery.each( obj, function(i,v){
// Due to rails' limited request param syntax, numeric array
// indices are not supported. To avoid serialization ambiguity
// issues, serialized arrays can only contain scalar values. php
// does not have this issue, but we should go with the lowest
// common denominator
add( prefix + ( param_traditional ? "" : "[]" ), v );
});
else if ( typeof obj == "object" )
if ( param_traditional )
add( prefix, obj );
else
jQuery.each( obj, function(k,v){
buildParams( prefix ? prefix + "[" + k + "]" : k, v );
});
else
add( prefix, obj );
});
// Return the resulting serialization
return s.join("&").replace('%20', "+");
}
\ No newline at end of file
};
\ No newline at end of file
......@@ -29,7 +29,7 @@ Drupal.media.popups.mediaBrowser = function(onSelect, options) {
var browserSrc = options.src;
// Params to send along to the iframe. Experimental.
debug.debug(options.params);
browserSrc += '&' + $.recursiveParam({params:options.params});
browserSrc += '&' + $.param({params:options.params});
//debug.debug(browserSrc);
var mediaIframe = Drupal.media.popups.getPopupIframe(browserSrc, 'mediaBrowser');
// Attach the onLoad event
......@@ -75,7 +75,7 @@ Drupal.media.popups.mediaBrowser = function(onSelect, options) {
Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad = function (e) {
var options = e.data;
if (!this.contentWindow || !this.contentWindow.Drupal.media.browser) {
if (!this.contentWindow || this.contentWindow.Drupal.media.browser == undefined) {
return;
}
......
......@@ -9,9 +9,8 @@ Drupal.behaviors.mediaLibrary = {
$('#media-browser-tabset').bind('tabsselect', function(event, ui) {
if (ui.tab.hash === '#media-tab-library') {
// @todo: implement the types param here.
var params = {
};
var params = {};
params.types = Drupal.settings.media.browser.library.types
//$(ui.panel).addClass('throbber');
library.reset($(ui.panel));
......@@ -73,12 +72,19 @@ Drupal.media.browser.library.prototype.loadMedia = function() {
that.render(that.renderElement);
}
jQuery.get(
this.settings.getMediaUrl,
this.params,
gotMedia,
'json'
);
var errorCallback = function() {
alert('Error getting media.');
clearTimeout(that.updaterTimeout);
}
jQuery.ajax({
url: this.settings.getMediaUrl,
type: 'GET',
dataType: 'json',
data: this.params,
error: errorCallback,
success: gotMedia
});
}
Drupal.media.browser.library.prototype.scrollUpdater = function(){
......
......@@ -13,17 +13,21 @@ function media_browser_experimental($selected = NULL) {
$output = array();
$output['#attached']['library'][] = array('media', 'media_browser_page_experimental');
$params = drupal_get_query_parameters();
$get = drupal_get_query_parameters();
array_walk_recursive($get, '_media_recursive_check_plain');
// We don't want everything, just this array.
$params = isset($get['params']) ? $get['params'] : array();
// How do we validate these? I don't know.
// I think PDO should protect them, but I'm not 100% certain.
array_walk_recursive($params, '_media_recursive_check_plain');
if (isset($params['fid'])) {
if (isset($get['fid'])) {
// A form has submitted, selected a piece (or pieces) of media and we refreshed.
// So we're just going to send the media entity out as a JS setting so the browser can close itself.
if (!is_array($params['fid'])) {
$selected = array($params['fid']);
if (!is_array($get['fid'])) {
$selected = array($get['fid']);
}
$selectedMedia = entity_load('media', $selected);
$setting = array('media' => array('selectedMedia' => array_values($selectedMedia)));
......@@ -137,11 +141,7 @@ function media_media_browser_new_plugins($params) {
$plugins = array();
$path = drupal_get_path('module', 'media');
if (isset($params['types'])) {
// This could be use to add validators to the upload form
$types = $params['types'];
}
$types = isset($params['types']) ? $params['types'] : array();
include_once($path . '/media.admin.inc');
include_once($path . '/media.pages.inc');
......@@ -182,6 +182,7 @@ function media_media_browser_new_plugins($params) {
'#settings' => array(
'viewMode' => 'thumbnails',
'getMediaUrl' => url('media/browser/experimental/list'),
'types' => $types,
// We should probably change this to load dynamically when requested
// via the JS file.
),
......
......@@ -17,3 +17,4 @@ files[] = test/media.entity.test
dependencies[] = file
dependencies[] = image
dependencies[] = file_styles
dependencies[] = entity
......@@ -10,26 +10,6 @@
*
*/
/**
* Register the media_autoload() function to load relevant classes on demand.
*/
spl_autoload_register('media_autoload');
/**
* Autoload API includes. Note that the code registry autoload is used only
* by the providing API module.
*/
function media_autoload($class) {
if (stripos($class, 'faces') === 0) {
module_load_include('inc', 'media', 'includes/faces');
}
if (stripos($class, 'entity') === 0) {
module_load_include('inc', 'media', 'includes/entity');
}
}
/* ***************************************** */
/* INCLUDES */
/* ***************************************** */
......
......@@ -372,15 +372,16 @@ class MediaEntityController extends EntityAPIController {
// @todo: refactor this.
return parent::load($ids, $normal_conditions);
}
public function buildQuery() {
parent::buildQuery();
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions = array(), $revision_id = FALSE);
if ($this->limit) {
$this->query->range($this->start, $this->limit);
$query->range($this->start, $this->limit);
}
foreach ($this->customConditions as $condition) {
$this->query->condition($condition[0], $condition[1], $condition[2]);
$query->condition($condition[0], $condition[1], $condition[2]);
}
return $query;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment