Example #1
0
 function _slug($field)
 {
     if (!empty($this->slug) && $this->slug !== '__generate__') {
         return true;
     }
     $this->load->helper(array('url', 'text', 'string'));
     $slug = reduce_multiples(strtolower(url_title(convert_accented_characters($this->title), 'dash')), '-', true);
     if (empty($slug)) {
         $t = new Category();
         $max = $t->select_max('id')->get();
         $slug = $max->id + 1;
     }
     if (is_numeric($slug)) {
         $slug = "{$slug}-1";
     }
     $s = new Slug();
     // Need to lock the table here to ensure that requests arriving at the same time
     // still get unique slugs
     if ($this->has_db_permission('lock tables')) {
         $this->db->query("LOCK TABLE {$s->table} WRITE");
         $locked = true;
     } else {
         $locked = false;
     }
     while ($s->where('id', "category.{$slug}")->count() > 0) {
         $slug = increment_string($slug, '-');
     }
     $this->db->query("INSERT INTO {$s->table}(id) VALUES ('category.{$slug}')");
     if ($locked) {
         $this->db->query('UNLOCK TABLES');
     }
     $this->slug = $slug;
 }