Skip to content

Commit

Permalink
made dashboard alias web friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
emrysr committed Jun 15, 2018
1 parent 2ac5abc commit 784bc6b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
48 changes: 31 additions & 17 deletions Views/dashboard_config.php
Expand Up @@ -75,13 +75,15 @@
$("#dashboard-config-button").click(function (){

$("textarea[name=content]").val($("#page").html());
$("textarea[name=content]").data('original', $("#page").html());// used to test for changes
});

$("#configure-save").click(function (){
var fields = {};

fields['name'] = $("input[name=name]").val();
fields['name'] =$("input[name=name]").val();
fields['alias'] = $("input[name=alias]").val();

fields['description'] = $("textarea[name=description]").val();
fields['backgroundcolor'] = $("input[name=backgroundcolor]").val().replace('#','');
fields['feedmode'] = $("select[name=feedmode]").val();
Expand All @@ -98,33 +100,45 @@
$.ajax({
type: "POST",
url : path+"dashboard/set.json",
data : "&id="+dashid+"&fields="+JSON.stringify(fields),
data : "&id="+dashid+"&fields="+encodeURIComponent(JSON.stringify(fields)),
dataType : 'json',
async: true,
success : function(result) {console.log(result)}
});

$.ajax({
type: "POST",
url : path+"dashboard/setcontent.json",
data : "&id="+dashid+'&content='+encodeURIComponent($("textarea[name=content]").val())+'&height='+height,
dataType: 'json',
async: true,
success : function(result) {
if (result.success!=undefined)
{
if (result.success!=undefined) {
if (result.alias===fields.alias) {
$('#dashConfigModal').modal('hide');
} else {
$("input[name=alias]").val(result.alias);
alert("Dashboard alias altered. Too long or not URL friendly." +
"\nYou sent: \n "+
fields.alias +
"\nAltered to : \n " +
result.alias);
}
}
}
});
var contentChanged = $("textarea[name=content]").val() != $("textarea[name=content]").data('original');
if (contentChanged) {
$.ajax({
type: "POST",
url : path+"dashboard/setcontent.json",
data : "&id="+dashid+'&content='+encodeURIComponent($("textarea[name=content]").val())+'&height='+height,
dataType: 'json',
async: true,
success : function(result) {
if (!result.success) {
alert(result.message);
} else {
$("#page").html($("textarea[name=content]").val());
redraw = 1;
reloadiframe = 0; // dont re-calculate vis iframe urls
$('#dashConfigModal').modal('hide');
}
}
}
});

$('#dashConfigModal').modal('hide');
});
}


$('#page-container').css("background-color","#"+fields['backgroundcolor']);

Expand Down
1 change: 1 addition & 0 deletions dashboard
23 changes: 18 additions & 5 deletions dashboard_model.php
Expand Up @@ -128,8 +128,9 @@ public function set($userid,$id,$fields)
{
$userid = (int) $userid;
$id = (int) $id;
$fields = json_decode(stripslashes($fields));

$fields = json_decode($fields);
$fields->alias = $this->make_slug($fields->alias); // make url friendly
$fields->alias = substr($fields->alias,0,20); // limit to 20 chars to match the db
$result = $this->mysqli->query("SELECT * FROM dashboard WHERE userid='$userid' and `id` = '$id'");
if ($row = $result->fetch_object())
{
Expand Down Expand Up @@ -159,13 +160,16 @@ public function set($userid,$id,$fields)

$stmt->execute();
$affected_rows = $stmt->affected_rows;
$error = $stmt->error;
$stmt->close();

if ($affected_rows>0){
return array('success'=>true, 'message'=>'Field updated');
return array('success'=>true, 'message'=>'Field updated', 'id'=>$id, 'alias'=>$row->alias);
} else {
return array('success'=>false, 'message'=>'Nothing changed', 'id'=>$id, 'alias'=>$row->alias);
}
}
return array('success'=>false, 'message'=>'Field could not be updated');
return array('success'=>false, 'message'=>'Field could not be updated'. " $error");
}

// Return the main dashboard from $userid
Expand Down Expand Up @@ -237,6 +241,15 @@ public function build_menu_array($location)
}
return $menu;
}

public function make_slug( $string, $separator = '-' ) {
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = array( '&' => 'and', "'" => '');
$string = mb_strtolower( trim( $string ), 'UTF-8' );
$string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
$string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);
return $string;
}
}

2 changes: 1 addition & 1 deletion dashboard_schema.php
Expand Up @@ -6,7 +6,7 @@
'content' => array('type' => 'text'),
'height' => array('type' => 'int(11)', 'default'=>'600'),
'name' => array('type' => "varchar(30)", 'default'=>'no name'),
'alias' => array('type' => "varchar(10)", 'default'=>''),
'alias' => array('type' => "varchar(20)", 'default'=>''),
'description' => array('type' => "varchar(255)", 'default'=>'no description'),
'main' => array('type' => 'tinyint(1)', 'default'=>'0'),
'public' => array('type' => 'tinyint(1)', 'default'=>'0'),
Expand Down

0 comments on commit 784bc6b

Please sign in to comment.