Skip to content

Commit

Permalink
update url slugging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyhealey committed Mar 3, 2016
1 parent 0e6c74c commit 929e734
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 192 deletions.
92 changes: 45 additions & 47 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,59 @@

// THIS NEEDS TO BE TESTED
function slug($name = "untitled")
{
// $pattern = '/(\A\W+|\W+\z)/';
{
// replace non-alphanumerics at the beginning and end of the
// string with nothing
$pattern = '/(\A[^\p{L}\p{N}]+|[^\p{L}\p{N}]+\z)/u';
$replace = '';
$tmp = preg_replace($pattern, $replace, $name);

// replace whitespace with hyphens
$pattern = '/\s+/';
// replace all non-alphanumerics with hyphens
$pattern = '/[^\p{L}\p{N}]+/u';
$replace = '-';
$tmp = preg_replace($pattern, $replace, $tmp);

// replace trailing hyphens
$pattern = '/[^-\w]+/';
// $pattern = '/[^-\p{L}\p{N}]+/u';
$pattern = '/[-]+\z/u';
$replace = '';
// $tmp = preg_replace($pattern, $replace, $tmp);
// make string url lowercase (in a unicode-safe way)
$tmp = mb_convert_case($tmp, MB_CASE_LOWER, "UTF-8");

// make url safe
$tmp = urlencode($tmp);

$tmp = strtolower($tmp);
return urlencode($tmp);
return $tmp;
}

// return a string of random characters [a-z, 0-9]
// of length $len
function rand_str($len=4)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$max = strlen($chars)-1;
$s = "";
for($i = 0; $i < $len; $i++)
{
$c = substr($chars, rand(0, $max), 1);
$s.= $c;
}
return $s;
}

// for our purposes, $alt is assumed to be the id of the object
// this url is meant to reference
function valid_url($u, $alt, $excludes)
{
// array_search returns the position (index) of $u in
// $excludes, or false if not present in the array.
// therefore, strict compare to false
if(empty($u) || array_search($u, $excludes) !== false)
{
$url = $alt;
while(array_search($url, $excludes) !== false)
$url = rand_str();
}
else
$url = $u;

return $url;
}

// why do i need two of these?
Expand Down Expand Up @@ -56,37 +89,7 @@ function resize($src, $dest, $scale)
$si->save($dest);
}

// for use on add.php
// return false if process fails
// (siblings must not have same url slug as object)
// return id of new object on success
function insert_object(&$a, $siblings)
{
global $oo;
global $dt_fmt;

if(!$a['name1'])
$a['name1'] = 'untitled';

if($a['url'])
$a['url'] = slug($a['url']);
else
$a['url'] = slug($a['name1']);

foreach($siblings as $s_id)
if($a['url'] == $oo->get($s_id)['url'])
return false;

foreach($a as $key => $value)
{
if($value)
$a[$key] = "'".$value."'";
else
$a[$key] = "null";
}

return $oo->insert($a);
}

function md2html($md)
{
Expand All @@ -112,11 +115,6 @@ function html2md($html)
return implode("\n", $md);
}

// for use on edit.php
function update_object()
{
}

function process_media($toid)
{
global $mm;
Expand Down
8 changes: 6 additions & 2 deletions models/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
class Model
{
const MYSQL_DATE_FMT = "Y-m-d H:i:s";

// takes: $id of database entry
// returns: associative array of database entry or NULL
public static function get($id)
Expand Down Expand Up @@ -68,7 +70,7 @@ public static function get_all( $fields = array("*"),
public static function insert($arr)
{
global $db;
$dt = date("Y-m-d H:i:s");
$dt = date(self::MYSQL_DATE_FMT);
$arr["created"] = "'".$dt."'";
$arr["modified"] = "'".$dt."'";
$keys = implode(", ", array_keys($arr));
Expand All @@ -88,6 +90,8 @@ public static function insert($arr)
public static function update($id, $arr)
{
global $db;
$dt = date(self::MYSQL_DATE_FMT);
$arr["modified"] = "'".$dt."'";
foreach($arr as $key => $value)
$pairs[] = $key."=".$value;
$z = implode(", ", $pairs);
Expand All @@ -108,7 +112,7 @@ public function deactivate($id)

$sql = "UPDATE ".static::table_name."
SET active = '0',
modified = '".date("Y-m-d H:i:s")."'
modified = '".date(self::MYSQL_DATE_FMT)."'
WHERE id = '$id'";

if($db->query($sql) === TRUE)
Expand Down
1 change: 1 addition & 0 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ a:active {
text-decoration: none;
color: #777;
background-color: #d6d6d6;
vertical-align: top;
}

a:hover {
Expand Down
67 changes: 63 additions & 4 deletions views/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
$var_info["input-type"]["deck"] = "textarea";
$var_info["input-type"]["body"] = "textarea";
$var_info["input-type"]["notes"] = "textarea";
$var_info["input-type"]["begin"] = "datetime-local";
$var_info["input-type"]["end"] = "datetime-local";
$var_info["input-type"]["begin"] = "text";
$var_info["input-type"]["end"] = "text";
$var_info["input-type"]["url"] = "text";
$var_info["input-type"]["rank"] = "number";
$var_info["input-type"]["rank"] = "text";

$var_info["label"] = array();
$var_info["label"]["name1"] = "Name";
Expand All @@ -25,7 +25,66 @@
$var_info["label"]["url"] = "URL Slug";
$var_info["label"]["rank"] = "Rank";

$dt_fmt = "Y-m-d H:i:s";
// for use on add.php
// return false if process fails
// (siblings must not have same url slug as object)
// return id of new object on success
function insert_object(&$new, $siblings)
{
global $oo;

// set default name if no name given
if(!$new['name1'])
$new['name1'] = 'untitled';

// slug-ify url
if($new['url'])
$new['url'] = slug($new['url']);

if(empty($new['url']))
$new['url'] = slug($new['name1']);

// make sure url doesn't clash with urls of siblings
$s_urls = array();
foreach($siblings as $s_id)
$s_urls[] = $oo->get($s_id)['url'];

// deal with dates
if(!empty($new['begin']))
{
$dt = strtotime($new['begin']);
$new['begin'] = date($oo::MYSQL_DATE_FMT, $dt);
}

if(!empty($new['end']))
{
$dt = strtotime($new['end']);
$new['end'] = date($oo::MYSQL_DATE_FMT, $dt);
}

// make mysql happy with nulls and such
foreach($new as $key => $value)
{
if($value)
$new[$key] = "'".$value."'";
else
$new[$key] = "null";
}

$id = $oo->insert($new);

// need to strip out the quotes that were added to appease sql
$u = str_replace("'", "", $new['url']);
$url = valid_url($u, strval($id), $s_urls);
if($url != $u)
{
$new['url'] = "'".$url."'";
$oo->update($id, $new);
}

return $id;
}

?><div id="body-container">
<div id="body" class="centre"><?
// TODO: this code is duplicated in
Expand Down
Loading

0 comments on commit 929e734

Please sign in to comment.