Index: boxes.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/boxes/boxes.admin.inc,v retrieving revision 1.5.2.1 diff -u -r1.5.2.1 boxes.admin.inc --- boxes.admin.inc 25 Apr 2010 18:41:54 -0000 1.5.2.1 +++ boxes.admin.inc 1 Jun 2010 21:44:28 -0000 @@ -38,8 +38,7 @@ * Submit handler for box_add_form. */ function boxes_add_form_submit($form, &$form_state) { - $box = boxes_create($form_state['values']['plugin_key']); - $box->set($form_state['values']); + $box = boxes_create($form_state['values']['plugin_key'], $form_state['values']); $box->save(); drupal_set_message(t('%name has been created.', array('%name' => $box->description))); $form_state['redirect'] = 'admin/build/block'; @@ -155,10 +154,8 @@ * Submit handler for the inline form. */ function boxes_box_inline_form_submit($form, $form_state) { - if (!$box = boxes_load($form_state['values']['delta'])) { - $box = boxes_create($form_state['values']['plugin_key']); - } - $box->set($form_state['values']); + $box = boxes_create($form_state['values']['plugin_key'], $form_state['values']); + if (module_exists('spaces') && $space = spaces_get_space()) { $space->controllers->boxes->set($box->delta, $box); } Index: boxes.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/boxes/boxes.install,v retrieving revision 1.2.2.3 diff -u -r1.2.2.3 boxes.install --- boxes.install 26 Apr 2010 14:21:23 -0000 1.2.2.3 +++ boxes.install 1 Jun 2010 21:44:28 -0000 @@ -150,3 +150,17 @@ } return $ret; } + +function boxes_update_6103() { + $ret = array(); + if (module_exists('spaces')) { + $result = db_query("SELECT * FROM {spaces_overrides} WHERE object_type = 'boxes'"); + while ($row = db_fetch_object($result)) { + $v = unserialize($row->value); + $row->value = (object)$v; + drupal_write_record('spaces_overrides', $row, array('type', 'id', 'object_type', 'object_id')); + } + $ret[] = array('success' => true, 'query' => 'Updated Spaces overrides'); + } + return $ret; +} Index: boxes.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/boxes/boxes.module,v retrieving revision 1.10.2.4 diff -u -r1.10.2.4 boxes.module --- boxes.module 25 May 2010 19:03:10 -0000 1.10.2.4 +++ boxes.module 1 Jun 2010 21:44:28 -0000 @@ -123,9 +123,8 @@ case 'save': $edit['delta'] = $delta; if (!$box = boxes_load($delta)) { - $box = boxes_create($edit['plugin_key']); + $box = boxes_create($edit['plugin_key'], $edit); } - $box->set($edit); $box->save(); break; @@ -146,8 +145,7 @@ $hash = dechex(crc32($user->sid . microtime())); } $delta = "{$identifier}-{$hash}"; - $box = boxes_create($plugin_key); - $box->set(array('delta' => $delta)); + $box = boxes_create($plugin_key, array('delta' => $delta)); } else { $box = boxes_load($delta); @@ -197,11 +195,18 @@ } /** - * Create a new box object. + * Instantiate box object. + * + * @param $plugin_key + * The string id of the boxes plugin. + * @param $values + * The values to set on the box. + * + * @return a box object. */ -function boxes_create($plugin_key) { +function boxes_create($plugin_key, $values = array()) { module_load_include('inc', 'boxes', 'plugins/boxes_box'); - return boxes_box::create($plugin_key); + return boxes_box::factory($plugin_key, $values); } /** @@ -453,8 +458,12 @@ function boxes_boxes_view_alter(&$box, $delta) { if (module_exists('spaces') && $space = spaces_get_space()) { if ($space_box = $space->controllers->boxes->get($delta)) { - $box = boxes_create($space_box['plugin_key']); - $box->set($space_box); + // Some older overrides may be stored as an array, check for these and + // convert them to objects. + if (is_array($space_box)) { + $space_box = (object)$space_box; + } + $box = boxes_create($space_box->plugin_key, $space_box); $box->new = FALSE; } } Index: plugins/boxes_box.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/boxes/plugins/boxes_box.inc,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 boxes_box.inc --- plugins/boxes_box.inc 14 May 2010 14:59:49 -0000 1.1.2.1 +++ plugins/boxes_box.inc 1 Jun 2010 21:44:28 -0000 @@ -8,7 +8,7 @@ * @see boxes_simple. */ abstract class boxes_box { - static $boxes; + static $boxes; // Static cache of box objects. public $delta; public $title; public $description; @@ -18,44 +18,51 @@ public $export_type; /** - * Load an existing box by its unique identifier $delta. + * Load existing box by its unique identifier $delta. */ - public static function load($delta) { - if (!isset(self::$boxes[$delta])) { + public static function load($delta, $reset = false) { + if (!isset(self::$boxes[$delta]) || $reset) { ctools_include('export'); if ($values = array_pop(ctools_export_load_object('box', 'names', array($delta)))) { - // Attempt to create an instance of the box class. - // Use 'simple' as the plugin class to handle legacy boxes. - try { - $box = self::create(isset($values->plugin_key) ? $values->plugin_key : 'simple'); - foreach ($box as $key => $value) { - if (isset($values->$key)) { - $box->$key = $values->$key; - } - } - $box->new = FALSE; // Set in __construct(). - self::$boxes[$delta] = $box; - } - catch (Exception $exception) { - return FALSE; - } + self::$boxes[$delta] = self::factory($values->plugin_key, $values); + self::$boxes[$delta]->new = false; } } return isset(self::$boxes[$delta]) ? self::$boxes[$delta] : NULL; } /** - * Create a new box. Factory for creating a box of a particular type. + * 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. + * An array with at least a plugin_key key identifying the plugin class to + * use for instantiating this box. */ - public static function create($plugin_key) { + 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; } else { @@ -72,34 +79,14 @@ } /** - * Set class variables. - * - * @param $values - * A keyed array where the keys are class variables or keys in - * $this->options declared by options_defaults(). - */ - public function set($values) { - foreach ($this as $key => $value) { - if (isset($values[$key])) { - $this->$key = $values[$key]; - } - } - foreach ($this->options_defaults() as $key => $value) { - if (isset($values[$key])) { - $this->options[$key] = $values[$key]; - } - } - // Make sure box is present in static cache as soon as delta is set. - if (!empty($this->delta)) { - self::$boxes[$this->delta] = $this; - } - } - - /** * 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_export_load_object_reset('box'); + self::$boxes = array(); } /** Index: plugins/spaces_controller_boxes.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/boxes/plugins/spaces_controller_boxes.inc,v retrieving revision 1.2 diff -u -r1.2 spaces_controller_boxes.inc --- plugins/spaces_controller_boxes.inc 30 Mar 2010 16:23:00 -0000 1.2 +++ plugins/spaces_controller_boxes.inc 1 Jun 2010 21:44:28 -0000 @@ -6,15 +6,20 @@ */ 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 = boxes_load(); + $boxes = array(); + foreach (ctools_export_load_object('box') as $box) { + $boxes[$box->delta] = (array)$box; + } $this->values['original'] = $boxes; $this->loaded_all['original'] = TRUE; } else if (!isset($this->loaded['original'][$id])) { - if ($box = boxes_load($id)) { + if ($box = array_pop(ctools_export_load_object('box', 'names', array($id)))) { $this->values['original'][$id] = $box; $this->loaded['original'][$id] = TRUE; } @@ -25,13 +30,17 @@ /** * Override of the set() method. * - * Remove flags used by to detect state and cast as array so that the box - * configuration can be loaded regardless of whether the plugin is available. + * 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 = (array)$value; + $box = new stdClass(); + foreach ($value as $k => $v) { + $box->$k = $v; + } parent::set($id, $box); } }