Created
September 3, 2013 23:50
-
-
Save danott/6431122 to your computer and use it in GitHub Desktop.
Old CodeIgniter libraries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* Additional custom CodeIgniter Inflector Helpers | |
* | |
* @package CodeIgniter | |
* @subpackage Helpers | |
* @category Helpers | |
* @author Daniel Ott | |
* @link http://danott.us | |
*/ | |
// ------------------------------------------------------------------------ | |
/** | |
* Return the Twitter-style "relative time" | |
* | |
* Returns time() or its GMT equivalent based on the config file preference | |
* | |
* @access public | |
* @param integer Unix timestamp | |
* @return string | |
* @todo Add future date support with "seconds away", "days away", etc. | |
*/ | |
if ( ! function_exists('relative_time')) | |
{ | |
function relative_time($then) | |
{ | |
// Need the date helper for the now() function | |
$CI =& get_instance(); | |
$CI->load->helper('date'); | |
$diff = now() - strtotime($then); | |
// If then is before now, $diff will be positive, and $then is in the past | |
// If now is before then, $diff will be negative, and $then is in the future | |
$direction = ($diff > 0) ? "ago" : "away"; | |
$diff = abs($diff); | |
// It's a matter of seconds! | |
if ($diff<60) | |
return $diff . " " . pluralize("second", $diff) . " " . $direction; | |
// It's a matter of minutes! | |
$diff = round($diff/60); | |
if ($diff<60) | |
return $diff . " " . pluralize("minute", $diff) . " " . $direction; | |
// It's a matter of hours! | |
$diff = round($diff/60); | |
if ($diff<24) | |
return $diff . " " . pluralize("hour", $diff) . " " . $direction; | |
// It's a matter of days! | |
$diff = round($diff/24); | |
if ($diff<7) | |
return $diff . " " . pluralize("day", $diff) . " " . $direction; | |
// It's a matter of weeks! | |
$diff = round($diff/7); | |
if ($diff<4) | |
return $diff . " " . pluralize("week", $diff) . " " . $direction; | |
// Forget about months and years, just say the date. | |
return "on " . date("F j, Y", strtotime($then)); | |
} | |
} | |
/** | |
* Pluralize a singular word | |
* | |
* CodeIgniter's built in inflector gives us a plural() function. | |
* This is simply a wrapper around that, that does the actual pluralization | |
* based on a passed in parameter of how many we're dealing with. | |
* | |
* @access public | |
* @param string | |
* @param integer | |
* @param string | |
* @return string | |
*/ | |
if( ! function_exists('pluralize')) | |
{ | |
function pluralize($singular_word, $how_many = 2, $irregular_plural = null) | |
{ | |
return ($how_many == 1) ? $singular_word : ((isset($irregular_plural)) ? $irregular_plural : plural($singular_word)); | |
} | |
} | |
/* End of file MY_inflector_helper.php */ | |
/* Location: ./system/application/helpers/MY_inflector_helper.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* CodeIgniter MY_Loader Class | |
* | |
* @package CodeIgniter | |
* @subpackage Libraries | |
* @category Libraries | |
* @author Daniel Ott | |
* @version 0.1 | |
* @link http://codeigniter.com/wiki/Unobtrusive_XHR/ | |
* | |
* In a default implementation of CodeIgniter, just drop this file in | |
* ~/system/application/libraries/, and it will be completely ready to work. | |
*/ | |
/** | |
* We'll use one file extension globally. If someone has other needs they can | |
* change this or build on the library. | |
*/ | |
if (!defined('XHR_EXTENSION')) define('XHR_EXTENSION', 'json'); | |
class MY_Loader extends CI_Loader { | |
function MY_Loader() { | |
parent::CI_Loader(); | |
/** | |
* Set an internal variable to know if the current controller request | |
* was made with XmlHttpRequest. | |
* Found at: http://codeigniter.com/forums/viewthread/59450/#292979 | |
*/ | |
$this->is_xhr = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"); | |
} | |
/** | |
* This function has been modified in MY_Loader to automatically load the | |
* JSON version of a view, if the request was made through XMLHTTPRequest. | |
* Example $this->load->view('home/create_event') would originally load | |
* home/create_event.php, now it would open home/create_event.json.php | |
* | |
* @access public | |
* @param string | |
* @param array | |
* @param bool | |
* @param bool | |
* @return void | |
* @todo Optional fourth parameter that would override this functionality | |
*/ | |
function view($view, $vars = array(), $return = FALSE, $ignore_xhr = FALSE) | |
{ | |
// Only overloading things that are requested with XmlHttpRequest | |
if($this->is_xhr && !$ignore_xhr) | |
{ | |
/** | |
* Build the modified view's path in the most forgiving way. This | |
* will create the correct path if the .php extension is included | |
* or left out of the original call to $this->load->view() | |
*/ | |
$modified_view = pathinfo($view, PATHINFO_DIRNAME) . "/" . pathinfo($view, PATHINFO_FILENAME) . "." . XHR_EXTENSION . ".php"; | |
/** | |
* Overloading the view will only take place if the new view file | |
* actually exists. Haven't tested this extensively in the field, | |
* may have to cut this part if there are performance hits. | |
*/ | |
if( file_exists($this->_ci_view_path . $modified_view) ) | |
{ | |
log_message('debug', 'My_Loader detected XHR and a corresponding template. Now loading view: ' . $modified_view); | |
$view = $modified_view; | |
} | |
} | |
// Call as usual, the original or modified view. | |
return parent::view($view, $vars, $return); | |
} | |
} | |
/* End of file MY_Loader.php */ | |
/* Location: ./system/application/libraries/My_Loader.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment