Doctrine 1.2.4
Doctrine_Template_Listener_Sluggable Class Reference

Inherits Doctrine_Record_Listener.

Public Member Functions

 __construct (array $options)
 
 getOption ($name)
 
 getOptions ()
 
 getUniqueSlug ($record, $slugFromFields)
 
 preInsert (Doctrine_Event $event)
 
 preUpdate (Doctrine_Event $event)
 
 setOption ($name, $value=null)
 

Protected Member Functions

 buildSlugFromFields ($record)
 
 buildSlugFromSlugField ($record)
 

Detailed Description

Definition at line 33 of file Sluggable.php.

Constructor & Destructor Documentation

Doctrine_Template_Listener_Sluggable::__construct ( array  $options)

__construct

Parameters
string$array
Returns
void

Definition at line 48 of file Sluggable.php.

{
$this->_options = $options;
}

Member Function Documentation

Doctrine_Template_Listener_Sluggable::buildSlugFromFields (   $record)
protected

Generate the slug for a given Doctrine_Record based on the configured options

Parameters
Doctrine_Record$record
Returns
string $slug

Definition at line 102 of file Sluggable.php.

{
if (empty($this->_options['fields'])) {
if (is_callable($this->_options['provider'])) {
$value = call_user_func($this->_options['provider'], $record);
} else if (method_exists($record, 'getUniqueSlug')) {
$value = $record->getUniqueSlug($record);
} else {
$value = (string) $record;
}
} else {
$value = '';
foreach ($this->_options['fields'] as $field) {
$value .= $record->$field . ' ';
}
$value = substr($value, 0, -1);
}
if ($this->_options['unique'] === true) {
return $this->getUniqueSlug($record, $value);
}
return call_user_func_array($this->_options['builder'], array($value, $record));
}
Doctrine_Template_Listener_Sluggable::buildSlugFromSlugField (   $record)
protected

Generate the slug for a given Doctrine_Record slug field

Parameters
Doctrine_Record$record
Returns
string $slug

Definition at line 133 of file Sluggable.php.

{
$name = $record->getTable()->getFieldName($this->_options['name']);
$value = $record->$name;
if ($this->_options['unique'] === true) {
return $this->getUniqueSlug($record, $value);
}
return call_user_func_array($this->_options['builder'], array($value, $record));
}
Doctrine_Record_Listener::getOption (   $name)
inherited

getOption returns the value of given option

Parameters
string$namethe name of the option
Returns
mixed the value of given option

Definition at line 74 of file Listener.php.

{
if (isset($this->_options[$name])) {
return $this->_options[$name];
}
return null;
}
Doctrine_Record_Listener::getOptions ( )
inherited

getOptions returns all options of this template and the associated values

Returns
array all options and their values

Definition at line 62 of file Listener.php.

{
return $this->_options;
}
Doctrine_Template_Listener_Sluggable::getUniqueSlug (   $record,
  $slugFromFields 
)

Creates a unique slug for a given Doctrine_Record. This function enforces the uniqueness by incrementing the values with a postfix if the slug is not unique

Parameters
Doctrine_Record$record
string$slugFromFields
Returns
string $slug

Definition at line 153 of file Sluggable.php.

