Skip to content

Commit

Permalink
Added functionality to hide menu elements created by other extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Giel Berkers committed Oct 4, 2012
1 parent 86201a2 commit 41bacde
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
16 changes: 15 additions & 1 deletion content/content.roles.php
Expand Up @@ -211,7 +211,21 @@ public function viewForm()

$group->appendChild($div);
$this->Form->appendChild($group);


// Add custom elements:
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __('Custom links')));

$label = new XMLElement('label', __('Hide custom menu elements'));
$label->appendChild(Widget::Textarea('custom_elements', 5, 15, $this->_data['custom_elements']));
$label->appendChild(new XMLElement('p', __('Enter the links of custom menu elements to exclude these links from the main navigation. You can use this to exclude extensions-driven menu-items like Dashboard or Search Index for example. Give one link per line, and enter the URL after the <code>/symphony/</code>-part. For example: <code>/extension/dashboard/index/</code>.'), array('class'=>'help')));

$group->appendChild($label);

$this->Form->appendChild($group);


// Add the actions:
$actions = new XMLElement('div');
$actions->setAttribute('class', 'actions');
Expand Down
50 changes: 39 additions & 11 deletions extension.driver.php
Expand Up @@ -6,13 +6,13 @@ public function about()
{
return array(
'name' => 'Author Roles',
'version' => '1.1',
'release-date' => '2012-05-08',
'version' => '1.2',
'release-date' => '2012-10-04',
'author' => array(
'name' => 'Giel Berkers',
'website' => 'http://www.gielberkers.com',
'email' => 'info@gielberkers.com'),
'description' => 'Author Roles for Symphony 2.2'
'description' => 'Author Roles for Symphony 2.2 and above'
);
}

Expand Down Expand Up @@ -75,13 +75,19 @@ public function extendNavigation($context)
'name' => __('Author Roles'),
'limit' => 'developer'
);

$data = $this->getCurrentAuthorRoleData();
if($data == false || Administration::instance()->Author->isDeveloper()) {
return;
}

// Disable non-visible sectors:

$custom_elements = explode("\n", $data['custom_elements']);
foreach($custom_elements as &$elem)
{
$elem = trim($elem);
}

// Disable non-visible sections:
$visible = array();
foreach($data['sections'] as $id => $rules)
{
Expand All @@ -99,6 +105,11 @@ public function extendNavigation($context)
$section['visible'] = 'no';
}
}
if(isset($section['link'])) {
if(in_array($section['link'], $custom_elements)) {
$section['visible'] = 'no';
}
}
}
}

Expand Down Expand Up @@ -684,6 +695,7 @@ public function getData($id_role)
// The name of the role:
$roleResult = Symphony::Database()->fetch('SELECT * FROM `tbl_author_roles` WHERE `id` = '.$id_role.';');
$data['name'] = $roleResult[0]['name'];
$data['custom_elements'] = $roleResult[0]['custom_elements'];

// Get sections from the section manager:
$availableSections = SectionManager::fetch();
Expand Down Expand Up @@ -765,17 +777,19 @@ public function deleteRole($id)
*/
public function saveData($values)
{
// Get the name:
// Get the main data:
$name = isset($values['name']) ? Symphony::Database()->cleanValue($values['name']) : '';
$custom_elements = isset($values['custom_elements']) ? $values['custom_elements'] : '';

if(!empty($name) && isset($values['id_role']))
{
// Create the role:
if($values['id_role'] == 0) {
Symphony::Database()->insert(array('name'=>$name), 'tbl_author_roles');
Symphony::Database()->insert(array('name'=>$name, 'custom_elements'=>$custom_elements), 'tbl_author_roles');
$id_role = Symphony::Database()->getInsertId();
} else {
$id_role = $values['id_role'];
Symphony::Database()->update(array('name'=>$name), 'tbl_author_roles', '`id` = '.$id_role);
Symphony::Database()->update(array('name'=>$name, 'custom_elements'=>$custom_elements), 'tbl_author_roles', '`id` = '.$id_role);
}
// First delete all links in the database before new ones are created:
Symphony::Database()->query('DELETE FROM `tbl_author_roles_sections` WHERE `id_role` = '.$id_role.';');
Expand Down Expand Up @@ -823,6 +837,7 @@ public function install()
Symphony::Database()->query("CREATE TABLE IF NOT EXISTS `tbl_author_roles` (
`id` INT(11) unsigned NOT NULL auto_increment,
`name` VARCHAR(255) NOT NULL,
`custom_elements` TEXT NULL,
PRIMARY KEY (`id`)
);");

Expand Down Expand Up @@ -865,7 +880,7 @@ public function install()
);");

}

