Skip to content

Commit

Permalink
Multiple section support. RE: #173
Browse files Browse the repository at this point in the history
  • Loading branch information
brendo committed Dec 10, 2012
1 parent 2d260e6 commit 0ac18d1
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 69 deletions.
82 changes: 42 additions & 40 deletions content/content.roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ public function __viewIndex() {
$this->setPageType('table');
$this->setTitle(__('%1$s – %2$s', array(__('Symphony'), __('Member Roles'))));

if(is_null(extension_Members::getFieldHandle('role')) && !is_null(extension_Members::getMembersSection())) {
if(!FieldManager::isFieldUsed(extension_Members::getFieldType('role'))) {
$this->pageAlert(
__('There is no Member: Role field in the active Members section. <a href="%s%d/">Add Member: Role field?</a>',
__('There are no Member: Role fields in this Symphony installation. <a href="%s">Add Member: Role field?</a>',
array(
SYMPHONY_URL . '/blueprints/sections/edit/',
extension_Members::getMembersSection()
SYMPHONY_URL . '/blueprints/sections/'
)),
Alert::NOTICE
);
Expand All @@ -25,6 +24,7 @@ public function __viewIndex() {
));

$roles = RoleManager::fetch();
$membersSections = extension_Members::discoverMemberSections();

$aTableHead = array(
array(__('Name'), 'col'),
Expand All @@ -39,17 +39,21 @@ public function __viewIndex() {
));
}

else if(is_null(extension_Members::getMembersSection())) {
else if(empty($membersSections)) {
$aTableBody = array(Widget::TableRow(
array(Widget::TableData(__('No Member section has been specified in <a href="%s">Preferences</a>. Please do this first.', array(SYMPHONY_URL.'/system/preferences/')), 'inactive', NULL, count($aTableHead)))
array(Widget::TableData(__('No Member sections exist in Symphony. <a href="%s">Create a Section?</a>',
array(
SYMPHONY_URL . '/blueprints/sections/'
))
, 'inactive', NULL, count($aTableHead)))
));
}

else {
$section = SectionManager::fetch(extension_Members::getMembersSection());
$hasRoles = FieldManager::isFieldUsed(extension_Members::getFieldType('role'));
$roleFields = FieldManager::fetch(null, null, 'ASC', 'sortorder', extension_Members::getFieldType('role'));

$with_selected_roles = array();
$hasRoles = !is_null(extension_Members::getFieldHandle('role'));

foreach($roles as $role){
// Setup each cell
Expand All @@ -62,16 +66,17 @@ public function __viewIndex() {
}

// Get the number of members for this role, as long as it's not the Public Role.
if($hasRoles && $role->get('id') != Role::PUBLIC_ROLE) {
$member_count = Symphony::Database()->fetchVar('count', 0, sprintf(
"SELECT COUNT(*) AS `count` FROM `tbl_entries_data_%d` WHERE `role_id` = %d",
extension_Members::getField('role')->get('id'), $role->get('id')
));

$td2 = Widget::TableData(Widget::Anchor(
"$member_count",
SYMPHONY_URL . '/publish/' . $section->get('handle') . '/?filter=' . extension_Members::getFieldHandle('role') . ':' . $role->get('id')
));
if($hasRoles && $role->get('id') !== Role::PUBLIC_ROLE) {
$member_count = 0;

foreach($roleFields as $roleField) {
$member_count += Symphony::Database()->fetchVar('count', 0, sprintf(
"SELECT COUNT(*) AS `count` FROM `tbl_entries_data_%d` WHERE `role_id` = %d",
$roleField->get('id'), $role->get('id')
));
}

$td2 = Widget::TableData($member_count);
}

else if($role->get('id') == Role::PUBLIC_ROLE) {
Expand All @@ -87,7 +92,7 @@ public function __viewIndex() {

if($hasRoles && $role->get('id') != Role::PUBLIC_ROLE) {
$with_selected_roles[] = array(
"move::" . $role->get('id'), false,$role->get('name')
"move::" . $role->get('id'), false, $role->get('name')
);
}
}
Expand Down Expand Up @@ -132,7 +137,7 @@ public function __viewEdit() {
if(!$role_id = $this->_context[1]) redirect(extension_Members::baseURL() . 'roles/');

if(!$existing = RoleManager::fetch($role_id)){
throw new SymphonyErrorPage(__('The role you requested to edit does not exist.'), __('Role not found'), 'error');
throw new SymphonyErrorPage(__('The role you requested to edit does not exist.'), __('Role not found'));
}
}

Expand Down Expand Up @@ -216,7 +221,7 @@ public function __viewEdit() {
$label = Widget::Label(__('Name'));
$label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name'])));

if(isset($this->_errors['name'])) $fieldset->appendChild(Widget::wrapFormElementWithError($label, $this->_errors['name']));
if(isset($this->_errors['name'])) $fieldset->appendChild(Widget::Error($label, $this->_errors['name']));
else $fieldset->appendChild($label);

$this->Form->appendChild($fieldset);
Expand Down Expand Up @@ -359,27 +364,28 @@ public function __viewEdit() {
}

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

}

public function __actionIndex() {
$checked = (is_array($_POST['items'])) ? array_keys($_POST['items']) : null;

if(is_array($checked) && !empty($checked)) {
if(preg_match('/move::(\d+)/i', $_POST['with-selected'], $match)) {
$roleFields = FieldManager::fetch(null, null, 'ASC', 'sortorder', extension_Members::getFieldType('role'));
$target_role = $match[1];

if(!$replacement = RoleManager::fetch($target_role)) return false;

foreach($checked as $role_id){
if($role_id == $target_role) continue;

Symphony::Database()->query(sprintf(
"UPDATE `tbl_entries_data_%d` SET `role_id` = %d WHERE `role_id` = %d",
extension_members::getField('role')->get('id'),
$target_role,
$role_id
));
foreach($roleFields as $roleField) {
Symphony::Database()->query(sprintf("
UPDATE `tbl_entries_data_%d` SET `role_id` = %d WHERE `role_id` = %d
",
$roleField->get('id'), $target_role, $role_id
));
}
}

return true;
Expand Down Expand Up @@ -423,7 +429,7 @@ public function __actionEdit() {
if(!$role_id = $this->_context[1]) redirect(extension_Members::baseURL() . 'roles/');

if(!$existing = RoleManager::fetch($role_id)){
throw new SymphonyErrorPage(__('The role you requested to edit does not exist.'), __('Role not found'), 'error');
throw new SymphonyErrorPage(__('The role you requested to edit does not exist.'), __('Role not found'));
}
}

Expand All @@ -444,11 +450,9 @@ public function __actionEdit() {
}
}
// If we are editing, we need to only run this check if the Role name has been altered
else {
if($handle != $existing->get('handle') && RoleManager::fetchRoleIDByHandle($handle)){
$this->_errors['name'] = __('A role with the name <code>%s</code> already exists.', array($name));
return false;
}
else if($handle != $existing->get('handle') && RoleManager::fetchRoleIDByHandle($handle)){
$this->_errors['name'] = __('A role with the name <code>%s</code> already exists.', array($name));
return false;
}

$data['roles'] = array(
Expand All @@ -469,10 +473,8 @@ public function __actionEdit() {
redirect(extension_members::baseURL() . 'roles/edit/' . $role_id . '/created/');
}
}
else {
if(RoleManager::edit($role_id, $data)) {
redirect(extension_members::baseURL() . 'roles/edit/' . $role_id . '/saved/');
}
else if(RoleManager::edit($role_id, $data)) {
redirect(extension_members::baseURL() . 'roles/edit/' . $role_id . '/saved/');
}
}
}
Expand All @@ -487,8 +489,8 @@ public function __actionDelete($role_id = null, $redirect = null, $purge_members
);
}

if(!$existing = RoleManager::fetch($role_id)){
throw new SymphonyErrorPage(__('The role you requested to delete does not exist.'), __('Role not found'), 'error');
if(!$existing = RoleManager::fetch($role_id)) {
throw new SymphonyErrorPage(__('The role you requested to delete does not exist.'), __('Role not found'));
}

// @todo What should happen to any Members that had this Role?
Expand Down
2 changes: 1 addition & 1 deletion fields/field.memberemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function createTable(){
Utilities:
-------------------------------------------------------------------------*/

public function fetchMemberIDBy($needle) {
public function fetchMemberIDBy($needle, $member_id = null) {
if(is_array($needle)) {
extract($needle);
}
Expand Down
9 changes: 6 additions & 3 deletions fields/field.memberpassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function createTable(){
* @param integer $member_id
* @return Entry|null
*/
public function fetchMemberIDBy($needle, $member_id) {
public function fetchMemberIDBy($needle, $member_id = null) {
if(is_array($needle)) {
extract($needle);
}
Expand All @@ -112,10 +112,13 @@ public function fetchMemberIDBy($needle, $member_id) {
SELECT `entry_id`, `reset`
FROM `tbl_entries_data_%d`
WHERE `password` = '%s'
AND `entry_id` = %d
AND %s
LIMIT 1
",
$this->get('id'), $password, Symphony::Database()->cleanValue($member_id)
$this->get('id'), $password,
is_null($member_id)
? '1 = 1'
: sprintf('`entry_id` = %d', Symphony::Database()->cleanValue($member_id))
));

// Check that if the password has been reset that it is still valid
Expand Down
2 changes: 1 addition & 1 deletion fields/field.memberusername.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function createTable(){
* @param string|array $needle
* @return Entry
*/
public function fetchMemberIDBy($needle) {
public function fetchMemberIDBy($needle, $member_id = null) {
if(is_array($needle)) {
extract($needle);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/class.identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function fetchMemberFromID($member_id){
return Identity::$driver->getMemberDriver()->initialiseMemberObject($member_id);
}

abstract public function fetchMemberIDBy($needle);
abstract public function fetchMemberIDBy($needle, $member_id = null);

/*-------------------------------------------------------------------------
Publish:
Expand All @@ -82,7 +82,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $error =

// Error?
if(!is_null($error)) {
$wrapper->appendChild(Widget::wrapFormElementWithError($label, $error));
$wrapper->appendChild(Widget::Error($label, $error));
}
else {
$wrapper->appendChild($label);
Expand Down
39 changes: 37 additions & 2 deletions lib/class.members.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function isLoggedIn();

// Finding
public static function setIdentityField(array $credentials, $simplified = true);
public function getMemberSectionID();
public function findMemberIDFromCredentials(array $credentials);
public function fetchMemberFromID($member_id = null);

Expand All @@ -35,12 +36,18 @@ public function appendLoginStatusToEventXML(array $context = null);
Utilities:
-------------------------------------------------------------------------*/

public function getMember() {
return $this->Member;
}

public function getMemberID() {
return self::$member_id;
}

public function getMember() {
return $this->Member;
public function getMemberSectionID() {
if(is_null($this->cookie)) $this->initialiseCookie();

return $this->cookie->get('members-section-id');
}

/*-------------------------------------------------------------------------
Expand All @@ -63,6 +70,32 @@ public function initialiseMemberObject($member_id = null) {
return $this->Member;
}

/**
* This function will adjust the locale for the currently logged in
* user if the active Member section has a Member: Timezone field.
*
* @param integer $member_id
* @return void
*/
public function updateSystemTimezoneOffset() {
if(is_null($this->Member)) return;

$timezone = extension_Members::getField('timezone', $this->getMember()->get('section_id'));

if(!$timezone instanceof fieldMemberTimezone) return;

$tz = $timezone->getMemberTimezone($this->getMemberID());

if(is_null($tz)) return;

try {
DateTimeObj::setDefaultTimezone($tz);
}
catch(Exception $ex) {
Symphony::Log()->pushToLog(__('Members Timezone') . ': ' . $ex->getMessage(), $ex->getCode(), true);
}
}

/*-------------------------------------------------------------------------
Finding:
-------------------------------------------------------------------------*/
Expand Down Expand Up @@ -117,6 +150,7 @@ public function addMemberDetailsToPageParams(array $context = null) {
$this->initialiseMemberObject();

$context['params']['member-id'] = $this->getMemberID();
$context['params']['member-section-id'] = $this->getMemberSectionID();

if(!is_null(extension_Members::getFieldHandle('role'))) {
$role_data = $this->getMember()->getData(extension_Members::getField('role')->get('id'));
Expand All @@ -140,6 +174,7 @@ public function appendLoginStatusToEventXML(array $context = null){
$result->setAttributearray(array(
'logged-in' => 'yes',
'id' => $this->getMemberID(),
'section-id' => $this->getMemberSectionID(),
'result' => 'success'
));
}
Expand Down
Loading

0 comments on commit 0ac18d1

Please sign in to comment.