{
/* fix for use with Column Aggregation Inheritance */
if ($record->getTable()->getOption('inheritanceMap')) {
$parentTable = $record->getTable()->getOption('parents');
$i = 0;
// Be sure that you do not instanciate an abstract class;
$reflectionClass = new ReflectionClass($parentTable[$i]);
while ($reflectionClass->isAbstract()) {
$i++;
$reflectionClass = new ReflectionClass($parentTable[$i]);
}
$table = Doctrine_Core::getTable($parentTable[$i]);
} else {
$table = $record->getTable();
}
$name = $table->getFieldName($this->_options['name']);
$proposal = call_user_func_array($this->_options['builder'], array($slugFromFields, $record));
$slug = $proposal;
$whereString = 'r.' . $name . ' LIKE ?';
$whereParams = array($proposal.'%');
if ($record->exists()) {
$identifier = $record->identifier();
$whereString .= ' AND r.' . implode(' != ? AND r.', $table->getIdentifierColumnNames()) . ' != ?';
$whereParams = array_merge($whereParams, array_values($identifier));
}
foreach ($this->_options['uniqueBy'] as $uniqueBy) {
if (is_null($record->$uniqueBy)) {
$whereString .= ' AND r.'.$uniqueBy.' IS NULL';
} else {
$whereString .= ' AND r.'.$uniqueBy.' = ?';
$value = $record->$uniqueBy;
if ($value instanceof Doctrine_Record) {
$value = current((array) $value->identifier());
}
$whereParams[] = $value;
}
}
// Disable indexby to ensure we get all records
$originalIndexBy = $table->getBoundQueryPart('indexBy');
$table->bindQueryPart('indexBy', null);
$query = $table->createQuery('r')
->select('r.' . $name)
->where($whereString , $whereParams)
->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
// We need to introspect SoftDelete to check if we are not disabling unique records too
if ($table->hasTemplate('Doctrine_Template_SoftDelete')) {
$softDelete = $table->getTemplate('Doctrine_Template_SoftDelete');
// we have to consider both situations here
if ($softDelete->getOption('type') == 'boolean') {
$conn = $query->getConnection();
$query->addWhere(
'(r.' . $softDelete->getOption('name') . ' = ' . $conn->convertBooleans(true) .
' OR r.' . $softDelete->getOption('name') . ' = ' . $conn->convertBooleans(false) . ')'
);
} else {
$query->addWhere('(r.' . $softDelete->getOption('name') . ' IS NOT NULL OR r.' . $softDelete->getOption('name') . ' IS NULL)');
}
}
$similarSlugResult = $query->execute();
$query->free();
// Change indexby back
$table->bindQueryPart('indexBy', $originalIndexBy);
$similarSlugs = array();
foreach ($similarSlugResult as $key => $value) {
$similarSlugs[$key] = strtolower($value[$name]);
}
$i = 1;
while (in_array(strtolower($slug), $similarSlugs)) {
$slug = call_user_func_array($this->_options['builder'], array($proposal.'-'.$i, $record));
$i++;
}
// If slug is longer then the column length then we need to trim it
// and try to generate a unique slug again
$length = $table->getFieldLength($this->_options['name']);
if (strlen($slug) > $length) {
$slug = substr($slug, 0, $length - (strlen($i) + 1));
$slug = $this->getUniqueSlug($record, $slug);
}
return $slug;
}
Doctrine_Template_Listener_Sluggable::preInsert ( Doctrine_Event  $event)

Set the slug value automatically when a record is inserted

Parameters
Doctrine_Event$event
Returns
void

Definition at line 59 of file Sluggable.php.

{
$record = $event->getInvoker();
$name = $record->getTable()->getFieldName($this->_options['name']);
if ( ! $record->$name) {
$record->$name = $this->buildSlugFromFields($record);
}
}
Doctrine_Template_Listener_Sluggable::preUpdate ( Doctrine_Event  $event)

Set the slug value automatically when a record is updated if the options are configured to allow it

Parameters
Doctrine_Event$event
Returns
void

Definition at line 76 of file Sluggable.php.

{
if (false !== $this->_options['unique']) {
$record = $event->getInvoker();
$name = $record->getTable()->getFieldName($this->_options['name']);
if ( ! $record->$name || (
false !== $this->_options['canUpdate'] &&
! array_key_exists($name, $record->getModified())
)) {
$record->$name = $this->buildSlugFromFields($record);
} else if ( ! empty($record->$name) &&
false !== $this->_options['canUpdate'] &&
array_key_exists($name, $record->getModified()
)) {
$record->$name = $this->buildSlugFromSlugField($record);
}
}
}
Doctrine_Record_Listener::setOption (   $name,
  $value = null 
)
inherited

setOption sets an option in order to allow flexible listener

Parameters
mixed$namethe name of the option to set
mixed$valuethe value of the option

Definition at line 47 of file Listener.php.

{
if (is_array($name)) {
$this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $name);
} else {
$this->_options[$name] = $value;
}
}

The documentation for this class was generated from the following file: