View source
<?php
interface BeanTypePluginInterface {
public function __construct($plugin_info);
public function getInfo($key = NULL);
public function buildURL();
public function getLabel();
public function getDescription();
public function setBean($bean);
public function isEditable();
public function values();
public function form($bean, $form, &$form_state);
public function validate($values, &$form_state);
public function submit(Bean $bean);
public function view($bean, $content, $view_mode = 'default', $langcode = NULL);
}
class Bean extends Entity {
public $label;
public $description;
public $title;
public $type;
public $data;
public $delta;
public $view_mode;
public $bid;
public $vid;
public $created;
public $changed;
public $log;
public $uid;
public $default_revision;
public $revisions = array();
protected $plugin;
public function getInfo($key = NULL) {
if ($this->plugin instanceof BeanTypePluginInterface) {
return $this->plugin
->getInfo($key);
}
return NULL;
}
public function __construct(array $values = array()) {
if (isset($values['is_new'])) {
$this->type = isset($values['type']) ? $values['type'] : '';
}
parent::__construct($values, 'bean');
}
public function adminTitle() {
return filter_xss_admin($this->label);
}
protected function setUp() {
parent::setUp();
if (!empty($this->type)) {
$plugin = bean_load_plugin_class($this->type);
$this
->loadUp($plugin);
}
return $this;
}
public function loadUp($plugin) {
if (empty($this->plugin)) {
if (!$plugin instanceof BeanTypePluginInterface) {
$plugin = bean_load_plugin_class('bean_default');
}
$this
->setPlugin($plugin)
->setFields();
}
return $this;
}
protected function setPlugin($plugin) {
$this->plugin = $plugin;
return $this;
}
protected function setFields() {
if (!empty($this->plugin) && !empty($this->type)) {
$values = is_array($this->data) ? $this->data : unserialize($this->data);
foreach ($this->plugin
->values() as $field => $default) {
$this->{$field} = isset($values[$field]) ? $values[$field] : $default;
}
}
return $this;
}
protected function defaultUri() {
return array(
'path' => 'block/' . strtolower($this
->identifier()),
);
}
protected function defaultLabel() {
if (empty($this->title)) {
return $this->label;
}
return $this->title;
}
public function setDefault() {
$this->default_revision = TRUE;
return $this;
}
public function getForm($form, &$form_state) {
return $this->plugin
->form($this, $form, $form_state);
}
public function validate($values, &$form_state) {
$this->plugin
->validate($values, $form_state);
}
public function url() {
$uri = $this
->defaultUri();
return $uri['path'];
}
protected function buildURL($type) {
$url = $this
->url();
return $url . '/' . $type;
}
public function viewURL() {
return $this
->buildURL('view');
}
public function editURL() {
return $this
->buildURL('edit');
}
public function deleteURL() {
return $this
->buildURL('delete');
}
public function typeName() {
return $this
->getInfo('label');
}
public function setValues($values) {
$this->data = array();
foreach ($this->plugin
->values() as $field => $value) {
$this->data[$field] = $values[$field];
}
return $this;
}
public function view($view_mode = 'default', $langcode = NULL, $page = NULL) {
$content = parent::view($view_mode, $langcode);
if (empty($this->plugin)) {
return $content;
}
return $this->plugin
->view($this, $content, $view_mode, $langcode);
}
public function save() {
$this
->setUid()
->checkDelta();
if (empty($this->created)) {
$this->created = REQUEST_TIME;
}
$this->changed = REQUEST_TIME;
$this->plugin
->submit($this);
$return = parent::save();
bean_reset(TRUE, TRUE);
if (module_exists('block')) {
bean_cache_clear_block($this);
}
return $return;
}
protected function setUid() {
global $user;
$this->uid = empty($this->uid) ? $user->uid : $this->uid;
return $this;
}
protected function checkDelta() {
if (empty($this->delta)) {
$max_length = 32;
if (module_exists('transliteration')) {
$delta = drupal_clean_css_identifier(strtolower(transliteration_get($this->label)));
}
else {
$delta = drupal_clean_css_identifier(strtolower($this->label));
}
if (is_numeric($delta)) {
$delta = 'bean-' . $delta;
}
$this->delta = substr($delta, 0, $max_length);
if ($this
->deltaExists($this->delta)) {
$i = 0;
$separator = '-';
$original_delta = $this->delta;
do {
$unique_suffix = $separator . $i++;
$this->delta = substr($original_delta, 0, $max_length - drupal_strlen($unique_suffix)) . $unique_suffix;
} while ($this
->deltaExists($this->delta));
}
}
return $this;
}
protected function deltaExists($delta) {
return db_select('bean', 'b')
->fields('b', array(
'bid',
))
->condition('delta', $delta)
->execute()
->fetchField();
}
public function loadRevisions() {
$this->revisions = db_select('bean_revision', 'br')
->condition('delta', $this
->identifier())
->fields('br')
->execute();
return $this;
}
}
class BeanEntityAPIController extends EntityAPIControllerExportable {
protected function setPlugin(Bean $bean) {
static $plugins = array();
if (empty($plugins[$bean->type])) {
$plugins[$bean->type] = bean_load_plugin_class($bean->type);
$bean
->loadUp($plugins[$bean->type]);
}
}
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
foreach ($queried_entities as $entity) {
$this
->setPlugin($entity);
}
parent::attachLoad($queried_entities, $revision_id);
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$beans = $ids ? $this
->load($ids) : array();
$deltas = array();
foreach ($beans as $bean) {
if ($bean
->internalIdentifier()) {
field_attach_delete('bean', $bean);
$deltas[] = $bean
->identifier();
}
}
if (!empty($deltas)) {
try {
if (module_exists('block')) {
db_delete('block')
->condition('module', 'bean')
->condition('delta', $deltas)
->execute();
db_delete('block_role')
->condition('module', 'bean')
->condition('delta', $deltas)
->execute();
db_delete('block_node_type')
->condition('module', 'bean')
->condition('delta', $deltas)
->execute();
}
} catch (Exception $e) {
if (isset($transaction)) {
$transaction
->rollback();
}
watchdog_exception('bean', $e);
throw $e;
}
}
parent::delete($ids, $transaction);
}
public function invoke($hook, $entity) {
parent::invoke($hook, $entity);
if (module_exists('rules')) {
rules_invoke_event($this->entityType . '_' . $hook, $entity);
}
}
protected function saveRevision($entity) {
global $user;
$org_uid = $entity->uid;
$entity->uid = $user->uid;
$return = parent::saveRevision($entity);
$entity->uid = $org_uid;
return $return;
}
}
class BeanException extends Exception {
}