Commit 7599ec0c authored by Nigel McNie's avatar Nigel McNie
Browse files

(#3063) part 2: Hide blocktypes that don't make sense when making a

View.

This means that blog/profile/resume blocktypes don't show up in
group/institution/site Views.

This is controlled through a new Blocktype method - allowed_in_view.
Various code had to be changed to listen to this method, mostly around
the display of the category browser.

When making a template, all blocktypes are available.

Part 3 is to change the configuration forms to be more sensible when
editing a template, and then testing what happens when blocktypes are
rendered somewhere that they "shouldn't" be - e.g. a My Friends block in
a group View.
parent 1cfe7200
......@@ -150,6 +150,14 @@ class PluginBlocktypeBlog extends PluginBlocktype {
return 'nocopy';
}
/**
* Blog blocktype is only allowed in personal views, because currently
* there's no such thing as group/site blogs
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -148,6 +148,14 @@ class PluginBlocktypeBlogpost extends PluginBlocktype {
return 'nocopy';
}
/**
* Blogpost blocktype is only allowed in personal views, because currently
* there's no such thing as group/site blogs
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -126,6 +126,14 @@ class PluginBlocktypeRecentposts extends PluginBlocktype {
return 'nocopy';
}
/**
* Recentposts blocktype is only allowed in personal views, because
* currently there's no such thing as group/site blogs
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -170,6 +170,14 @@ class PluginBlocktypeContactinfo extends PluginBlocktype {
return 'shallow';
}
/**
* Contactinfo blocktype is only allowed in personal views, because
* there's no such thing as group/site profiles
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -231,6 +231,14 @@ class PluginBlocktypeProfileinfo extends PluginBlocktype {
return 'shallow';
}
/**
* Profileinfo blocktype is only allowed in personal views, because
* there's no such thing as group/site profiles
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -145,6 +145,14 @@ class PluginBlocktypeEntireresume extends PluginBlocktype {
return 'shallow';
}
/**
* Entireresume blocktype is only allowed in personal views, because
* there's no such thing as group/site resumes
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -156,6 +156,14 @@ class PluginBlocktypeResumefield extends PluginBlocktype {
return 'shallow';
}
/**
* Resumefield blocktype is only allowed in personal views, because
* there's no such thing as group/site resumes
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -139,15 +139,13 @@ abstract class PluginBlocktype extends Plugin {
return get_string('pluginname', 'artefact.' . $name);
}
public static function get_blocktypes_for_category_and_viewtype($category, $viewtype) {
public static function get_blocktypes_for_category($category, View $view) {
$sql = 'SELECT bti.name, bti.artefactplugin
FROM {blocktype_installed} bti
JOIN {blocktype_installed_category} btic ON btic.blocktype = bti.name
JOIN {blocktype_installed_viewtype} bivt ON bivt.blocktype = bti.name
WHERE btic.category = ?
AND bivt.viewtype = ?
ORDER BY bti.name';
if (!$bts = get_records_sql_array($sql, array($category, $viewtype))) {
if (!$bts = get_records_sql_array($sql, array($category))) {
return false;
}
......@@ -156,15 +154,16 @@ abstract class PluginBlocktype extends Plugin {
foreach ($bts as $bt) {
$namespaced = blocktype_single_to_namespaced($bt->name, $bt->artefactplugin);
safe_require('blocktype', $namespaced);
$temp = array(
'name' => $bt->name,
'title' => call_static_method(generate_class_name('blocktype', $namespaced), 'get_title'),
'description' => call_static_method(generate_class_name('blocktype', $namespaced), 'get_description'),
'singleonly' => call_static_method(generate_class_name('blocktype', $namespaced), 'single_only'),
'artefactplugin' => $bt->artefactplugin,
'thumbnail_path' => get_config('wwwroot') . 'thumb.php?type=blocktype&bt=' . $bt->name . ((!empty($bt->artefactplugin)) ? '&ap=' . $bt->artefactplugin : ''),
);
$blocktypes[] = $temp;
if ($view->get('template') || call_static_method(generate_class_name('blocktype', $namespaced), 'allowed_in_view', $view)) {
$blocktypes[] = array(
'name' => $bt->name,
'title' => call_static_method(generate_class_name('blocktype', $namespaced), 'get_title'),
'description' => call_static_method(generate_class_name('blocktype', $namespaced), 'get_description'),
'singleonly' => call_static_method(generate_class_name('blocktype', $namespaced), 'single_only'),
'artefactplugin' => $bt->artefactplugin,
'thumbnail_path' => get_config('wwwroot') . 'thumb.php?type=blocktype&bt=' . $bt->name . ((!empty($bt->artefactplugin)) ? '&ap=' . $bt->artefactplugin : ''),
);
}
}
return $blocktypes;
}
......@@ -203,6 +202,28 @@ abstract class PluginBlocktype extends Plugin {
return 'shallow';
}
/**
* Whether this blocktype is allowed in the given View.
*
* Some blocktypes may wish to limit whether they're allowed in a View if,
* for example, they make no sense when the view is owned by a certain type
* of owner.
*
* For example, the 'profile information' blocktype makes no sense in a
* group View.
*
* Of course, blocktypes could implement stranger rules - e.g. only allow
* when the view has 'ponies' in its description (BTW: such blocktypes
* would be totally awesome).
*
* @param View The View to check
* @return boolean Whether blocks of this blocktype are allowed in the
* given view.
*/
public static function allowed_in_view(View $view) {
return true;
}
}
abstract class SystemBlockType extends PluginBlockType {
......
......@@ -147,6 +147,13 @@ class PluginBlocktypeMyfriends extends SystemBlocktype {
return 'shallow';
}
/**
* Myfriends only makes sense for personal views
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -71,6 +71,13 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
return 'shallow';
}
/**
* Mygroups only makes sense for personal views
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -104,6 +104,13 @@ class PluginBlocktypeMyviews extends SystemBlocktype {
return 'shallow';
}
/**
* Myviews only makes sense for personal views
*/
public static function allowed_in_view(View $view) {
return $view->get('owner') != null;
}
}
?>
......@@ -174,6 +174,13 @@ class PluginBlocktypeWall extends SystemBlocktype {
return 'shallow';
}
/**
* Wall only makes sense on profile viewtypes
*/
public static function allowed_in_view(View $view) {
return $view->get('type') == 'profile';
}
}
?>
......@@ -54,6 +54,7 @@ class View {
private $columns;
private $dirtycolumns; // for when we change stuff
private $tags;
private $categorydata;
private $editingroles;
private $template;
private $copynewuser = 0;
......@@ -272,6 +273,9 @@ class View {
if ($field == 'tags') { // special case
return $this->get_tags();
}
if ($field == 'categorydata') {
return $this->get_category_data();
}
return $this->{$field};
}
......@@ -631,39 +635,10 @@ class View {
/**
* Returns HTML for the category list
*
* @param string $defaultcategory The currently selected category
* @param View $view The view we're currently using
* @param string $category The currently selected category
*/
public static function build_category_list($defaultcategory, View $view, $new=0) {
require_once(get_config('docroot') . '/blocktype/lib.php');
// Change to a left join to show tabs with no results
$cats = get_records_sql_array('SELECT bc.name, COUNT(*) AS "count"
FROM {blocktype_category} bc
INNER JOIN {blocktype_installed_category} bic ON (bc.name = bic.category)
WHERE EXISTS (
SELECT 1
FROM {blocktype_installed_viewtype} bivt
JOIN {blocktype_installed} bi ON bi.name = bivt.blocktype
WHERE bivt.viewtype = ?
AND bic.blocktype = bi.name
)
GROUP BY bc.name
ORDER BY bc.name', array($view->get('type')));
$categories = array_map(
create_function(
'$a',
'return array(
"name" => $a->name,
"title" => call_static_method("PluginBlocktype", "category_title_from_name", $a->name) . " (" . $a->count . ")",
);'
),
$cats
);
// The 'internal' plugin is known to the outside world as 'profile', so
// we need to sort on the actual name
usort($categories, create_function('$a, $b', 'return strnatcasecmp($a[\'title\'], $b[\'title\']);'));
public function build_category_list($category, $new=0) {
$categories = $this->get_category_data();
$flag = false;
foreach ($categories as &$cat) {
$classes = '';
......@@ -671,7 +646,7 @@ class View {
$flag = true;
$classes[] = 'first';
}
if ($defaultcategory == $cat['name']) {
if ($category == $cat['name']) {
$classes[] = 'current';
}
if ($classes) {
......@@ -684,11 +659,66 @@ class View {
$smarty = smarty_core();
$smarty->assign('categories', $categories);
$smarty->assign('viewid', $view->get('id'));
$smarty->assign('viewid', $this->get('id'));
$smarty->assign('new', $new);
return $smarty->fetch('view/blocktypecategorylist.tpl');
}
/**
* Gets the name of the first blocktype category for this View.
*
* This can change based on what blocktypes allow themselves to be in what
* types of View. For example, in a group View, blog blocktypes aren't
* allowed (yet), so the first blocktype category shown won't be "blog"
*/
public function get_default_category() {
$data = $this->get_category_data();
return $data[0]['name'];
}
/**
* Gets information about blocktype categories for blocks that can be put
* in this View
*
* For each category, returns its name, a localised title and the number of
* blocktypes in the category that can be put in this View.
*
* If a category has no blocktypes that can be put in this View, it is not
* returned
*/
private function get_category_data() {
if (isset($this->category_data)) {
return $this->category_data;
}
require_once(get_config('docroot') . '/blocktype/lib.php');
$categories = array();
foreach (get_records_array('blocktype_installed_category') as $blocktypecategory) {
safe_require('blocktype', $blocktypecategory->blocktype);
if ($this->get('template') || call_static_method(generate_class_name("blocktype", $blocktypecategory->blocktype), "allowed_in_view", $this)) {
if (!isset($categories[$blocktypecategory->category])) {
$categories[$blocktypecategory->category] = array(
'name' => $blocktypecategory->category,
'title' => call_static_method("PluginBlocktype", "category_title_from_name", $blocktypecategory->category),
'count' => 0,
);
}
$categories[$blocktypecategory->category]['count']++;
}
}
foreach ($categories as &$category) {
$category['title'] .= ' (' . $category['count'] . ')';
unset($category['count']);
}
// The 'internal' plugin is known to the outside world as 'profile', so
// we need to sort on the actual name
usort($categories, create_function('$a, $b', 'return strnatcasecmp($a[\'title\'], $b[\'title\']);'));
return $this->category_data = $categories;
}
/**
* Returns HTML for the blocktype list for a particular category
*
......@@ -699,7 +729,7 @@ class View {
*/
public function build_blocktype_list($category, $javascript=false) {
require_once(get_config('docroot') . 'blocktype/lib.php');
$blocktypes = PluginBlockType::get_blocktypes_for_category_and_viewtype($category, $this->get('type'));
$blocktypes = PluginBlockType::get_blocktypes_for_category($category, $this);
$smarty = smarty_core();
$smarty->assign_by_ref('blocktypes', $blocktypes);
......@@ -1044,7 +1074,12 @@ class View {
throw new ParamOutOfRangeException(get_string('missingparam'. $require, 'error'));
}
}
safe_require('blocktype', $values['blocktype']);
if (!$this->get('template') && !call_static_method(generate_class_name('blocktype', $values['blocktype']), 'allowed_in_view', $this)) {
throw new UserException('[translate] Cannot put ' . $values['blocktype'] . ' blocktypes into this view');
}
$bi = new BlockInstance(0,
array(
'blocktype' => $values['blocktype'],
......
<ul>
{foreach from=$categories item=category}
<li class="{$category.class}"><a href="{$WWWROOT}view/blocks.php?id={$viewid}&amp;c={$category.name | escape}&amp;new={$new}">{$category.title | escape}</a></li>
{foreach from=$categories key=name item=category}
<li class="{$category.class}"><a href="{$WWWROOT}view/blocks.php?id={$viewid}&amp;c={$category.name|escape}&amp;new={$new}">{$category.title|escape}</a></li>
{/foreach}
</ul>
......@@ -107,14 +107,7 @@ else {
$category = param_alpha('c', '');
// Make the default category the first tab if none is set
if ($category === '') {
$category = get_field_sql("
SELECT bc.name
FROM {blocktype_category} bc
JOIN {blocktype_installed_category} bic ON bic.category = bc.name
JOIN {blocktype_installed_viewtype} biv ON biv.blocktype = bic.blocktype
WHERE biv.viewtype = ?
ORDER BY bc.name
LIMIT 1", array($view->get('type')));
$category = $view->get_default_category();
}
$view->process_changes($category, $new);
......@@ -127,7 +120,7 @@ $smarty = smarty(array('views', 'tinytinymce', 'paginator', 'tablerenderer'), ar
// The list of categories for the tabbed interface
$smarty->assign('category_list', View::build_category_list($category, $view, $new));
$smarty->assign('category_list', $view->build_category_list($category, $new));
// The list of blocktypes for the default category
$smarty->assign('blocktype_list', $view->build_blocktype_list($category));
......
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