Commit 10ff13f4 authored by Melissa Draper's avatar Melissa Draper Committed by Gerrit Code Review
Browse files

Merge "Allow users to edit clean urls"

parents 3663cd40 5c3cdcc6
......@@ -85,6 +85,20 @@ if ($authobj->authname == 'internal') {
);
}
if (get_config('cleanurls')) {
$elements['changeprofileurl'] = array(
'value' => '<tr><td colspan="2"><h3>' . get_string('changeprofileurl', 'account') . '</h3></td></tr>'
);
$elements['urlid'] = array(
'type' => 'text',
'defaultvalue' => $USER->get('urlid'),
'title' => get_string('profileurl', 'account'),
'prehtml' => '<span class="description">' . get_config('wwwroot') . get_config('cleanurluserdefault') . '/</span> ',
'description' => get_string('profileurldescription', 'account') . ' ' . get_string('cleanurlallowedcharacters'),
'rules' => array('maxlength' => 30, 'regex' => get_config('cleanurlvalidate')),
);
}
$elements['accountoptionsdesc'] = array(
'value' => '<tr><td colspan="2"><h3>' . get_string('accountoptionsdesc', 'account') . '</h3></td></tr>'
);
......@@ -149,6 +163,15 @@ function accountprefs_validate(Pieform $form, $values) {
$form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
}
}
if (get_config('cleanurls') && $values['urlid'] != $USER->get('urlid')) {
if (strlen($values['urlid']) < 3) {
$form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
}
else if (record_exists('usr', 'urlid', $values['urlid'])) {
$form->set_error('urlid', get_string('urlalreadytaken', 'account'));
}
}
}
function accountprefs_submit(Pieform $form, $values) {
......@@ -196,6 +219,11 @@ function accountprefs_submit(Pieform $form, $values) {
$returndata['username'] = $values['username'];
}
if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) {
$USER->urlid = $values['urlid'];
$USER->commit();
}
db_commit();
$updatelang = isset($values['lang']) && $values['lang'] != $oldlang;
......
......@@ -73,6 +73,7 @@ else {
'hidemembersfrommembers' => 0,
'invitefriends' => 0,
'suggestfriends' => 0,
'urlid' => null,
);
}
......@@ -95,6 +96,15 @@ $form = array(
'cols' => 55,
'defaultvalue' => $group_data->description,
),
'urlid' => array(
'type' => 'text',
'title' => get_string('groupurl', 'group'),
'prehtml' => '<span class="description">' . get_config('wwwroot') . get_config('cleanurlgroupdefault') . '/</span> ',
'description' => get_string('groupurldescription', 'group') . ' ' . get_string('cleanurlallowedcharacters'),
'rules' => array('maxlength' => 30, 'regex' => get_config('cleanurlvalidate')),
'defaultvalue' => $group_data->urlid,
'ignore' => !$id || !get_config('cleanurls'),
),
'settings' => array(
'type' => 'fieldset',
'collapsible' => true,
......@@ -347,6 +357,25 @@ function editgroup_validate(Pieform $form, $values) {
}
}
}
if (isset($values['urlid']) && get_config('cleanurls')) {
$urlidlength = strlen($values['urlid']);
if ($group_data->urlid != $values['urlid']) {
if ($urlidlength && record_exists('group', 'urlid', $values['urlid'])) {
$form->set_error('urlid', get_string('groupurltaken', 'group'));
}
else if (!$urlidlength) {
// Once you've set a group url, there's no going back
$form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
}
}
// If the urlid is empty, we'll generate it when creating a group, but if it's 1 or 2 characters
// long, it's an error.
if ($urlidlength > 0 && $urlidlength < 3) {
$form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
}
}
if (!empty($values['open'])) {
if (!empty($values['controlled'])) {
$form->set_error('open', get_string('membershipopencontrolled', 'group'));
......@@ -393,6 +422,10 @@ function editgroup_submit(Pieform $form, $values) {
'suggestfriends' => intval($values['suggestfriends']),
);
if (get_config('cleanurls') && isset($values['urlid'])) {
$newvalues['urlid'] = $values['urlid'];
}
db_begin();
if ($group_data->id) {
......
......@@ -38,6 +38,12 @@ $string['changeusernamedesc'] = 'The username you use to log into %s. Usernames
$string['usernameexists'] = 'This username is taken, please choose another.';
$string['accountoptionsdesc'] = 'General account options';
$string['changeprofileurl'] = 'Change profile URL';
$string['profileurl'] = 'Profile URL';
$string['profileurldescription'] = 'A personalised URL for your profile page. This field must be 3-30 characters long.';
$string['urlalreadytaken'] = 'This profile URL is already taken. Please choose another.';
$string['friendsnobody'] = 'Nobody may add me as a friend';
$string['friendsauth'] = 'New friends require my authorisation';
$string['friendsauto'] = 'New friends are automatically authorised';
......
......@@ -47,6 +47,10 @@ $string['groupsaved'] = 'Group saved successfully';
$string['invalidgroup'] = 'The group doesn\'t exist';
$string['canteditdontown'] = 'You can\'t edit this group because you don\'t own it';
$string['groupdescription'] = 'Group description';
$string['groupurl'] = 'Group homepage URL';
$string['groupurldescription'] = "The URL of your group's homepage. This field must be 3-30 characters long.";
$string['groupurltaken'] = 'That URL is already taken by another group.';
$string['Membership'] = 'Membership';
$string['Roles'] = 'Roles';
$string['Open'] = 'Open';
......
......@@ -1031,3 +1031,6 @@ $string['setblocktitle'] = 'Set a block title';
$string['filenotfound'] = 'File not found';
$string['betweenxandy'] = 'Between %s and %s';
// Clean URLs
$string['cleanurlallowedcharacters'] = 'Only lowercase letters from a-z, numbers, and - are allowed.';
......@@ -37,6 +37,9 @@ $string['editaccess'] = 'Edit access';
$string['next'] = 'Next';
$string['back'] = 'Back';
$string['title'] = 'Page title';
$string['viewurl'] = 'Page URL';
$string['viewurldescription'] = 'A readable URL for your page. This field must be between 3 and 100 characters long.';
$string['userviewurltaken'] = 'This URL is already taken. Please choose another.';
$string['description'] = 'Page description';
$string['startdate'] = 'Access start date/time';
$string['stopdate'] = 'Access end date/time';
......
......@@ -373,7 +373,7 @@ function group_create($data) {
(object) array(
'name' => $data['name'],
'description' => isset($data['description']) ? $data['description'] : null,
'urlid' => $data['urlid'],
'urlid' => isset($data['urlid']) ? $data['urlid'] : null,
'grouptype' => $data['grouptype'],
'category' => isset($data['category']) ? intval($data['category']) : null,
'jointype' => $jointype,
......
......@@ -85,6 +85,22 @@ if ($studentid !== '') {
$ownerformatoptions[FORMAT_NAME_STUDENTID] = sprintf($formatstring, get_string('studentid'), $studentid);
}
// Clean urls are only available for portfolio views owned by groups or users who already
// have their own clean profiles or group homepages.
if ($urlallowed = get_config('cleanurls') && $view->get('type') == 'portfolio' && !$institution) {
if ($group) {
$groupdata = get_record('group', 'id', $group);
$urlallowed &= strlen($groupdata->urlid);
$cleanurlbase = get_config('wwwroot') . get_config('cleanurlgroupdefault') . '/' . $groupdata->urlid . '/';
}
else {
$userurlid = $USER->get('urlid');
if ($urlallowed &= (!is_null($userurlid) && strlen($userurlid))) {
$cleanurlbase = get_config('wwwroot') . get_config('cleanurluserdefault') . '/' . $userurlid . '/';
}
}
}
$editview = array(
'name' => 'editview',
'method' => 'post',
......@@ -107,6 +123,15 @@ $editview = array(
'defaultvalue' => $view->get('title'),
'rules' => array( 'required' => true ),
),
'urlid' => array(
'type' => 'text',
'title' => get_string('viewurl', 'view'),
'prehtml' => '<span class="description">' . (isset($cleanurlbase) ? $cleanurlbase : '') . '</span> ',
'description' => get_string('viewurldescription', 'view') . ' ' . get_string('cleanurlallowedcharacters'),
'defaultvalue' => $new ? null : $view->get('urlid'),
'rules' => array('maxlength' => 100, 'regex' => get_config('cleanurlvalidate')),
'ignore' => !$urlallowed || $new,
),
'description' => array(
'type' => 'wysiwyg',
'title' => get_string('description','view'),
......@@ -161,8 +186,24 @@ $editview['elements']['submit'] = array(
$editview = pieform($editview);
function editview_validate(Pieform $form, $values) {
global $view;
if (isset($values['urlid']) && $values['urlid'] != $view->get('urlid')) {
if (strlen($values['urlid']) < 3) {
$form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
}
else if ($group = $view->get('group') and record_exists('view', 'group', $group, 'urlid', $values['urlid'])) {
$form->set_error('urlid', get_string('groupviewurltaken', 'view'));
}
else if ($owner = $view->get('owner') and record_exists('view', 'owner', $owner, 'urlid', $values['urlid'])) {
$form->set_error('urlid', get_string('userviewurltaken', 'view'));
}
}
}
function editview_submit(Pieform $form, $values) {
global $new, $view, $SESSION;
global $new, $view, $SESSION, $urlallowed;
$view->set('title', $values['title']);
$view->set('description', $values['description']);
......@@ -173,6 +214,15 @@ function editview_submit(Pieform $form, $values) {
if (isset($values['ownerformat']) && $view->get('owner')) {
$view->set('ownerformat', $values['ownerformat']);
}
if (isset($values['urlid'])) {
$view->set('urlid', strlen($values['urlid']) == 0 ? null : $values['urlid']);
}
else if ($new && $urlallowed) {
// Generate one automatically based on the title
$desired = generate_urlid($values['title'], get_config('cleanurlviewdefault'), 3, 100);
$ownerinfo = (object) array('owner' => $view->get('owner'), 'group' => $view->get('group'));
$view->set('urlid', View::new_urlid($desired, $ownerinfo));
}
$SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
$view->commit();
redirect('/view/blocks.php?id=' . $view->get('id'));
......
Supports Markdown
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