/**
* Uninstallation
*/
Expand All @@ -877,5 +892,18 @@ public function uninstall()
Symphony::Database()->query("DROP TABLE `tbl_author_roles_sections`");
Symphony::Database()->query("DROP TABLE `tbl_author_roles_fields`");
}

/**
* Update instructions
* @param $previousVersion
* The version that is currently installed in this Symphony installation
* @return boolean
*/
public function update($previousVersion) {
if (version_compare($previousVersion, '1.2', '<')) {
// Update from pre-1.1 to 1.2:
return Symphony::Database()->query('ALTER TABLE `tbl_author_roles` ADD `custom_elements` TEXT NULL;');
}
}
}
?>

1 change: 1 addition & 0 deletions extension.meta.xml
Expand Up @@ -16,5 +16,6 @@
<releases>
<release version="1.0" date="2011-03-17" min="2.2" max="2.2.5" />
<release version="1.1" date="2012-05-08" min="2.3" />
<release version="1.2" date="2012-10-04" min="2.3" />
</releases>
</extension>
21 changes: 20 additions & 1 deletion readme.md
@@ -1,6 +1,6 @@
# Author Roles #

Version: 1.1
Version: 1.2
Author: Giel Berkers
Website: http://www.gielberkers.com
E-mail: info@gielberkers.com
Expand All @@ -17,6 +17,7 @@ More detailed: The extension allows you to set the following permissions:
- **Hide fields on certain sections**
- **Let the author only see their own created entries**
- **Show or hide certain entries on the publish page of sections**
- **Hide menu items created by other extensions**

## How does it work? ##

Expand All @@ -26,6 +27,24 @@ When you create a role you can set the permission for each section, by using the

By clicking 'edit' underneath 'Entries' you can set various permissions. You can set if the author is only allowed to see the entries he/she created, and you can set a filter. The filter works quite easy actually. First you choose if the filter is to show or hide certain entries. In the textfield you can on or more ID's of entries. You can also set a range of ID's by using a hyphen.

## Important sidenote for hiding menu items created by other extensions ##

Since extensions can not (yet) influence the execution order of extensions in Symphony, you need to apply a tiny little 'hack'
to Symphony to make sure the 'Author Roles'-extension is executed as last, after all other extensions have made their possible
modifications to the navigation. To do this, you need to edit `lib/toolkit/class.extensionmanager.php` and look at the SQL-query
in the `__construct()`-function for this line:

ORDER BY t2.delegate, t1.name

and change it to:

ORDER BY t2.delegate, t1.id ASC

This makes the execution order of extensions ordered by ID instead of name. Now, if you added this extension as last to your list,
you're safe now, otherwise you have to go into the database, look for the `tbl_extensions`-table, write down the ID, and update the
ID to be the highest number in the table. Don't change the other extensions! After that, edit the `tbl_extensions_delegates`-table
and change the rows which have `extension_id` set to the old ID, and update them to the new ID. Now everything should work just fine!

## Sidenotes ##

- Fields that are set to 'hidden' will be made hidden with JavaScript. This is to ensure that all the data gets send and stored on a POST-action when an entry is saved. However, this could cause some issues with third-party field-types, although these aren't encountered yet.
Expand Down

0 comments on commit 41bacde

Please sign in to comment.