CAFPE Docs
  • Class

Classes

  • Article_model
  • Articles
  • Auth
  • Auth_controller
  • Bcrypt
  • Hierarchical_controllers
  • Ion_auth
  • Ion_auth_model
  • MY_Controller
  • MY_Lang
  • MY_Model
  • Pages
  • Private_pages
  • Public_controller
  • Seeder
  • Slug
  • User_sessions
  • Users

Functions

  • action_result
  • actions_widget
  • admin_area_buttons
  • base64_current_url_encode
  • base64_url_decode
  • base64_url_encode
  • current_lang
  • jscript_tag
  • lang_switcher
  • logged_in
  • redirect_lang
  • site_url_lang
  • style_tag
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 
<?php  defined('BASEPATH') or exit('No direct script access allowed');
/**
 * CodeIgniter Slug Library
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the Academic Free License version 3.0
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * http://opensource.org/licenses/AFL-3.0
 *
 * @package     CodeIgniter
 * @author      Eric Barnes
 * @copyright   Copyright (c) Eric Barnes (http://ericlbarnes.com)
 * @license     http://opensource.org/licenses/AFL-3.0 Academic Free License (AFL 3.0)
 * @link        http://code.ericlbarnes.com
 * @filesource
 *
 * Added modifications to be able to use the library with different table fields
 */

// ------------------------------------------------------------------------

/**
 * Slug Library
 *
 * Nothing but legless, boneless creatures who are responsible for creating
 * magic "friendly urls" in your CodeIgniter application. Slugs are nocturnal
 * feeders, hiding during daylight hours.
 *
 * @subpackage Libraries
 */
class Slug
{
    /**
     * The name of the table
     *
     * @var string
     */
    public $table = '';

    /**
     * The primary id field in the table
     *
     * @var string
     */
    public $id = 'id';

    /**
     * The replacement (Either underscore or dash)
     *
     * @var string
     */
    public $replacement = 'dash';

    // ------------------------------------------------------------------------

    /**
     * Setup all vars
     *
     * @param array $config
     * @return void
     */
    public function __construct($config = array())
    {
        $this->set_config($config);
        log_message('debug', 'Slug Class Initialized');
    }

    // ------------------------------------------------------------------------

    /**
     * Manually Set Config
     *
     * Pass an array of config vars to override previous setup
     *
     * @param   array
     * @return  void
     */
    public function set_config($config = array())
    {
        if (! empty($config)) {
            foreach ($config as $key => $value) {
                $this->{$key} = $value;
            }
        }
    }

    // ------------------------------------------------------------------------

    /**
     * Create a uri string
     *
     * This wraps into the _check_uri method to take a character
     * string and convert into ascii characters.
     *
     * @param   string
     * @uses    Slug::_check_uri()
     * @uses    Slug::create_slug()
     * @return  string
     */
    public function create_uri($title = '', $field = 'uri')
    {
        if (empty($title)) {
            return null;
        }

        return $this->_check_uri($this->create_slug($title), $field);
    }

    // ------------------------------------------------------------------------

    /**
     * Create Slug
     *
     * Returns a string with all spaces converted to underscores (by default), accented
     * characters converted to non-accented characters, and non word characters removed.
     *
     * @param   string $string the string you want to slug
     * @param   string $replacement will replace keys in map
     * @return  string
     */
    public function create_slug($string)
    {
        $CI =& get_instance();
        $CI->load->helper(array('url', 'text', 'string'));
        $string = strtolower(url_title(convert_accented_characters($string), $this->replacement));
        return reduce_multiples($string, $this->_get_replacement(), true);
    }

    // ------------------------------------------------------------------------

    /**
     * Check URI
     *
     * Checks other items for the same uri and if something else has it
     * change the name to "name-1".
     *
     * @param   string $uri
     * @param   int $count
     * @return  string
     */
    private function _check_uri($uri, $field, $count = 0)
    {
        $CI =& get_instance();
        $new_uri = ($count > 0) ? $uri.$this->_get_replacement().$count : $uri;

        // Setup the query
        $CI->db->select($field)->where($field, $new_uri);

        if ($CI->db->count_all_results($this->table) > 0) {
            return $this->_check_uri($uri, $field, ++$count);
        } else {
            return $new_uri;
        }
    }

    // ------------------------------------------------------------------------

    /**
     * Get the replacement type
     *
     * Either a dash or underscore generated off the term.
     *
     * @return string
     */
    private function _get_replacement()
    {
        return ($this->replacement === 'dash') ? '-' : '_';
    }
}
CAFPE Docs API documentation generated by ApiGen