04.12 php saved
mariano_iglesias
Tags add more
 
Note
Modified version of the Slug Behavior that uses more than one field as label to be used when generating a slug.
  1. <?php
  2.  /**
  3. * Slug Behavior class file.
  4. *
  5. * Model Behavior to support slugs.
  6. *
  7. * @filesource
  8. * @package    app
  9. * @subpackage    models.behaviors
  10. */
  11.  
  12. /**
  13. * Add slug behavior to a model.
  14. * @author    Mariano Iglesias
  15. * @package    app
  16. * @subpackage    models.behaviors
  17. */
  18. class SlugBehavior extends ModelBehavior {
  19.     /**
  20.      * Initiate behaviour for the model using specified settings.
  21.      *
  22.      * @param object $model    Model using the behaviour
  23.      * @param array $settings    Settings to override for model.
  24.      *
  25.      * @access public
  26.      */
  27.     function setup(&$model, $settings = array()) {
  28.         $default = array( 'label' => array('title'), 'slug' => 'slug', 'separator' => '-', 'length' => 100, 'overwrite' => false );
  29.          
  30.         if (!isset($this->settings[$model->name])) {
  31.             $this->settings[$model->name] = $default;
  32.         }
  33.        
  34.         if (isset($settings) && is_array($settings) && isset($settings['label']) && !is_array($settings['label'])) {
  35.             $settings['label'] = array( $settings['label'] );
  36.         }
  37.          
  38.         $this->settings[$model->name] = array_merge($this->settings[$model->name], ife(is_array($settings), $settings, array()));
  39.     }
  40.      
  41.     /**
  42.      * Run before a model is saved, used to set up slug for model.
  43.      *
  44.      * @param object $model    Model about to be saved.
  45.      *
  46.      * @access public
  47.      * @since 1.0
  48.      */
  49.     function beforeSave(&$model) {
  50.             foreach($this->settings[$model->name]['label'] as $field) {
  51.                 if (!$model->hasField($field)) {
  52.                     return;
  53.                 }
  54.             }
  55.            
  56.         if ($model->hasField($this->settings[$model->name]['slug'])) {
  57.             if (($this->settings[$model->name]['overwrite'] || empty($model->{$model->primaryKey}))) {
  58.                 $label = '';
  59.                
  60.                 foreach($this->settings[$model->name]['label'] as $field) {
  61.                     $label .= ife(!empty($label), ' ', '');
  62.                     $label .= $model->data[$model->name][$field];
  63.                 }
  64.                
  65.                 if (empty($label)) {
  66.                     return;
  67.                 }
  68.                
  69.                 $slug = $this->_slug($label, $this->settings[$model->name]);
  70.                  
  71.                 $conditions = array($model->name . '.' . $this->settings[$model->name]['slug'] => 'LIKE ' . $slug . '%');
  72.                  
  73.                 if (!empty($model->{$model->primaryKey})) {
  74.                     $conditions[$model->name . '.' . $model->primaryKey] = '!= ' . $model->{$model->primaryKey};
  75.                 }
  76.                  
  77.                 $result = $model->findAll($conditions, $model->name . '.*', null, null, 1, 0);
  78.                  
  79.                 if ($result !== false && count($result) > 0) {
  80.                     $slugs = Set::extract($result, '{n}.' . $this->settings[$model->name]['slug']);
  81.                 }
  82.              
  83.                 if (isset($slugs) && count($slugs) > 0) {
  84.                     $begginingSlug = $slug;
  85.                     $index = 1;
  86.              
  87.                     while($index > 0) {
  88.                         if (!in_array($begginingSlug . $this->settings[$model->name]['separator'] . $index, $slugs)) {
  89.                             $slug = $begginingSlug . $this->settings[$model->name]['separator'] . $index;
  90.                             $index = -1;
  91.                         }
  92.                         $index++;
  93.                     }
  94.                 }
  95.                  
  96.                 $model->data[$model->name][$this->settings[$model->name]['slug']] = $slug;
  97.             }
  98.         }
  99.     }
  100.      
  101.     /**
  102.      * Generate a slug for the given strung using specified settings.
  103.      *
  104.      * @param string $string    String.
  105.      * @param array $settings    Settings to use (looks for 'separator' and 'length')
  106.      *
  107.      * @return string    Slug for given string.
  108.      *
  109.      * @access private
  110.      */
  111.     function _slug($string, $settings) {
  112.         $string = strtolower($string);
  113.          
  114.         $string = preg_replace('/[^a-z0-9' . $settings['separator'] . ']/i', $settings['separator'], $string);
  115.         $string = preg_replace('/' . $settings['separator'] . '[' . $settings['separator'] . ']*/', $settings['separator'], $string);
  116.          
  117.         if (strlen($string) > $settings['length']) {
  118.             $string = substr($string, 0, $settings['length']);
  119.         }
  120.          
  121.         $string = preg_replace('/' . $settings['separator'] . '$/', '', $string);
  122.         $string = preg_replace('/^' . $settings['separator'] . '/', '', $string);
  123.          
  124.         return $string;
  125.     }
  126. }
  127. ?>
Parsed in 0.227 seconds, using GeSHi 1.0.7.14

Modify this Paste