Commit 0656fbac authored by Penny Leach's avatar Penny Leach Committed by Nigel McNie
Browse files

front end and back end COLLIDE

parent 83ebc2ec
...@@ -39,6 +39,14 @@ class PluginBlocktypeImage extends PluginBlocktype { ...@@ -39,6 +39,14 @@ class PluginBlocktypeImage extends PluginBlocktype {
public static function get_categories() { public static function get_categories() {
return array('file'); return array('file');
} }
public static function render_instance(BlockInstance $instance) {
require_once(get_config('docroot') . 'lib/artefact.php');
$configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
$image = artefact_instance_from_id($configdata['artefactid']);
return $image->render_self($configdata); // in this case, config data contains width & height
}
} }
?> ?>
...@@ -861,6 +861,13 @@ class ArtefactTypeImage extends ArtefactTypeFile { ...@@ -861,6 +861,13 @@ class ArtefactTypeImage extends ArtefactTypeFile {
parent::delete(); parent::delete();
} }
public function render_self($options) {
$src = get_config('wwwroot') . 'artefact/file/download.php?file=' . $this->id;
if (isset($options['viewid'])) {
$src .= '&view=' . $options['viewid'];
}
return '<img src="' . $src . '" />'; // more later
}
} }
?> ?>
...@@ -467,6 +467,10 @@ abstract class ArtefactType { ...@@ -467,6 +467,10 @@ abstract class ArtefactType {
*/ */
public static abstract function get_links($id); public static abstract function get_links($id);
// @TODO maybe uncomment this later and implement it everywhere
// when we know a bit more about what blocks we want.
//public abstract function render_self($options);
// ******************** HELPER FUNCTIONS ******************** // // ******************** HELPER FUNCTIONS ******************** //
protected function get_artefact_type() { protected function get_artefact_type() {
......
...@@ -51,6 +51,8 @@ abstract class PluginBlocktype extends Plugin { ...@@ -51,6 +51,8 @@ abstract class PluginBlocktype extends Plugin {
public static abstract function get_categories(); public static abstract function get_categories();
public static abstract function render_instance(BlockInstance $instance);
/** /**
* This function must be implemented in the subclass if it has config * This function must be implemented in the subclass if it has config
*/ */
...@@ -120,16 +122,68 @@ class BlockInstance { ...@@ -120,16 +122,68 @@ class BlockInstance {
private $title; private $title;
private $configdata; private $configdata;
private $dirty; private $dirty;
private $view;
private $view_obj;
private $column;
private $order;
private $canmoveleft;
private $canmoveright;
private $canmoveup;
private $canmovedown;
private $maxorderincolumn;
public function __construct($id=0, $data=null) {
if (!empty($id)) {
if (empty($data)) {
if (!$data = get_record('block_instance','id',$id)) {
throw new BlockInstanceNotFoundException(get_string('blockinstancenotfound', 'error', $id));
}
}
$this->id = $id;
}
else {
$this->dirty = true;
}
if (empty($data)) {
$data = array();
}
foreach ((array)$data as $field => $value) {
if (property_exists($this, $field)) {
$this->{$field} = $value;
}
}
}
public function get($field) { public function get($field) {
if (!property_exists($this, $field)) { if (!property_exists($this, $field)) {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this)); throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
} }
if ($field == 'configdata') {
// make sure we unserialise it
if (!is_array($this->configdata)) {
$this->configdata = unserialize($this->configdata);
}
}
if (strpos($field, 'canmove') === 0) {
return $this->can_move(substr($field, strlen('canmove'))); // needs to be calculated.
}
if ($field == 'maxorderincolumn') {
// only fetch this when we're asked, it's a db query.
if (empty($this->maxorderincolumn)) {
$this->maxorderincolumn = get_field(
'block_instance',
'max("order")',
'view', $this->view, 'column', $this->column);
}
}
return $this->{$field}; return $this->{$field};
} }
public function set($field, $value) { public function set($field, $value) {
if (property_exists($this, $field)) { if (property_exists($this, $field)) {
if ($field == 'configdata') {
throw new InvalidArgumentException(get_string('blockconfigdatacalledfromset', 'error'));
}
if ($this->{$field} != $value) { if ($this->{$field} != $value) {
// only set it to dirty if it's changed // only set it to dirty if it's changed
$this->dirty = true; $this->dirty = true;
...@@ -140,30 +194,75 @@ class BlockInstance { ...@@ -140,30 +194,75 @@ class BlockInstance {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this)); throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
} }
public function __construct($id=0, $data=null) { /**
if (!empty($id)) { * converts this instance to a stdclass object
if (empty($data)) { * that can be used in data structures
if (!$data = get_record('block_instance','id',$id)) { */
throw new BlockInstanceNotFoundException(get_string('blockinstancenotfound', 'error', $id)); public function to_stdclass() {
} $stdclass = new StdClass;
$vars = get_object_vars($this);
foreach (array_keys($vars) as $field) {
if ($field == 'dirty') {
continue;
} }
$this->id = $id; $stdclass->{$field} = $this->get($field); // do it this way for calculated ones
} }
else { $stdclass->content = $this->render();
$this->dirty = true; return $stdclass;
}
/**
* basically just calls the callback function for the blocktype
*/
public function render() {
safe_require('blocktype', $this->get('blocktype'));
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'render_instance', $this);
}
public function commit() {
if (empty($this->dirty)) {
return;
} }
if (empty($data)) { $fordb = new StdClass;
$data = array(); foreach (get_object_vars($this) as $k => $v) {
$fordb->{$k} = $v;
} }
foreach ((array)$data as $field => $value) { if (empty($this->id)) {
if (property_exists($this, $field)) { $this->id = insert_record('block_instance', $fordb, 'id', true);
$this->{$field} = $value; }
} else {
update_record('block_instance', $fordb, 'id');
} }
// @TODO maybe handle_event here.
$this->dirty = false;
} }
public function to_stdclass() { /**
return (object)get_object_vars($this); * @return View the view object this block instance is in
*/
public function get_view() {
if (empty($this->view_obj)) {
$this->view_obj = new View($this->get('view'));
}
return $this->view_obj;
}
public function can_move($direction) {
switch ($direction) {
case 'left':
return ($this->column > 1);
case 'right':
return ($this->column < $this->get_view()->get('numcolumns'));
case 'up':
return ($this->order > 1);
break;
case 'down':
return ($this->order < $this->get('maxorderincolumn'));
default:
throw new InvalidArgumentException(get_string('invaliddirection', 'error', $direction));
}
} }
} }
......
...@@ -67,6 +67,8 @@ $string['blocktypemissingconfigform'] = 'Block type %s must implement config_for ...@@ -67,6 +67,8 @@ $string['blocktypemissingconfigform'] = 'Block type %s must implement config_for
$string['blocktypemissingconfigsave'] = 'Block type %s must implement config_save'; $string['blocktypemissingconfigsave'] = 'Block type %s must implement config_save';
$string['versionphpmissing'] = 'Plugin %s %s is missing version.php!'; $string['versionphpmissing'] = 'Plugin %s %s is missing version.php!';
$string['blocktypeprovidedbyartefactnotinstallable'] = 'This will be installed as part of the installation of artefact plugin %s'; $string['blocktypeprovidedbyartefactnotinstallable'] = 'This will be installed as part of the installation of artefact plugin %s';
$string['blockconfigdatacalledfromset'] = 'Configdata should not be set directly, use PluginBlockType::instance_config_save instead';
$string['invaliddirection'] = 'Invalid direction %s';
// if you change these next two , be sure to change them in libroot/errors.php // if you change these next two , be sure to change them in libroot/errors.php
// as they are duplicated there, in the case that get_string was not available. // as they are duplicated there, in the case that get_string was not available.
......
...@@ -175,18 +175,6 @@ ...@@ -175,18 +175,6 @@
<KEY NAME="typefk" TYPE="foreign" FIELDS="type" REFTABLE="activity_type" REFFIELDS="name" /> <KEY NAME="typefk" TYPE="foreign" FIELDS="type" REFTABLE="activity_type" REFFIELDS="name" />
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="block_instance">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="blocktype" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="configdata" TYPE="text" NOTNULL="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="blocktypefk" TYPE="foreign" FIELDS="blocktype" REFTABLE="blocktype_installed" REFFIELDS="name" />
</KEYS>
</TABLE>
<TABLE NAME="artefact"> <TABLE NAME="artefact">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" /> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
...@@ -389,7 +377,7 @@ ...@@ -389,7 +377,7 @@
<FIELD NAME="mtime" TYPE="datetime" NOTNULL="true" /> <FIELD NAME="mtime" TYPE="datetime" NOTNULL="true" />
<FIELD NAME="atime" TYPE="datetime" NOTNULL="true" /> <FIELD NAME="atime" TYPE="datetime" NOTNULL="true" />
<FIELD NAME="submittedto" TYPE="int" LENGTH="10" NOTNULL="false" /> <FIELD NAME="submittedto" TYPE="int" LENGTH="10" NOTNULL="false" />
<FIELD NAME="numcolumns" TYPE="int" LENGTH="2" NOTNULL="false" /> <FIELD NAME="numcolumns" TYPE="int" LENGTH="2" NOTNULL="true" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" /> <KEY NAME="primary" TYPE="primary" FIELDS="id" />
...@@ -407,35 +395,36 @@ ...@@ -407,35 +395,36 @@
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" /> <KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="view_artefact"> <TABLE NAME="block_instance">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" /> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="blocktype" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="configdata" TYPE="text" NOTNULL="false" />
<FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" /> <FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="artefact" TYPE="int" LENGTH="10" NOTNULL="true" /> <FIELD NAME="column" TYPE="int" LENGTH="2" NOTNULL="true" />
<FIELD NAME="block" TYPE="int" LENGTH="10" NOTNULL="true" /> <FIELD NAME="order" TYPE="int" LENGTH="2" NOTNULL="true" />
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
<FIELD NAME="format" TYPE="text" NOTNULL="true" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" /> <KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="blocktypefk" TYPE="foreign" FIELDS="blocktype" REFTABLE="blocktype_installed" REFFIELDS="name" />
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" /> <KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
<KEY NAME="artefactfk" TYPE="foreign" FIELDS="artefact" REFTABLE="artefact" REFFIELDS="id" /> <KEY NAME="viewcolumnorderuk" TYPE="unique" FIELDS="view,column,order" />
<KEY NAME="blockfk" TYPE="foreign" FIELDS="block" REFTABLE="block_instance" REFFIELDS="id" />
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="view_block"> <TABLE NAME="view_artefact">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" /> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" /> <FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="artefact" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="block" TYPE="int" LENGTH="10" NOTNULL="true" /> <FIELD NAME="block" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="column" TYPE="int" LENGTH="2" NOTNULL="true" /> <FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
<FIELD NAME="order" TYPE="int" LENGTH="2" NOTNULL="true" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" /> <KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" /> <KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
<KEY NAME="artefactfk" TYPE="foreign" FIELDS="artefact" REFTABLE="artefact" REFFIELDS="id" />
<KEY NAME="blockfk" TYPE="foreign" FIELDS="block" REFTABLE="block_instance" REFFIELDS="id" /> <KEY NAME="blockfk" TYPE="foreign" FIELDS="block" REFTABLE="block_instance" REFFIELDS="id" />
<KEY NAME="columnorderuk" TYPE="unique" FIELDS="column,order" />
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="view_feedback"> <TABLE NAME="view_feedback">
......
...@@ -60,6 +60,7 @@ class View { ...@@ -60,6 +60,7 @@ class View {
} }
else { else {
$this->ctime = time(); $this->ctime = time();
$this->dirty = true;
} }
if (empty($data)) { if (empty($data)) {
...@@ -286,11 +287,10 @@ class View { ...@@ -286,11 +287,10 @@ class View {
return; return;
} }
$sql = 'SELECT bi.*, vb.id AS vbid, vb.view, vb.block, vb.column, vb.order $sql = 'SELECT bi.*
FROM {view_block} vb FROM {block_instance} bi
JOIN {block_instance} bi ON vb.block = bi.id WHERE bi.view = ?
WHERE vb.view = ? ORDER BY bi.column, bi.order';
ORDER BY vb.column, vb.order';
if (!$data = get_records_sql_array($sql, array($this->get('id')))) { if (!$data = get_records_sql_array($sql, array($this->get('id')))) {
$data = array(); $data = array();
} }
...@@ -302,7 +302,7 @@ class View { ...@@ -302,7 +302,7 @@ class View {
foreach ($data as $block) { foreach ($data as $block) {
$b = new BlockInstance($block->id, (array)$block); $b = new BlockInstance($block->id, (array)$block);
$this->columns[$block->column]['blockinstances'][] = $b->to_stdclass(); $this->columns[$block->column]['blockinstances'][] = (array)$b->to_stdclass();
} }
} }
......
...@@ -253,14 +253,14 @@ EOF; ...@@ -253,14 +253,14 @@ EOF;
/** /**
* Returns the HTML for the columns of a particular views * Returns the HTML for the columns of a particular views
* *
* @param int $view The view to build the columns for * @param View $view The view to build the columns for
* @param bool $javascript Set to true if the caller is a json script, * @param bool $javascript Set to true if the caller is a json script,
* meaning that nothing for the standard HTML version * meaning that nothing for the standard HTML version
* alone should be output * alone should be output
*/ */
function view_build_columns($view, $javascript=false) { function view_build_columns(View $view, $javascript=false) {
global $COLUMNS; global $COLUMNS;
$numcols = $COLUMNS['count']; $numcols = $view->get('numcolumns');
$result = ''; $result = '';
for ($i = 1; $i <= $numcols; $i++) { for ($i = 1; $i <= $numcols; $i++) {
...@@ -273,25 +273,25 @@ function view_build_columns($view, $javascript=false) { ...@@ -273,25 +273,25 @@ function view_build_columns($view, $javascript=false) {
/** /**
* Returns the HTML for a particular view column * Returns the HTML for a particular view column
* *
* @param int $view The view to build the column for * @param View $view The view to build the column for
* @param int $column The column to build * @param int $column The column to build
* @param bool $javascript Set to true if the caller is a json script, * @param bool $javascript Set to true if the caller is a json script,
* meaning that nothing for the standard HTML version * meaning that nothing for the standard HTML version
* alone should be output * alone should be output
*/ */
function view_build_column($view, $column, $javascript=false) { function view_build_column(View $view, $column, $javascript=false) {
global $COLUMNS; global $COLUMNS;
// FIXME: TEMPORARY. Just so if we're adding a new column, we can insert a blank one // FIXME: TEMPORARY. Just so if we're adding a new column, we can insert a blank one
if ($javascript) { if ($javascript) {
$data = array('blockinstances' => array()); $data = array('blockinstances' => array());
} }
else { else {
$data = $COLUMNS['columns'][$column]; $data = $view->get_column_datastructure($column);
} }
$result = ''; $result = '';
$result = '<div id="column_' . $column . '" class="column columns' . $COLUMNS['count'] . '"> $result = '<div id="column_' . $column . '" class="column columns' . $view->get('numcolumns') . '">
<div class="column-header">'; <div class="column-header">';
if ($column == 1) { if ($column == 1) {
......
...@@ -31,6 +31,7 @@ require(dirname(__FILE__) . '/init.php'); ...@@ -31,6 +31,7 @@ require(dirname(__FILE__) . '/init.php');
require(dirname(__FILE__) . '/viewlib.php'); require(dirname(__FILE__) . '/viewlib.php');
$view = param_integer('view'); $view = param_integer('view');
$view = new View($view);
$action = param_alphanumext('action'); $action = param_alphanumext('action');
switch ($action) { switch ($action) {
...@@ -65,7 +66,7 @@ case 'blocktype_list': ...@@ -65,7 +66,7 @@ case 'blocktype_list':
break; break;
case 'add_column': case 'add_column':
$column = param_integer('column'); $column = param_integer('column');
if (view_add_column($view, $column)) { if (view_add_column($view->id, $column)) {
json_reply(false, array('message' => false, 'data' => view_build_column($view, $column, true))); json_reply(false, array('message' => false, 'data' => view_build_column($view, $column, true)));
} }
else { else {
...@@ -73,7 +74,7 @@ case 'add_column': ...@@ -73,7 +74,7 @@ case 'add_column':
} }
case 'remove_column': case 'remove_column':
$column = param_integer('column'); $column = param_integer('column');
if (view_remove_column($view, $column)) { if (view_remove_column($view->id, $column)) {
// Just do it - no message // Just do it - no message
json_reply(false, false); json_reply(false, false);
} }
......
...@@ -29,9 +29,11 @@ define('PUBLIC', 1); ...@@ -29,9 +29,11 @@ define('PUBLIC', 1);
define('MENUITEM', 'viewrework'); define('MENUITEM', 'viewrework');
require('init.php'); require('init.php');
require('viewlib.php'); require('viewlib.php');
require('view.php');
define('TITLE', 'Views Rework [DANGER construction site]'); define('TITLE', 'Views Rework [DANGER construction site]');
$view = param_integer('view'); $view = param_integer('view');
$view = new View($view);
view_process_changes(); view_process_changes();
$smarty = smarty(array('views'), array('<link rel="stylesheet" href="views.css" type="text/css">')); $smarty = smarty(array('views'), array('<link rel="stylesheet" href="views.css" type="text/css">'));
......
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