/** * Will format the date in tables as per our wishes. * Will leave date unchanged if $dateFormat not recognized * * @param string $date Date in 'standard' format YYYY-MM-DD * @param string $dateFormat Target formatting to use (YYYY-MM-DD, DD-MM-YYYY, D MONTH YYYY, RSS) * @return string Formatted date */ public static function date($date, $dateFormat) { // check if input date is valid if (Fari_Filter::isDate($date)) { // split into params list($year, $month, $day) = preg_split('/[-\\.\\/ ]/', $date); // else return input } else { return $date; } switch ($dateFormat) { case 'DD-MM-YYYY': return $day . '-' . $month . '-' . $year; break; case 'D MONTH YYYY': // get month's name $month = date('F', mktime(0, 0, 0, $month, 1)); // make a nice day formatting, 9th, 10th etc. if ($day < 10) { $day = substr($day, 1, 1); } return $day . ' ' . $month . ' ' . $year; break; case 'RSS': return date(DATE_RSS, mktime(0, 0, 0, $month, $day, $year)); break; // for unknown formats or default, just return // for unknown formats or default, just return default: return $date; } }
public function index($param) { // are we saving? if ($_POST) { $success = TRUE; // save categories, sources & types $category = Fari_Escape::text($_POST['category']); $categorySlug = Fari_Escape::slug($category); $source = Fari_Escape::text($_POST['source']); $sourceSlug = Fari_Escape::slug($source); $type = Fari_Escape::text($_POST['type']); $typeSlug = Fari_Escape::slug($type); if (empty($category)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $category, 'type' => 'category')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $category, 'slug' => $categorySlug, 'type' => 'category')); } } if (empty($source)) { Fari_Message::fail('The source can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $source, 'type' => 'source')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $source, 'slug' => $sourceSlug, 'type' => 'source')); } } if (empty($type)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $type, 'type' => 'type')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $type, 'type' => 'type')); } } if ($success) { $title = Fari_Escape::text($_POST['title']); if (empty($title)) { Fari_Message::fail('The title can\'t be empty.'); } else { $slug = Fari_Escape::slug($_POST['title']); // unique slug/title $result = Fari_Db::selectRow('kb', 'id', array('slug' => $slug)); if (!empty($result)) { Fari_Message::fail('The title is not unique.'); } else { $text = Fari_Escape::quotes($_POST['textarea']); // convert title & main text to its stems and add lowercase originals better matches) $titleStems = Knowledge::stems($title) . ' ' . strtolower($title); $stems = Knowledge::stems($text) . ' ' . strtolower($text); $tags = Fari_Escape::text($_POST['tags']); $category = Fari_Escape::text($_POST['category']); $source = Fari_Escape::text($_POST['source']); $type = Fari_Escape::text($_POST['type']); $comments = Fari_Escape::text($_POST['comments']); $date = Fari_Escape::text($_POST['date']); // date if (!Fari_Filter::isDate($date)) { Fari_Message::fail('The date is not in the correct format.'); } else { // INSERT Fari_Db::insert('kb', array('title' => $title, 'slug' => $slug, 'text' => $text, 'tags' => $tags, 'category' => $category, 'categorySlug' => $categorySlug, 'source' => $source, 'sourceSlug' => $sourceSlug, 'type' => $type, 'stems' => $stems, 'comments' => $comments, 'date' => $date, 'titleStems' => $titleStems, 'starred' => 'empty')); Fari_Message::success('Saved successfully.'); $this->redirect('/text/edit/' . $slug); die; } } } } } // fetch categories, sources & types $this->view->categories = $categories = Fari_Db::select('hierarchy', 'key, value', array('type' => 'category'), 'slug ASC'); $this->view->sources = $sources = Fari_Db::select('hierarchy', 'key, value', array('type' => 'source'), 'slug ASC'); $this->view->types = $types = Fari_Db::select('hierarchy', 'key, value', array('type' => 'type'), 'value ASC'); // form if save failed... $this->view->saved = $_POST; // get all messages $this->view->messages = Fari_Message::get(); $this->view->display('new'); }
/** * Convert a time to distance from now. * * @param string $time A timestamp of a date (or convert into one from YYYY-MM-DD) * @return string A formatted string of a date from now, e.g.: '3 days ago' */ public static function age($time) { // convert YYYY-MM-DD into a timestamp if (Fari_Filter::isDate($time)) { list($year, $month, $day) = preg_split('/[-\\.\\/ ]/', $time); $time = mktime('1', '1', '1', $month, $day, $year); } // time now $now = time(); // the difference $difference = $now - $time; // in the past? $ago = $difference > 0 ? 1 : 0; // absolute value $difference = abs($difference); // switch case textual difference switch ($difference) { case $difference < 60: $result = 'less than a minute'; break; case $difference < 60 * 2: $result = '2 minutes'; break; case $difference < 60 * 3: $result = '3 minutes'; break; case $difference < 60 * 4: $result = '4 minutes'; break; case $difference < 60 * 5: $result = '5 minutes'; break; case $difference < 60 * 10: $result = '10 minutes'; break; case $difference < 60 * 15: $result = '15 minutes'; break; case $difference < 60 * 20: $result = '20 minutes'; break; case $difference < 60 * 25: $result = '25 minutes'; break; case $difference < 60 * 30: $result = 'half an hour'; break; case $difference < 60 * 40: $result = '40 minutes'; break; case $difference < 60 * 50: $result = '50 minutes'; break; case $difference < 60 * 60: $result = 'an hour'; break; case $difference < 60 * 60 * 24: $result = 'a day'; break; case $difference < 60 * 60 * 24 * 7: $result = 'a week'; break; case $difference < 60 * 60 * 24 * 7 * 2: $result = 'two weeks'; break; case $difference < 60 * 60 * 24 * 7 * 3: $result = 'three weeks'; break; case $difference < 60 * 60 * 24 * 30: $result = 'a month'; break; case $difference < 60 * 60 * 24 * 60: $result = 'two months'; break; case $difference < 60 * 60 * 24 * 90: $result = 'three months'; break; case $difference < 60 * 60 * 24 * 120: $result = 'four months'; break; case $difference < 60 * 60 * 24 * 182: $result = 'half a year'; break; case $difference < 60 * 60 * 24 * 365: $result = 'a year'; break; case $difference < 60 * 60 * 24 * 365 * 2: $result = 'two years'; break; case $difference < 60 * 60 * 24 * 365 * 3: $result = 'three years'; break; case $difference < 60 * 60 * 24 * 365 * 4: $result = 'four years'; break; case $difference < 60 * 60 * 24 * 365 * 5: $result = 'five years'; break; case $difference < 60 * 60 * 24 * 365 * 6: $result = 'six years'; break; case $difference < 60 * 60 * 24 * 365 * 7: $result = 'seven years'; break; case $difference < 60 * 60 * 24 * 365 * 10: $result = 'a decade'; break; case $difference < 60 * 60 * 24 * 365 * 20: $result = 'two decades'; break; case $difference < 60 * 60 * 24 * 365 * 30: $result = 'three decades'; break; case $difference < 60 * 60 * 24 * 365 * 40: $result = 'four decades'; break; case $difference < 60 * 60 * 24 * 365 * 50: $result = 'half a century'; break; case $difference < 60 * 60 * 24 * 365 * 100: $result = 'a century'; break; default: $result = 'more than a century'; break; } return $ago ? $result . ' ago' : 'in ' . $result; }
public function edit($slug) { $slug = Fari_Escape::text($slug); // are we saving? if ($_POST) { $success = TRUE; // save categories, sources & types $category = Fari_Escape::text($_POST['category']); $categorySlug = Fari_Escape::slug($category); $source = Fari_Escape::text($_POST['source']); $sourceSlug = Fari_Escape::slug($source); $type = Fari_Escape::text($_POST['type']); $typeSlug = Fari_Escape::slug($type); if (empty($category)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $category, 'type' => 'category')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $category, 'slug' => $categorySlug, 'type' => 'category')); } } if (empty($source)) { Fari_Message::fail('The source can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $source, 'type' => 'source')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $source, 'slug' => $sourceSlug, 'type' => 'source')); } } if (empty($type)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $type, 'type' => 'type')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $type, 'type' => 'type')); } } if ($success) { $text = Fari_Escape::quotes($_POST['textarea']); // convert main text to stems & add the lowercase original to it (better matches) $stems = Knowledge::stems($text) . ' ' . strtolower($text); $tags = Fari_Escape::text($_POST['tags']); $category = Fari_Escape::text($_POST['category']); $source = Fari_Escape::text($_POST['source']); $type = Fari_Escape::text($_POST['type']); $comments = Fari_Escape::text($_POST['comments']); $date = Fari_Escape::text($_POST['date']); // date if (!Fari_Filter::isDate($date)) { Fari_Message::fail('The date is not in the correct format.'); } else { // INSERT Fari_Db::update('kb', array('text' => $text, 'comments' => $comments, 'date' => $date, 'tags' => $tags, 'category' => $category, 'categorySlug' => $categorySlug, 'source' => $source, 'sourceSlug' => $sourceSlug, 'type' => $type, 'stems' => $stems), array('slug' => $slug)); Fari_Message::success('Saved successfully.'); } } } // fetch categories, sources & types $this->view->categories = $categories = Fari_Db::select('hierarchy', 'key, value', array('type' => 'category'), 'slug ASC'); $this->view->sources = $sources = Fari_Db::select('hierarchy', 'key, value', array('type' => 'source'), 'slug ASC'); $this->view->types = $types = Fari_Db::select('hierarchy', 'key, value', array('type' => 'type'), 'value ASC'); // form $saved = Fari_Db::selectRow('kb', '*', array('slug' => $slug)); $saved['textarea'] = $saved['text']; // for reuse... $this->view->saved = $saved; // get all messages $this->view->messages = Fari_Message::get(); $this->view->display('edit'); }