2431 lines
74 KiB
PHP
2431 lines
74 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\MediaBundle\Model\om;
|
|
|
|
use \BaseObject;
|
|
use \BasePeer;
|
|
use \Criteria;
|
|
use \DateTime;
|
|
use \Exception;
|
|
use \PDO;
|
|
use \Persistent;
|
|
use \Propel;
|
|
use \PropelCollection;
|
|
use \PropelDateTime;
|
|
use \PropelException;
|
|
use \PropelObjectCollection;
|
|
use \PropelPDO;
|
|
use Trinity\Bundle\MediaBundle\Model\Media;
|
|
use Trinity\Bundle\MediaBundle\Model\MediaQuery;
|
|
use Trinity\Bundle\MediaBundle\Model\Type;
|
|
use Trinity\Bundle\MediaBundle\Model\TypePeer;
|
|
use Trinity\Bundle\MediaBundle\Model\TypeQuery;
|
|
|
|
abstract class BaseType extends BaseObject implements Persistent
|
|
{
|
|
/**
|
|
* Peer class name
|
|
*/
|
|
const PEER = 'Trinity\\Bundle\\MediaBundle\\Model\\TypePeer';
|
|
|
|
/**
|
|
* The Peer class.
|
|
* Instance provides a convenient way of calling static methods on a class
|
|
* that calling code may not be able to identify.
|
|
* @var TypePeer
|
|
*/
|
|
protected static $peer;
|
|
|
|
/**
|
|
* The flag var to prevent infinit loop in deep copy
|
|
* @var boolean
|
|
*/
|
|
protected $startCopy = false;
|
|
|
|
/**
|
|
* The value for the id field.
|
|
* @var int
|
|
*/
|
|
protected $id;
|
|
|
|
/**
|
|
* The value for the label field.
|
|
* @var string
|
|
*/
|
|
protected $label;
|
|
|
|
/**
|
|
* The value for the icon field.
|
|
* @var string
|
|
*/
|
|
protected $icon;
|
|
|
|
/**
|
|
* The value for the thumbnail field.
|
|
* @var string
|
|
*/
|
|
protected $thumbnail;
|
|
|
|
/**
|
|
* The value for the mimes field.
|
|
* @var string
|
|
*/
|
|
protected $mimes;
|
|
|
|
/**
|
|
* The value for the created_at field.
|
|
* @var string
|
|
*/
|
|
protected $created_at;
|
|
|
|
/**
|
|
* The value for the updated_at field.
|
|
* @var string
|
|
*/
|
|
protected $updated_at;
|
|
|
|
/**
|
|
* The value for the sortable_rank field.
|
|
* @var int
|
|
*/
|
|
protected $sortable_rank;
|
|
|
|
/**
|
|
* The value for the slug field.
|
|
* @var string
|
|
*/
|
|
protected $slug;
|
|
|
|
/**
|
|
* @var PropelObjectCollection|Media[] Collection to store aggregation of Media objects.
|
|
*/
|
|
protected $collMedias;
|
|
protected $collMediasPartial;
|
|
|
|
/**
|
|
* Flag to prevent endless save loop, if this object is referenced
|
|
* by another object which falls in this transaction.
|
|
* @var boolean
|
|
*/
|
|
protected $alreadyInSave = false;
|
|
|
|
/**
|
|
* Flag to prevent endless validation loop, if this object is referenced
|
|
* by another object which falls in this transaction.
|
|
* @var boolean
|
|
*/
|
|
protected $alreadyInValidation = false;
|
|
|
|
/**
|
|
* Flag to prevent endless clearAllReferences($deep=true) loop, if this object is referenced
|
|
* @var boolean
|
|
*/
|
|
protected $alreadyInClearAllReferencesDeep = false;
|
|
|
|
// sortable behavior
|
|
|
|
/**
|
|
* Queries to be executed in the save transaction
|
|
* @var array
|
|
*/
|
|
protected $sortableQueries = array();
|
|
|
|
/**
|
|
* An array of objects scheduled for deletion.
|
|
* @var PropelObjectCollection
|
|
*/
|
|
protected $mediasScheduledForDeletion = null;
|
|
|
|
/**
|
|
* Get the [id] column value.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* Get the [label] column value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getLabel()
|
|
{
|
|
return $this->label;
|
|
}
|
|
|
|
/**
|
|
* Get the [icon] column value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getIcon()
|
|
{
|
|
return $this->icon;
|
|
}
|
|
|
|
/**
|
|
* Get the [thumbnail] column value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getThumbnail()
|
|
{
|
|
return $this->thumbnail;
|
|
}
|
|
|
|
/**
|
|
* Get the [mimes] column value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getMimes()
|
|
{
|
|
return $this->mimes;
|
|
}
|
|
|
|
/**
|
|
* Get the [optionally formatted] temporal [created_at] column value.
|
|
*
|
|
*
|
|
* @param string $format The date/time format string (either date()-style or strftime()-style).
|
|
* If format is null, then the raw DateTime object will be returned.
|
|
* @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null, and 0 if column value is 0000-00-00 00:00:00
|
|
* @throws PropelException - if unable to parse/validate the date/time value.
|
|
*/
|
|
public function getCreatedAt($format = null)
|
|
{
|
|
if ($this->created_at === null) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->created_at === '0000-00-00 00:00:00') {
|
|
// while technically this is not a default value of null,
|
|
// this seems to be closest in meaning.
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$dt = new DateTime($this->created_at);
|
|
} catch (Exception $x) {
|
|
throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->created_at, true), $x);
|
|
}
|
|
|
|
if ($format === null) {
|
|
// Because propel.useDateTimeClass is true, we return a DateTime object.
|
|
return $dt;
|
|
}
|
|
|
|
if (strpos($format, '%') !== false) {
|
|
return strftime($format, $dt->format('U'));
|
|
}
|
|
|
|
return $dt->format($format);
|
|
|
|
}
|
|
|
|
/**
|
|
* Get the [optionally formatted] temporal [updated_at] column value.
|
|
*
|
|
*
|
|
* @param string $format The date/time format string (either date()-style or strftime()-style).
|
|
* If format is null, then the raw DateTime object will be returned.
|
|
* @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null, and 0 if column value is 0000-00-00 00:00:00
|
|
* @throws PropelException - if unable to parse/validate the date/time value.
|
|
*/
|
|
public function getUpdatedAt($format = null)
|
|
{
|
|
if ($this->updated_at === null) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->updated_at === '0000-00-00 00:00:00') {
|
|
// while technically this is not a default value of null,
|
|
// this seems to be closest in meaning.
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$dt = new DateTime($this->updated_at);
|
|
} catch (Exception $x) {
|
|
throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->updated_at, true), $x);
|
|
}
|
|
|
|
if ($format === null) {
|
|
// Because propel.useDateTimeClass is true, we return a DateTime object.
|
|
return $dt;
|
|
}
|
|
|
|
if (strpos($format, '%') !== false) {
|
|
return strftime($format, $dt->format('U'));
|
|
}
|
|
|
|
return $dt->format($format);
|
|
|
|
}
|
|
|
|
/**
|
|
* Get the [sortable_rank] column value.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getSortableRank()
|
|
{
|
|
return $this->sortable_rank;
|
|
}
|
|
|
|
/**
|
|
* Get the [slug] column value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getSlug()
|
|
{
|
|
return $this->slug;
|
|
}
|
|
|
|
/**
|
|
* Set the value of [id] column.
|
|
*
|
|
* @param int $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setId($v)
|
|
{
|
|
if ($v !== null && is_numeric($v)) {
|
|
$v = (int) $v;
|
|
}
|
|
|
|
if ($this->id !== $v) {
|
|
$this->id = $v;
|
|
$this->modifiedColumns[] = TypePeer::ID;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setId()
|
|
|
|
/**
|
|
* Set the value of [label] column.
|
|
*
|
|
* @param string $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setLabel($v)
|
|
{
|
|
if ($v !== null && is_numeric($v)) {
|
|
// // $v = (string) $v;
|
|
}
|
|
|
|
if ($this->label !== $v) {
|
|
$this->label = $v;
|
|
$this->modifiedColumns[] = TypePeer::LABEL;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setLabel()
|
|
|
|
/**
|
|
* Set the value of [icon] column.
|
|
*
|
|
* @param string $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setIcon($v)
|
|
{
|
|
if (!isset($this->rollback_icon)) {
|
|
$this->rollback_icon = null;
|
|
}
|
|
|
|
$this->rollback_icon = $this->getIcon();
|
|
|
|
if ($v !== null && is_numeric($v)) {
|
|
// // $v = (string) $v;
|
|
}
|
|
|
|
if ($this->icon !== $v) {
|
|
$this->icon = $v;
|
|
$this->modifiedColumns[] = TypePeer::ICON;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setIcon()
|
|
|
|
/**
|
|
* Set the value of [thumbnail] column.
|
|
*
|
|
* @param string $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setThumbnail($v)
|
|
{
|
|
if (!isset($this->rollback_thumbnail)) {
|
|
$this->rollback_thumbnail = null;
|
|
}
|
|
|
|
$this->rollback_thumbnail = $this->getThumbnail();
|
|
|
|
if ($v !== null && is_numeric($v)) {
|
|
// // $v = (string) $v;
|
|
}
|
|
|
|
if ($this->thumbnail !== $v) {
|
|
$this->thumbnail = $v;
|
|
$this->modifiedColumns[] = TypePeer::THUMBNAIL;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setThumbnail()
|
|
|
|
/**
|
|
* Set the value of [mimes] column.
|
|
*
|
|
* @param string $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setMimes($v)
|
|
{
|
|
if ($v !== null && is_numeric($v)) {
|
|
// // $v = (string) $v;
|
|
}
|
|
|
|
if ($this->mimes !== $v) {
|
|
$this->mimes = $v;
|
|
$this->modifiedColumns[] = TypePeer::MIMES;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setMimes()
|
|
|
|
/**
|
|
* Sets the value of [created_at] column to a normalized version of the date/time value specified.
|
|
*
|
|
* @param mixed $v string, integer (timestamp), or DateTime value.
|
|
* Empty strings are treated as null.
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setCreatedAt($v)
|
|
{
|
|
$dt = PropelDateTime::newInstance($v, null, 'DateTime');
|
|
if ($this->created_at !== null || $dt !== null) {
|
|
$currentDateAsString = ($this->created_at !== null && $tmpDt = new DateTime($this->created_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
|
|
$newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
|
|
if ($currentDateAsString !== $newDateAsString) {
|
|
$this->created_at = $newDateAsString;
|
|
$this->modifiedColumns[] = TypePeer::CREATED_AT;
|
|
}
|
|
} // if either are not null
|
|
|
|
|
|
return $this;
|
|
} // setCreatedAt()
|
|
|
|
/**
|
|
* Sets the value of [updated_at] column to a normalized version of the date/time value specified.
|
|
*
|
|
* @param mixed $v string, integer (timestamp), or DateTime value.
|
|
* Empty strings are treated as null.
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setUpdatedAt($v)
|
|
{
|
|
$dt = PropelDateTime::newInstance($v, null, 'DateTime');
|
|
if ($this->updated_at !== null || $dt !== null) {
|
|
$currentDateAsString = ($this->updated_at !== null && $tmpDt = new DateTime($this->updated_at)) ? $tmpDt->format('Y-m-d H:i:s') : null;
|
|
$newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
|
|
if ($currentDateAsString !== $newDateAsString) {
|
|
$this->updated_at = $newDateAsString;
|
|
$this->modifiedColumns[] = TypePeer::UPDATED_AT;
|
|
}
|
|
} // if either are not null
|
|
|
|
|
|
return $this;
|
|
} // setUpdatedAt()
|
|
|
|
/**
|
|
* Set the value of [sortable_rank] column.
|
|
*
|
|
* @param int $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setSortableRank($v)
|
|
{
|
|
if ($v !== null && is_numeric($v)) {
|
|
$v = (int) $v;
|
|
}
|
|
|
|
if ($this->sortable_rank !== $v) {
|
|
$this->sortable_rank = $v;
|
|
$this->modifiedColumns[] = TypePeer::SORTABLE_RANK;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setSortableRank()
|
|
|
|
/**
|
|
* Set the value of [slug] column.
|
|
*
|
|
* @param string $v new value
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setSlug($v)
|
|
{
|
|
if ($v !== null && is_numeric($v)) {
|
|
// // $v = (string) $v;
|
|
}
|
|
|
|
if ($this->slug !== $v) {
|
|
$this->slug = $v;
|
|
$this->modifiedColumns[] = TypePeer::SLUG;
|
|
}
|
|
|
|
|
|
return $this;
|
|
} // setSlug()
|
|
|
|
/**
|
|
* Indicates whether the columns in this object are only set to default values.
|
|
*
|
|
* This method can be used in conjunction with isModified() to indicate whether an object is both
|
|
* modified _and_ has some values set which are non-default.
|
|
*
|
|
* @return boolean Whether the columns in this object are only been set with default values.
|
|
*/
|
|
public function hasOnlyDefaultValues()
|
|
{
|
|
// otherwise, everything was equal, so return true
|
|
return true;
|
|
} // hasOnlyDefaultValues()
|
|
|
|
/**
|
|
* Hydrates (populates) the object variables with values from the database resultset.
|
|
*
|
|
* An offset (0-based "start column") is specified so that objects can be hydrated
|
|
* with a subset of the columns in the resultset rows. This is needed, for example,
|
|
* for results of JOIN queries where the resultset row includes columns from two or
|
|
* more tables.
|
|
*
|
|
* @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
|
|
* @param int $startcol 0-based offset column which indicates which restultset column to start with.
|
|
* @param boolean $rehydrate Whether this object is being re-hydrated from the database.
|
|
* @return int next starting column
|
|
* @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
|
|
*/
|
|
public function hydrate($row, $startcol = 0, $rehydrate = false)
|
|
{
|
|
try {
|
|
|
|
$this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
|
|
$this->label = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
|
|
$this->icon = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
|
|
$this->thumbnail = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
|
|
$this->mimes = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
|
|
$this->created_at = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
|
|
$this->updated_at = ($row[$startcol + 6] !== null) ? (string) $row[$startcol + 6] : null;
|
|
$this->sortable_rank = ($row[$startcol + 7] !== null) ? (int) $row[$startcol + 7] : null;
|
|
$this->slug = ($row[$startcol + 8] !== null) ? (string) $row[$startcol + 8] : null;
|
|
$this->resetModified();
|
|
|
|
$this->setNew(false);
|
|
|
|
if ($rehydrate) {
|
|
$this->ensureConsistency();
|
|
}
|
|
$this->postHydrate($row, $startcol, $rehydrate);
|
|
return $startcol + 9; // 9 = TypePeer::NUM_HYDRATE_COLUMNS.
|
|
|
|
} catch (Exception $e) {
|
|
throw new PropelException("Error populating Type object", $e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks and repairs the internal consistency of the object.
|
|
*
|
|
* This method is executed after an already-instantiated object is re-hydrated
|
|
* from the database. It exists to check any foreign keys to make sure that
|
|
* the objects related to the current object are correct based on foreign key.
|
|
*
|
|
* You can override this method in the stub class, but you should always invoke
|
|
* the base method from the overridden method (i.e. parent::ensureConsistency()),
|
|
* in case your model changes.
|
|
*
|
|
* @throws PropelException
|
|
*/
|
|
public function ensureConsistency()
|
|
{
|
|
|
|
} // ensureConsistency
|
|
|
|
/**
|
|
* Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
|
|
*
|
|
* This will only work if the object has been saved and has a valid primary key set.
|
|
*
|
|
* @param boolean $deep (optional) Whether to also de-associated any related objects.
|
|
* @param PropelPDO $con (optional) The PropelPDO connection to use.
|
|
* @return void
|
|
* @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
|
|
*/
|
|
public function reload($deep = false, PropelPDO $con = null)
|
|
{
|
|
if ($this->isDeleted()) {
|
|
throw new PropelException("Cannot reload a deleted object.");
|
|
}
|
|
|
|
if ($this->isNew()) {
|
|
throw new PropelException("Cannot reload an unsaved object.");
|
|
}
|
|
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME, Propel::CONNECTION_READ);
|
|
}
|
|
|
|
// We don't need to alter the object instance pool; we're just modifying this instance
|
|
// already in the pool.
|
|
|
|
$stmt = TypePeer::doSelectStmt($this->buildPkeyCriteria(), $con);
|
|
$row = $stmt->fetch(PDO::FETCH_NUM);
|
|
$stmt->closeCursor();
|
|
if (!$row) {
|
|
throw new PropelException('Cannot find matching row in the database to reload object values.');
|
|
}
|
|
$this->hydrate($row, 0, true); // rehydrate
|
|
|
|
if ($deep) { // also de-associate any related objects?
|
|
|
|
$this->collMedias = null;
|
|
|
|
} // if (deep)
|
|
}
|
|
|
|
/**
|
|
* Removes this object from datastore and sets delete attribute.
|
|
*
|
|
* @param PropelPDO $con
|
|
* @return void
|
|
* @throws PropelException
|
|
* @throws Exception
|
|
* @see BaseObject::setDeleted()
|
|
* @see BaseObject::isDeleted()
|
|
*/
|
|
public function delete(PropelPDO $con = null)
|
|
{
|
|
if ($this->isDeleted()) {
|
|
throw new PropelException("This object has already been deleted.");
|
|
}
|
|
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
|
|
}
|
|
|
|
$con->beginTransaction();
|
|
try {
|
|
$deleteQuery = TypeQuery::create()
|
|
->filterByPrimaryKey($this->getPrimaryKey());
|
|
$ret = $this->preDelete($con);
|
|
// sortable behavior
|
|
|
|
TypePeer::shiftRank(-1, $this->getSortableRank() + 1, null, $con);
|
|
TypePeer::clearInstancePool();
|
|
|
|
if ($ret) {
|
|
$deleteQuery->delete($con);
|
|
$this->postDelete($con);
|
|
// uploadable behavior
|
|
$this->setThumbnail(null);
|
|
$this->setDeleteThumbnail(true);
|
|
$this->removeThumbnail();
|
|
|
|
$this->setIcon(null);
|
|
$this->setDeleteIcon(true);
|
|
$this->removeIcon();
|
|
|
|
|
|
$con->commit();
|
|
$this->setDeleted(true);
|
|
} else {
|
|
$con->commit();
|
|
}
|
|
} catch (Exception $e) {
|
|
$con->rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Persists this object to the database.
|
|
*
|
|
* If the object is new, it inserts it; otherwise an update is performed.
|
|
* All modified related objects will also be persisted in the doSave()
|
|
* method. This method wraps all precipitate database operations in a
|
|
* single transaction.
|
|
*
|
|
* @param PropelPDO $con
|
|
* @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
|
|
* @throws PropelException
|
|
* @throws Exception
|
|
* @see doSave()
|
|
*/
|
|
public function save(PropelPDO $con = null)
|
|
{
|
|
if ($this->isDeleted()) {
|
|
throw new PropelException("You cannot save an object that has been deleted.");
|
|
}
|
|
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
|
|
}
|
|
|
|
$con->beginTransaction();
|
|
$isInsert = $this->isNew();
|
|
try {
|
|
$ret = $this->preSave($con);
|
|
// sortable behavior
|
|
$this->processSortableQueries($con);
|
|
// sluggable behavior
|
|
|
|
if ($this->isColumnModified(TypePeer::SLUG) && $this->getSlug()) {
|
|
$this->setSlug($this->makeSlugUnique($this->getSlug()));
|
|
} elseif ($this->isColumnModified(TypePeer::LABEL)) {
|
|
$this->setSlug($this->createSlug());
|
|
} elseif (!$this->getSlug()) {
|
|
$this->setSlug($this->createSlug());
|
|
}
|
|
// uploadable behavior
|
|
$this->uploadThumbnail();
|
|
$this->uploadIcon();
|
|
|
|
if ($isInsert) {
|
|
$ret = $ret && $this->preInsert($con);
|
|
// timestampable behavior
|
|
if (!$this->isColumnModified(TypePeer::CREATED_AT)) {
|
|
$this->setCreatedAt(time());
|
|
}
|
|
if (!$this->isColumnModified(TypePeer::UPDATED_AT)) {
|
|
$this->setUpdatedAt(time());
|
|
}
|
|
// sortable behavior
|
|
if (!$this->isColumnModified(TypePeer::RANK_COL)) {
|
|
$this->setSortableRank(TypeQuery::create()->getMaxRank($con) + 1);
|
|
}
|
|
|
|
} else {
|
|
$ret = $ret && $this->preUpdate($con);
|
|
// timestampable behavior
|
|
if ($this->isModified() && !$this->isColumnModified(TypePeer::UPDATED_AT)) {
|
|
$this->setUpdatedAt(time());
|
|
}
|
|
}
|
|
if ($ret) {
|
|
$affectedRows = $this->doSave($con);
|
|
if ($isInsert) {
|
|
$this->postInsert($con);
|
|
// uploadable behavior
|
|
$this->saveThumbnailFile();
|
|
$this->saveIconFile();
|
|
|
|
} else {
|
|
$this->postUpdate($con);
|
|
// uploadable behavior
|
|
$this->saveThumbnailFile();
|
|
$this->saveIconFile();
|
|
|
|
}
|
|
$this->postSave($con);
|
|
// uploadable behavior
|
|
|
|
if ($this->delete_thumbnail) {
|
|
$this->removeThumbnail();
|
|
}
|
|
|
|
if ($this->delete_icon) {
|
|
$this->removeIcon();
|
|
}
|
|
|
|
TypePeer::addInstanceToPool($this);
|
|
} else {
|
|
$affectedRows = 0;
|
|
}
|
|
$con->commit();
|
|
|
|
return $affectedRows;
|
|
} catch (Exception $e) {
|
|
$con->rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Performs the work of inserting or updating the row in the database.
|
|
*
|
|
* If the object is new, it inserts it; otherwise an update is performed.
|
|
* All related objects are also updated in this method.
|
|
*
|
|
* @param PropelPDO $con
|
|
* @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
|
|
* @throws PropelException
|
|
* @see save()
|
|
*/
|
|
protected function doSave(PropelPDO $con)
|
|
{
|
|
$affectedRows = 0; // initialize var to track total num of affected rows
|
|
if (!$this->alreadyInSave) {
|
|
$this->alreadyInSave = true;
|
|
|
|
if ($this->isNew() || $this->isModified()) {
|
|
// persist changes
|
|
if ($this->isNew()) {
|
|
$this->doInsert($con);
|
|
} else {
|
|
$this->doUpdate($con);
|
|
}
|
|
$affectedRows += 1;
|
|
$this->resetModified();
|
|
}
|
|
|
|
if ($this->mediasScheduledForDeletion !== null) {
|
|
if (!$this->mediasScheduledForDeletion->isEmpty()) {
|
|
foreach ($this->mediasScheduledForDeletion as $media) {
|
|
// need to save related object because we set the relation to null
|
|
$media->save($con);
|
|
}
|
|
$this->mediasScheduledForDeletion = null;
|
|
}
|
|
}
|
|
|
|
if ($this->collMedias !== null) {
|
|
foreach ($this->collMedias as $referrerFK) {
|
|
if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
|
|
$affectedRows += $referrerFK->save($con);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->alreadyInSave = false;
|
|
|
|
}
|
|
|
|
return $affectedRows;
|
|
} // doSave()
|
|
|
|
/**
|
|
* Insert the row in the database.
|
|
*
|
|
* @param PropelPDO $con
|
|
*
|
|
* @throws PropelException
|
|
* @see doSave()
|
|
*/
|
|
protected function doInsert(PropelPDO $con)
|
|
{
|
|
$modifiedColumns = array();
|
|
$index = 0;
|
|
|
|
$this->modifiedColumns[] = TypePeer::ID;
|
|
if (null !== $this->id) {
|
|
throw new PropelException('Cannot insert a value for auto-increment primary key (' . TypePeer::ID . ')');
|
|
}
|
|
|
|
// check the columns in natural order for more readable SQL queries
|
|
if ($this->isColumnModified(TypePeer::ID)) {
|
|
$modifiedColumns[':p' . $index++] = '`id`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::LABEL)) {
|
|
$modifiedColumns[':p' . $index++] = '`label`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::ICON)) {
|
|
$modifiedColumns[':p' . $index++] = '`icon`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::THUMBNAIL)) {
|
|
$modifiedColumns[':p' . $index++] = '`thumbnail`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::MIMES)) {
|
|
$modifiedColumns[':p' . $index++] = '`mimes`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::CREATED_AT)) {
|
|
$modifiedColumns[':p' . $index++] = '`created_at`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::UPDATED_AT)) {
|
|
$modifiedColumns[':p' . $index++] = '`updated_at`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::SORTABLE_RANK)) {
|
|
$modifiedColumns[':p' . $index++] = '`sortable_rank`';
|
|
}
|
|
if ($this->isColumnModified(TypePeer::SLUG)) {
|
|
$modifiedColumns[':p' . $index++] = '`slug`';
|
|
}
|
|
|
|
$sql = sprintf(
|
|
'INSERT INTO `media_type` (%s) VALUES (%s)',
|
|
implode(', ', $modifiedColumns),
|
|
implode(', ', array_keys($modifiedColumns))
|
|
);
|
|
|
|
try {
|
|
$stmt = $con->prepare($sql);
|
|
foreach ($modifiedColumns as $identifier => $columnName) {
|
|
switch ($columnName) {
|
|
case '`id`':
|
|
$stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
|
|
break;
|
|
case '`label`':
|
|
$stmt->bindValue($identifier, $this->label, PDO::PARAM_STR);
|
|
break;
|
|
case '`icon`':
|
|
$stmt->bindValue($identifier, $this->icon, PDO::PARAM_STR);
|
|
break;
|
|
case '`thumbnail`':
|
|
$stmt->bindValue($identifier, $this->thumbnail, PDO::PARAM_STR);
|
|
break;
|
|
case '`mimes`':
|
|
$stmt->bindValue($identifier, $this->mimes, PDO::PARAM_STR);
|
|
break;
|
|
case '`created_at`':
|
|
$stmt->bindValue($identifier, $this->created_at, PDO::PARAM_STR);
|
|
break;
|
|
case '`updated_at`':
|
|
$stmt->bindValue($identifier, $this->updated_at, PDO::PARAM_STR);
|
|
break;
|
|
case '`sortable_rank`':
|
|
$stmt->bindValue($identifier, $this->sortable_rank, PDO::PARAM_INT);
|
|
break;
|
|
case '`slug`':
|
|
$stmt->bindValue($identifier, $this->slug, PDO::PARAM_STR);
|
|
break;
|
|
}
|
|
}
|
|
$stmt->execute();
|
|
} catch (Exception $e) {
|
|
Propel::log($e->getMessage(), Propel::LOG_ERR);
|
|
throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), $e);
|
|
}
|
|
|
|
try {
|
|
$pk = $con->lastInsertId();
|
|
} catch (Exception $e) {
|
|
throw new PropelException('Unable to get autoincrement id.', $e);
|
|
}
|
|
$this->setId($pk);
|
|
|
|
$this->setNew(false);
|
|
}
|
|
|
|
/**
|
|
* Update the row in the database.
|
|
*
|
|
* @param PropelPDO $con
|
|
*
|
|
* @see doSave()
|
|
*/
|
|
protected function doUpdate(PropelPDO $con)
|
|
{
|
|
$selectCriteria = $this->buildPkeyCriteria();
|
|
$valuesCriteria = $this->buildCriteria();
|
|
BasePeer::doUpdate($selectCriteria, $valuesCriteria, $con);
|
|
}
|
|
|
|
/**
|
|
* Array of ValidationFailed objects.
|
|
* @var array ValidationFailed[]
|
|
*/
|
|
protected $validationFailures = array();
|
|
|
|
/**
|
|
* Gets any ValidationFailed objects that resulted from last call to validate().
|
|
*
|
|
*
|
|
* @return array ValidationFailed[]
|
|
* @see validate()
|
|
*/
|
|
public function getValidationFailures()
|
|
{
|
|
return $this->validationFailures;
|
|
}
|
|
|
|
/**
|
|
* Validates the objects modified field values and all objects related to this table.
|
|
*
|
|
* If $columns is either a column name or an array of column names
|
|
* only those columns are validated.
|
|
*
|
|
* @param mixed $columns Column name or an array of column names.
|
|
* @return boolean Whether all columns pass validation.
|
|
* @see doValidate()
|
|
* @see getValidationFailures()
|
|
*/
|
|
public function validate($columns = null)
|
|
{
|
|
$res = $this->doValidate($columns);
|
|
if ($res === true) {
|
|
$this->validationFailures = array();
|
|
|
|
return true;
|
|
}
|
|
|
|
$this->validationFailures = $res;
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* This function performs the validation work for complex object models.
|
|
*
|
|
* In addition to checking the current object, all related objects will
|
|
* also be validated. If all pass then <code>true</code> is returned; otherwise
|
|
* an aggreagated array of ValidationFailed objects will be returned.
|
|
*
|
|
* @param array $columns Array of column names to validate.
|
|
* @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
|
|
*/
|
|
protected function doValidate($columns = null)
|
|
{
|
|
if (!$this->alreadyInValidation) {
|
|
$this->alreadyInValidation = true;
|
|
$retval = null;
|
|
|
|
$failureMap = array();
|
|
|
|
|
|
if (($retval = TypePeer::doValidate($this, $columns)) !== true) {
|
|
$failureMap = array_merge($failureMap, $retval);
|
|
}
|
|
|
|
|
|
if ($this->collMedias !== null) {
|
|
foreach ($this->collMedias as $referrerFK) {
|
|
if (!$referrerFK->validate($columns)) {
|
|
$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$this->alreadyInValidation = false;
|
|
}
|
|
|
|
return (!empty($failureMap) ? $failureMap : true);
|
|
}
|
|
|
|
/**
|
|
* Retrieves a field from the object by name passed in as a string.
|
|
*
|
|
* @param string $name name
|
|
* @param string $type The type of fieldname the $name is of:
|
|
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
|
|
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
|
|
* Defaults to BasePeer::TYPE_PHPNAME
|
|
* @return mixed Value of field.
|
|
*/
|
|
public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
|
|
{
|
|
$pos = TypePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
|
|
$field = $this->getByPosition($pos);
|
|
|
|
return $field;
|
|
}
|
|
|
|
/**
|
|
* Retrieves a field from the object by Position as specified in the xml schema.
|
|
* Zero-based.
|
|
*
|
|
* @param int $pos position in xml schema
|
|
* @return mixed Value of field at $pos
|
|
*/
|
|
public function getByPosition($pos)
|
|
{
|
|
switch ($pos) {
|
|
case 0:
|
|
return $this->getId();
|
|
break;
|
|
case 1:
|
|
return $this->getLabel();
|
|
break;
|
|
case 2:
|
|
return $this->getIcon();
|
|
break;
|
|
case 3:
|
|
return $this->getThumbnail();
|
|
break;
|
|
case 4:
|
|
return $this->getMimes();
|
|
break;
|
|
case 5:
|
|
return $this->getCreatedAt();
|
|
break;
|
|
case 6:
|
|
return $this->getUpdatedAt();
|
|
break;
|
|
case 7:
|
|
return $this->getSortableRank();
|
|
break;
|
|
case 8:
|
|
return $this->getSlug();
|
|
break;
|
|
default:
|
|
return null;
|
|
break;
|
|
} // switch()
|
|
}
|
|
|
|
/**
|
|
* Exports the object as an array.
|
|
*
|
|
* You can specify the key type of the array by passing one of the class
|
|
* type constants.
|
|
*
|
|
* @param string $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
|
|
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
|
|
* Defaults to BasePeer::TYPE_PHPNAME.
|
|
* @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true.
|
|
* @param array $alreadyDumpedObjects List of objects to skip to avoid recursion
|
|
* @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
|
|
*
|
|
* @return array an associative array containing the field names (as keys) and field values
|
|
*/
|
|
public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
|
|
{
|
|
if (isset($alreadyDumpedObjects['Type'][$this->getPrimaryKey()])) {
|
|
return '*RECURSION*';
|
|
}
|
|
$alreadyDumpedObjects['Type'][$this->getPrimaryKey()] = true;
|
|
$keys = TypePeer::getFieldNames($keyType);
|
|
$result = array(
|
|
$keys[0] => $this->getId(),
|
|
$keys[1] => $this->getLabel(),
|
|
$keys[2] => $this->getIcon(),
|
|
$keys[3] => $this->getThumbnail(),
|
|
$keys[4] => $this->getMimes(),
|
|
$keys[5] => $this->getCreatedAt(),
|
|
$keys[6] => $this->getUpdatedAt(),
|
|
$keys[7] => $this->getSortableRank(),
|
|
$keys[8] => $this->getSlug(),
|
|
);
|
|
if ($includeForeignObjects) {
|
|
if (null !== $this->collMedias) {
|
|
$result['Medias'] = $this->collMedias->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sets a field from the object by name passed in as a string.
|
|
*
|
|
* @param string $name peer name
|
|
* @param mixed $value field value
|
|
* @param string $type The type of fieldname the $name is of:
|
|
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
|
|
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
|
|
* Defaults to BasePeer::TYPE_PHPNAME
|
|
* @return void
|
|
*/
|
|
public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
|
|
{
|
|
$pos = TypePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
|
|
|
|
$this->setByPosition($pos, $value);
|
|
}
|
|
|
|
/**
|
|
* Sets a field from the object by Position as specified in the xml schema.
|
|
* Zero-based.
|
|
*
|
|
* @param int $pos position in xml schema
|
|
* @param mixed $value field value
|
|
* @return void
|
|
*/
|
|
public function setByPosition($pos, $value)
|
|
{
|
|
switch ($pos) {
|
|
case 0:
|
|
$this->setId($value);
|
|
break;
|
|
case 1:
|
|
$this->setLabel($value);
|
|
break;
|
|
case 2:
|
|
$this->setIcon($value);
|
|
break;
|
|
case 3:
|
|
$this->setThumbnail($value);
|
|
break;
|
|
case 4:
|
|
$this->setMimes($value);
|
|
break;
|
|
case 5:
|
|
$this->setCreatedAt($value);
|
|
break;
|
|
case 6:
|
|
$this->setUpdatedAt($value);
|
|
break;
|
|
case 7:
|
|
$this->setSortableRank($value);
|
|
break;
|
|
case 8:
|
|
$this->setSlug($value);
|
|
break;
|
|
} // switch()
|
|
}
|
|
|
|
/**
|
|
* Populates the object using an array.
|
|
*
|
|
* This is particularly useful when populating an object from one of the
|
|
* request arrays (e.g. $_POST). This method goes through the column
|
|
* names, checking to see whether a matching key exists in populated
|
|
* array. If so the setByName() method is called for that column.
|
|
*
|
|
* You can specify the key type of the array by additionally passing one
|
|
* of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
|
|
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
|
|
* The default key type is the column's BasePeer::TYPE_PHPNAME
|
|
*
|
|
* @param array $arr An array to populate the object from.
|
|
* @param string $keyType The type of keys the array uses.
|
|
* @return void
|
|
*/
|
|
public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
|
|
{
|
|
$keys = TypePeer::getFieldNames($keyType);
|
|
|
|
if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
|
|
if (array_key_exists($keys[1], $arr)) $this->setLabel($arr[$keys[1]]);
|
|
if (array_key_exists($keys[2], $arr)) $this->setIcon($arr[$keys[2]]);
|
|
if (array_key_exists($keys[3], $arr)) $this->setThumbnail($arr[$keys[3]]);
|
|
if (array_key_exists($keys[4], $arr)) $this->setMimes($arr[$keys[4]]);
|
|
if (array_key_exists($keys[5], $arr)) $this->setCreatedAt($arr[$keys[5]]);
|
|
if (array_key_exists($keys[6], $arr)) $this->setUpdatedAt($arr[$keys[6]]);
|
|
if (array_key_exists($keys[7], $arr)) $this->setSortableRank($arr[$keys[7]]);
|
|
if (array_key_exists($keys[8], $arr)) $this->setSlug($arr[$keys[8]]);
|
|
}
|
|
|
|
/**
|
|
* Build a Criteria object containing the values of all modified columns in this object.
|
|
*
|
|
* @return Criteria The Criteria object containing all modified values.
|
|
*/
|
|
public function buildCriteria()
|
|
{
|
|
$criteria = new Criteria(TypePeer::DATABASE_NAME);
|
|
|
|
if ($this->isColumnModified(TypePeer::ID)) $criteria->add(TypePeer::ID, $this->id);
|
|
if ($this->isColumnModified(TypePeer::LABEL)) $criteria->add(TypePeer::LABEL, $this->label);
|
|
if ($this->isColumnModified(TypePeer::ICON)) $criteria->add(TypePeer::ICON, $this->icon);
|
|
if ($this->isColumnModified(TypePeer::THUMBNAIL)) $criteria->add(TypePeer::THUMBNAIL, $this->thumbnail);
|
|
if ($this->isColumnModified(TypePeer::MIMES)) $criteria->add(TypePeer::MIMES, $this->mimes);
|
|
if ($this->isColumnModified(TypePeer::CREATED_AT)) $criteria->add(TypePeer::CREATED_AT, $this->created_at);
|
|
if ($this->isColumnModified(TypePeer::UPDATED_AT)) $criteria->add(TypePeer::UPDATED_AT, $this->updated_at);
|
|
if ($this->isColumnModified(TypePeer::SORTABLE_RANK)) $criteria->add(TypePeer::SORTABLE_RANK, $this->sortable_rank);
|
|
if ($this->isColumnModified(TypePeer::SLUG)) $criteria->add(TypePeer::SLUG, $this->slug);
|
|
|
|
return $criteria;
|
|
}
|
|
|
|
/**
|
|
* Builds a Criteria object containing the primary key for this object.
|
|
*
|
|
* Unlike buildCriteria() this method includes the primary key values regardless
|
|
* of whether or not they have been modified.
|
|
*
|
|
* @return Criteria The Criteria object containing value(s) for primary key(s).
|
|
*/
|
|
public function buildPkeyCriteria()
|
|
{
|
|
$criteria = new Criteria(TypePeer::DATABASE_NAME);
|
|
$criteria->add(TypePeer::ID, $this->id);
|
|
|
|
return $criteria;
|
|
}
|
|
|
|
/**
|
|
* Returns the primary key for this object (row).
|
|
* @return int
|
|
*/
|
|
public function getPrimaryKey()
|
|
{
|
|
return $this->getId();
|
|
}
|
|
|
|
/**
|
|
* Generic method to set the primary key (id column).
|
|
*
|
|
* @param int $key Primary key.
|
|
* @return void
|
|
*/
|
|
public function setPrimaryKey($key)
|
|
{
|
|
$this->setId($key);
|
|
}
|
|
|
|
/**
|
|
* Returns true if the primary key for this object is null.
|
|
* @return boolean
|
|
*/
|
|
public function isPrimaryKeyNull()
|
|
{
|
|
|
|
return null === $this->getId();
|
|
}
|
|
|
|
/**
|
|
* Sets contents of passed object to values from current object.
|
|
*
|
|
* If desired, this method can also make copies of all associated (fkey referrers)
|
|
* objects.
|
|
*
|
|
* @param object $copyObj An object of Type (or compatible) type.
|
|
* @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
|
|
* @param boolean $makeNew Whether to reset autoincrement PKs and make the object new.
|
|
* @throws PropelException
|
|
*/
|
|
public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
|
|
{
|
|
$copyObj->setLabel($this->getLabel());
|
|
$copyObj->setIcon($this->getIcon());
|
|
$copyObj->setThumbnail($this->getThumbnail());
|
|
$copyObj->setMimes($this->getMimes());
|
|
$copyObj->setCreatedAt($this->getCreatedAt());
|
|
$copyObj->setUpdatedAt($this->getUpdatedAt());
|
|
$copyObj->setSortableRank($this->getSortableRank());
|
|
$copyObj->setSlug($this->getSlug());
|
|
|
|
if ($deepCopy && !$this->startCopy) {
|
|
// important: temporarily setNew(false) because this affects the behavior of
|
|
// the getter/setter methods for fkey referrer objects.
|
|
$copyObj->setNew(false);
|
|
// store object hash to prevent cycle
|
|
$this->startCopy = true;
|
|
|
|
foreach ($this->getMedias() as $relObj) {
|
|
if ($relObj !== $this) { // ensure that we don't try to copy a reference to ourselves
|
|
$copyObj->addMedia($relObj->copy($deepCopy));
|
|
}
|
|
}
|
|
|
|
//unflag object copy
|
|
$this->startCopy = false;
|
|
} // if ($deepCopy)
|
|
|
|
if ($makeNew) {
|
|
$copyObj->setNew(true);
|
|
$copyObj->setId(NULL); // this is a auto-increment column, so set to default value
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Makes a copy of this object that will be inserted as a new row in table when saved.
|
|
* It creates a new object filling in the simple attributes, but skipping any primary
|
|
* keys that are defined for the table.
|
|
*
|
|
* If desired, this method can also make copies of all associated (fkey referrers)
|
|
* objects.
|
|
*
|
|
* @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
|
|
* @return Type Clone of current object.
|
|
* @throws PropelException
|
|
*/
|
|
public function copy($deepCopy = false)
|
|
{
|
|
// we use get_class(), because this might be a subclass
|
|
$clazz = get_class($this);
|
|
$copyObj = new $clazz();
|
|
$this->copyInto($copyObj, $deepCopy);
|
|
|
|
return $copyObj;
|
|
}
|
|
|
|
/**
|
|
* Returns a peer instance associated with this om.
|
|
*
|
|
* Since Peer classes are not to have any instance attributes, this method returns the
|
|
* same instance for all member of this class. The method could therefore
|
|
* be static, but this would prevent one from overriding the behavior.
|
|
*
|
|
* @return TypePeer
|
|
*/
|
|
public function getPeer()
|
|
{
|
|
if (self::$peer === null) {
|
|
self::$peer = new TypePeer();
|
|
}
|
|
|
|
return self::$peer;
|
|
}
|
|
|
|
|
|
/**
|
|
* Initializes a collection based on the name of a relation.
|
|
* Avoids crafting an 'init[$relationName]s' method name
|
|
* that wouldn't work when StandardEnglishPluralizer is used.
|
|
*
|
|
* @param string $relationName The name of the relation to initialize
|
|
* @return void
|
|
*/
|
|
public function initRelation($relationName)
|
|
{
|
|
if ('Media' == $relationName) {
|
|
$this->initMedias();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clears out the collMedias collection
|
|
*
|
|
* This does not modify the database; however, it will remove any associated objects, causing
|
|
* them to be refetched by subsequent calls to accessor method.
|
|
*
|
|
* @return Type The current object (for fluent API support)
|
|
* @see addMedias()
|
|
*/
|
|
public function clearMedias()
|
|
{
|
|
$this->collMedias = null; // important to set this to null since that means it is uninitialized
|
|
$this->collMediasPartial = null;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* reset is the collMedias collection loaded partially
|
|
*
|
|
* @return void
|
|
*/
|
|
public function resetPartialMedias($v = true)
|
|
{
|
|
$this->collMediasPartial = $v;
|
|
}
|
|
|
|
/**
|
|
* Initializes the collMedias collection.
|
|
*
|
|
* By default this just sets the collMedias collection to an empty array (like clearcollMedias());
|
|
* however, you may wish to override this method in your stub class to provide setting appropriate
|
|
* to your application -- for example, setting the initial array to the values stored in database.
|
|
*
|
|
* @param boolean $overrideExisting If set to true, the method call initializes
|
|
* the collection even if it is not empty
|
|
*
|
|
* @return void
|
|
*/
|
|
public function initMedias($overrideExisting = true)
|
|
{
|
|
if (null !== $this->collMedias && !$overrideExisting) {
|
|
return;
|
|
}
|
|
$this->collMedias = new PropelObjectCollection();
|
|
$this->collMedias->setModel('Media');
|
|
}
|
|
|
|
/**
|
|
* Gets an array of Media objects which contain a foreign key that references this object.
|
|
*
|
|
* If the $criteria is not null, it is used to always fetch the results from the database.
|
|
* Otherwise the results are fetched from the database the first time, then cached.
|
|
* Next time the same method is called without $criteria, the cached collection is returned.
|
|
* If this Type is new, it will return
|
|
* an empty collection or the current collection; the criteria is ignored on a new object.
|
|
*
|
|
* @param Criteria $criteria optional Criteria object to narrow the query
|
|
* @param PropelPDO $con optional connection object
|
|
* @return PropelObjectCollection|Media[] List of Media objects
|
|
* @throws PropelException
|
|
*/
|
|
public function getMedias($criteria = null, PropelPDO $con = null)
|
|
{
|
|
$partial = $this->collMediasPartial && !$this->isNew();
|
|
if (null === $this->collMedias || null !== $criteria || $partial) {
|
|
if ($this->isNew() && null === $this->collMedias) {
|
|
// return empty collection
|
|
$this->initMedias();
|
|
} else {
|
|
$collMedias = MediaQuery::create(null, $criteria)
|
|
->filterByType($this)
|
|
->find($con);
|
|
if (null !== $criteria) {
|
|
if (false !== $this->collMediasPartial && count($collMedias)) {
|
|
$this->initMedias(false);
|
|
|
|
foreach($collMedias as $obj) {
|
|
if (false == $this->collMedias->contains($obj)) {
|
|
$this->collMedias->append($obj);
|
|
}
|
|
}
|
|
|
|
$this->collMediasPartial = true;
|
|
}
|
|
|
|
$collMedias->getInternalIterator()->rewind();
|
|
return $collMedias;
|
|
}
|
|
|
|
if($partial && $this->collMedias) {
|
|
foreach($this->collMedias as $obj) {
|
|
if($obj->isNew()) {
|
|
$collMedias[] = $obj;
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->collMedias = $collMedias;
|
|
$this->collMediasPartial = false;
|
|
}
|
|
}
|
|
|
|
return $this->collMedias;
|
|
}
|
|
|
|
/**
|
|
* Sets a collection of Media objects related by a one-to-many relationship
|
|
* to the current object.
|
|
* It will also schedule objects for deletion based on a diff between old objects (aka persisted)
|
|
* and new objects from the given Propel collection.
|
|
*
|
|
* @param PropelCollection $medias A Propel collection.
|
|
* @param PropelPDO $con Optional connection object
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function setMedias(PropelCollection $medias, PropelPDO $con = null)
|
|
{
|
|
$mediasToDelete = $this->getMedias(new Criteria(), $con)->diff($medias);
|
|
|
|
$this->mediasScheduledForDeletion = unserialize(serialize($mediasToDelete));
|
|
|
|
foreach ($mediasToDelete as $mediaRemoved) {
|
|
$mediaRemoved->setType(null);
|
|
}
|
|
|
|
$this->collMedias = null;
|
|
foreach ($medias as $media) {
|
|
$this->addMedia($media);
|
|
}
|
|
|
|
$this->collMedias = $medias;
|
|
$this->collMediasPartial = false;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Returns the number of related Media objects.
|
|
*
|
|
* @param Criteria $criteria
|
|
* @param boolean $distinct
|
|
* @param PropelPDO $con
|
|
* @return int Count of related Media objects.
|
|
* @throws PropelException
|
|
*/
|
|
public function countMedias(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
|
|
{
|
|
$partial = $this->collMediasPartial && !$this->isNew();
|
|
if (null === $this->collMedias || null !== $criteria || $partial) {
|
|
if ($this->isNew() && null === $this->collMedias) {
|
|
return 0;
|
|
}
|
|
|
|
if($partial && !$criteria) {
|
|
return count($this->getMedias());
|
|
}
|
|
$query = MediaQuery::create(null, $criteria);
|
|
if ($distinct) {
|
|
$query->distinct();
|
|
}
|
|
|
|
return $query
|
|
->filterByType($this)
|
|
->count($con);
|
|
}
|
|
|
|
return count($this->collMedias);
|
|
}
|
|
|
|
/**
|
|
* Method called to associate a Media object to this object
|
|
* through the Media foreign key attribute.
|
|
*
|
|
* @param Media $l Media
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function addMedia(Media $l)
|
|
{
|
|
if ($this->collMedias === null) {
|
|
$this->initMedias();
|
|
$this->collMediasPartial = true;
|
|
}
|
|
if (!in_array($l, $this->collMedias->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
|
|
$this->doAddMedia($l);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param Media $media The media object to add.
|
|
*/
|
|
protected function doAddMedia($media)
|
|
{
|
|
$this->collMedias[]= $media;
|
|
$media->setType($this);
|
|
}
|
|
|
|
/**
|
|
* @param Media $media The media object to remove.
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function removeMedia($media)
|
|
{
|
|
if ($this->getMedias()->contains($media)) {
|
|
$this->collMedias->remove($this->collMedias->search($media));
|
|
if (null === $this->mediasScheduledForDeletion) {
|
|
$this->mediasScheduledForDeletion = clone $this->collMedias;
|
|
$this->mediasScheduledForDeletion->clear();
|
|
}
|
|
$this->mediasScheduledForDeletion[]= $media;
|
|
$media->setType(null);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* If this collection has already been initialized with
|
|
* an identical criteria, it returns the collection.
|
|
* Otherwise if this Type is new, it will return
|
|
* an empty collection; or if this Type has previously
|
|
* been saved, it will retrieve related Medias from storage.
|
|
*
|
|
* This method is protected by default in order to keep the public
|
|
* api reasonable. You can provide public methods for those you
|
|
* actually need in Type.
|
|
*
|
|
* @param Criteria $criteria optional Criteria object to narrow the query
|
|
* @param PropelPDO $con optional connection object
|
|
* @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
|
|
* @return PropelObjectCollection|Media[] List of Media objects
|
|
*/
|
|
public function getMediasJoinCategory($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
|
|
{
|
|
$query = MediaQuery::create(null, $criteria);
|
|
$query->joinWith('Category', $join_behavior);
|
|
|
|
return $this->getMedias($query, $con);
|
|
}
|
|
|
|
/**
|
|
* Clears the current object and sets all attributes to their default values
|
|
*/
|
|
public function clear()
|
|
{
|
|
$this->id = null;
|
|
$this->label = null;
|
|
$this->icon = null;
|
|
$this->thumbnail = null;
|
|
$this->mimes = null;
|
|
$this->created_at = null;
|
|
$this->updated_at = null;
|
|
$this->sortable_rank = null;
|
|
$this->slug = null;
|
|
$this->alreadyInSave = false;
|
|
$this->alreadyInValidation = false;
|
|
$this->alreadyInClearAllReferencesDeep = false;
|
|
$this->clearAllReferences();
|
|
$this->resetModified();
|
|
$this->setNew(true);
|
|
$this->setDeleted(false);
|
|
}
|
|
|
|
/**
|
|
* Resets all references to other model objects or collections of model objects.
|
|
*
|
|
* This method is a user-space workaround for PHP's inability to garbage collect
|
|
* objects with circular references (even in PHP 5.3). This is currently necessary
|
|
* when using Propel in certain daemon or large-volumne/high-memory operations.
|
|
*
|
|
* @param boolean $deep Whether to also clear the references on all referrer objects.
|
|
*/
|
|
public function clearAllReferences($deep = false)
|
|
{
|
|
if ($deep && !$this->alreadyInClearAllReferencesDeep) {
|
|
$this->alreadyInClearAllReferencesDeep = true;
|
|
if ($this->collMedias) {
|
|
foreach ($this->collMedias as $o) {
|
|
$o->clearAllReferences($deep);
|
|
}
|
|
}
|
|
|
|
$this->alreadyInClearAllReferencesDeep = false;
|
|
} // if ($deep)
|
|
|
|
if ($this->collMedias instanceof PropelCollection) {
|
|
$this->collMedias->clearIterator();
|
|
}
|
|
$this->collMedias = null;
|
|
}
|
|
|
|
/**
|
|
* return the string representation of this object
|
|
*
|
|
* @return string
|
|
*/
|
|
public function __toString()
|
|
{
|
|
return (string) $this->exportTo(TypePeer::DEFAULT_STRING_FORMAT);
|
|
}
|
|
|
|
/**
|
|
* return true is the object is in saving state
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isAlreadyInSave()
|
|
{
|
|
return $this->alreadyInSave;
|
|
}
|
|
|
|
// timestampable behavior
|
|
|
|
/**
|
|
* Mark the current object so that the update date doesn't get updated during next save
|
|
*
|
|
* @return Type The current object (for fluent API support)
|
|
*/
|
|
public function keepUpdateDateUnchanged()
|
|
{
|
|
$this->modifiedColumns[] = TypePeer::UPDATED_AT;
|
|
|
|
return $this;
|
|
}
|
|
|
|
// sortable behavior
|
|
|
|
/**
|
|
* Wrap the getter for rank value
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getRank()
|
|
{
|
|
return $this->sortable_rank;
|
|
}
|
|
|
|
/**
|
|
* Wrap the setter for rank value
|
|
*
|
|
* @param int
|
|
* @return Type
|
|
*/
|
|
public function setRank($v)
|
|
{
|
|
return $this->setSortableRank($v);
|
|
}
|
|
|
|
/**
|
|
* Check if the object is first in the list, i.e. if it has 1 for rank
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isFirst()
|
|
{
|
|
return $this->getSortableRank() == 1;
|
|
}
|
|
|
|
/**
|
|
* Check if the object is last in the list, i.e. if its rank is the highest rank
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isLast(PropelPDO $con = null)
|
|
{
|
|
return $this->getSortableRank() == TypeQuery::create()->getMaxRank($con);
|
|
}
|
|
|
|
/**
|
|
* Get the next item in the list, i.e. the one for which rank is immediately higher
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type
|
|
*/
|
|
public function getNext(PropelPDO $con = null)
|
|
{
|
|
|
|
return TypeQuery::create()->findOneByRank($this->getSortableRank() + 1, $con);
|
|
}
|
|
|
|
/**
|
|
* Get the previous item in the list, i.e. the one for which rank is immediately lower
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type
|
|
*/
|
|
public function getPrevious(PropelPDO $con = null)
|
|
{
|
|
|
|
return TypeQuery::create()->findOneByRank($this->getSortableRank() - 1, $con);
|
|
}
|
|
|
|
/**
|
|
* Insert at specified rank
|
|
* The modifications are not persisted until the object is saved.
|
|
*
|
|
* @param integer $rank rank value
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*
|
|
* @throws PropelException
|
|
*/
|
|
public function insertAtRank($rank, PropelPDO $con = null)
|
|
{
|
|
$maxRank = TypeQuery::create()->getMaxRank($con);
|
|
if ($rank < 1 || $rank > $maxRank + 1) {
|
|
throw new PropelException('Invalid rank ' . $rank);
|
|
}
|
|
// move the object in the list, at the given rank
|
|
$this->setSortableRank($rank);
|
|
if ($rank != $maxRank + 1) {
|
|
// Keep the list modification query for the save() transaction
|
|
$this->sortableQueries []= array(
|
|
'callable' => array(self::PEER, 'shiftRank'),
|
|
'arguments' => array(1, $rank, null, )
|
|
);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Insert in the last rank
|
|
* The modifications are not persisted until the object is saved.
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*
|
|
* @throws PropelException
|
|
*/
|
|
public function insertAtBottom(PropelPDO $con = null)
|
|
{
|
|
$this->setSortableRank(TypeQuery::create()->getMaxRank($con) + 1);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Insert in the first rank
|
|
* The modifications are not persisted until the object is saved.
|
|
*
|
|
* @return Type the current object
|
|
*/
|
|
public function insertAtTop()
|
|
{
|
|
return $this->insertAtRank(1);
|
|
}
|
|
|
|
/**
|
|
* Move the object to a new rank, and shifts the rank
|
|
* Of the objects inbetween the old and new rank accordingly
|
|
*
|
|
* @param integer $newRank rank value
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*
|
|
* @throws PropelException
|
|
*/
|
|
public function moveToRank($newRank, PropelPDO $con = null)
|
|
{
|
|
if ($this->isNew()) {
|
|
throw new PropelException('New objects cannot be moved. Please use insertAtRank() instead');
|
|
}
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME);
|
|
}
|
|
if ($newRank < 1 || $newRank > TypeQuery::create()->getMaxRank($con)) {
|
|
throw new PropelException('Invalid rank ' . $newRank);
|
|
}
|
|
|
|
$oldRank = $this->getSortableRank();
|
|
if ($oldRank == $newRank) {
|
|
return $this;
|
|
}
|
|
|
|
$con->beginTransaction();
|
|
try {
|
|
// shift the objects between the old and the new rank
|
|
$delta = ($oldRank < $newRank) ? -1 : 1;
|
|
TypePeer::shiftRank($delta, min($oldRank, $newRank), max($oldRank, $newRank), $con);
|
|
|
|
// move the object to its new rank
|
|
$this->setSortableRank($newRank);
|
|
$this->save($con);
|
|
|
|
$con->commit();
|
|
|
|
return $this;
|
|
} catch (Exception $e) {
|
|
$con->rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Exchange the rank of the object with the one passed as argument, and saves both objects
|
|
*
|
|
* @param Type $object
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*
|
|
* @throws Exception if the database cannot execute the two updates
|
|
*/
|
|
public function swapWith($object, PropelPDO $con = null)
|
|
{
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME);
|
|
}
|
|
$con->beginTransaction();
|
|
try {
|
|
$oldRank = $this->getSortableRank();
|
|
$newRank = $object->getSortableRank();
|
|
$this->setSortableRank($newRank);
|
|
$this->save($con);
|
|
$object->setSortableRank($oldRank);
|
|
$object->save($con);
|
|
$con->commit();
|
|
|
|
return $this;
|
|
} catch (Exception $e) {
|
|
$con->rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move the object higher in the list, i.e. exchanges its rank with the one of the previous object
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*/
|
|
public function moveUp(PropelPDO $con = null)
|
|
{
|
|
if ($this->isFirst()) {
|
|
return $this;
|
|
}
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME);
|
|
}
|
|
$con->beginTransaction();
|
|
try {
|
|
$prev = $this->getPrevious($con);
|
|
$this->swapWith($prev, $con);
|
|
$con->commit();
|
|
|
|
return $this;
|
|
} catch (Exception $e) {
|
|
$con->rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move the object higher in the list, i.e. exchanges its rank with the one of the next object
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*/
|
|
public function moveDown(PropelPDO $con = null)
|
|
{
|
|
if ($this->isLast($con)) {
|
|
return $this;
|
|
}
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME);
|
|
}
|
|
$con->beginTransaction();
|
|
try {
|
|
$next = $this->getNext($con);
|
|
$this->swapWith($next, $con);
|
|
$con->commit();
|
|
|
|
return $this;
|
|
} catch (Exception $e) {
|
|
$con->rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move the object to the top of the list
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*/
|
|
public function moveToTop(PropelPDO $con = null)
|
|
{
|
|
if ($this->isFirst()) {
|
|
return $this;
|
|
}
|
|
|
|
return $this->moveToRank(1, $con);
|
|
}
|
|
|
|
/**
|
|
* Move the object to the bottom of the list
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return integer the old object's rank
|
|
*/
|
|
public function moveToBottom(PropelPDO $con = null)
|
|
{
|
|
if ($this->isLast($con)) {
|
|
return false;
|
|
}
|
|
if ($con === null) {
|
|
$con = Propel::getConnection(TypePeer::DATABASE_NAME);
|
|
}
|
|
$con->beginTransaction();
|
|
try {
|
|
$bottom = TypeQuery::create()->getMaxRank($con);
|
|
$res = $this->moveToRank($bottom, $con);
|
|
$con->commit();
|
|
|
|
return $res;
|
|
} catch (Exception $e) {
|
|
$con->rollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes the current object from the list.
|
|
* The modifications are not persisted until the object is saved.
|
|
*
|
|
* @param PropelPDO $con optional connection
|
|
*
|
|
* @return Type the current object
|
|
*/
|
|
public function removeFromList(PropelPDO $con = null)
|
|
{
|
|
// Keep the list modification query for the save() transaction
|
|
$this->sortableQueries []= array(
|
|
'callable' => array(self::PEER, 'shiftRank'),
|
|
'arguments' => array(-1, $this->getSortableRank() + 1, null)
|
|
);
|
|
// remove the object from the list
|
|
$this->setSortableRank(null);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Execute queries that were saved to be run inside the save transaction
|
|
*/
|
|
protected function processSortableQueries($con)
|
|
{
|
|
foreach ($this->sortableQueries as $query) {
|
|
$query['arguments'][]= $con;
|
|
call_user_func_array($query['callable'], $query['arguments']);
|
|
}
|
|
$this->sortableQueries = array();
|
|
}
|
|
|
|
// sluggable behavior
|
|
|
|
/**
|
|
* Create a unique slug based on the object
|
|
*
|
|
* @return string The object slug
|
|
*/
|
|
protected function createSlug()
|
|
{
|
|
$slug = $this->createRawSlug();
|
|
$slug = $this->limitSlugSize($slug);
|
|
$slug = $this->makeSlugUnique($slug);
|
|
|
|
return $slug;
|
|
}
|
|
|
|
/**
|
|
* Create the slug from the appropriate columns
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function createRawSlug()
|
|
{
|
|
return '' . $this->cleanupSlugPart($this->getLabel()) . '';
|
|
}
|
|
|
|
/**
|
|
* Cleanup a string to make a slug of it
|
|
* Removes special characters, replaces blanks with a separator, and trim it
|
|
*
|
|
* @param string $slug the text to slugify
|
|
* @param string $replacement the separator used by slug
|
|
* @return string the slugified text
|
|
*/
|
|
protected static function cleanupSlugPart($slug, $replacement = '-')
|
|
{
|
|
// transliterate
|
|
if (function_exists('iconv')) {
|
|
$slug = iconv('utf-8', 'us-ascii//TRANSLIT', $slug);
|
|
}
|
|
|
|
// lowercase
|
|
if (function_exists('mb_strtolower')) {
|
|
$slug = mb_strtolower($slug);
|
|
} else {
|
|
$slug = strtolower($slug);
|
|
}
|
|
|
|
// remove accents resulting from OSX's iconv
|
|
$slug = str_replace(array('\'', '`', '^'), '', $slug);
|
|
|
|
// replace non letter or digits with separator
|
|
$slug = preg_replace('/\W+/', $replacement, $slug);
|
|
|
|
// trim
|
|
$slug = trim($slug, $replacement);
|
|
|
|
if (empty($slug)) {
|
|
return 'n-a';
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
|
|
/**
|
|
* Make sure the slug is short enough to accomodate the column size
|
|
*
|
|
* @param string $slug the slug to check
|
|
* @param int $incrementReservedSpace the number of characters to keep empty
|
|
*
|
|
* @return string the truncated slug
|
|
*/
|
|
protected static function limitSlugSize($slug, $incrementReservedSpace = 3)
|
|
{
|
|
// check length, as suffix could put it over maximum
|
|
if (strlen($slug) > (255 - $incrementReservedSpace)) {
|
|
$slug = substr($slug, 0, 255 - $incrementReservedSpace);
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the slug, ensuring its uniqueness
|
|
*
|
|
* @param string $slug the slug to check
|
|
* @param string $separator the separator used by slug
|
|
* @param int $alreadyExists false for the first try, true for the second, and take the high count + 1
|
|
* @return string the unique slug
|
|
*/
|
|
protected function makeSlugUnique($slug, $separator = '-', $alreadyExists = false)
|
|
{
|
|
if (!$alreadyExists) {
|
|
$slug2 = $slug;
|
|
} else {
|
|
$slug2 = $slug . $separator;
|
|
}
|
|
|
|
$query = TypeQuery::create('q')
|
|
->where('q.Slug ' . ($alreadyExists ? 'REGEXP' : '=') . ' ?', $alreadyExists ? '^' . $slug2 . '[0-9]+$' : $slug2)
|
|
->prune($this)
|
|
;
|
|
|
|
if (!$alreadyExists) {
|
|
$count = $query->count();
|
|
if ($count > 0) {
|
|
return $this->makeSlugUnique($slug, $separator, true);
|
|
}
|
|
|
|
return $slug2;
|
|
}
|
|
|
|
// Already exists
|
|
$object = $query
|
|
->addDescendingOrderByColumn('LENGTH(slug)')
|
|
->addDescendingOrderByColumn('slug')
|
|
->findOne();
|
|
|
|
// First duplicate slug
|
|
if (null == $object) {
|
|
return $slug2 . '1';
|
|
}
|
|
|
|
$slugNum = substr($object->getSlug(), strlen($slug) + 1);
|
|
if (0 == $slugNum[0]) {
|
|
$slugNum[0] = 1;
|
|
}
|
|
|
|
return $slug2 . ($slugNum + 1);
|
|
}
|
|
|
|
// uploadable behavior
|
|
protected $uploadDirs = array (
|
|
'thumbnail' => 'uploads/media/type/thumbnail',
|
|
'icon' => 'uploads/media/type/icon',
|
|
);
|
|
|
|
public $rollback_thumbnail = null;
|
|
|
|
public $rollback_icon = null;
|
|
|
|
public $delete_thumbnail = false;
|
|
|
|
public $delete_icon = false;
|
|
|
|
public $thumbnail_file = null;
|
|
|
|
public $icon_file = null;
|
|
|
|
public $has_uploaded_thumbnail = false;
|
|
|
|
public $has_uploaded_icon = false;
|
|
|
|
|
|
/**
|
|
* Return a new filename randomly
|
|
*
|
|
* @param $extension
|
|
* @return string
|
|
*
|
|
*/
|
|
public function getNewFilename($extension)
|
|
{
|
|
return md5(time().uniqid()).($extension ? '.'.$extension : '');
|
|
}
|
|
|
|
/**
|
|
* Return the upload dir of $field field
|
|
*
|
|
* @param $field
|
|
* @return mixed
|
|
*/
|
|
public function getUploadDir($field)
|
|
{
|
|
$dir = $this->uploadDirs[$field];
|
|
|
|
$transform_dir = preg_replace_callback(
|
|
'`\{(.*)\}`sU',
|
|
function($matches) {
|
|
return method_exists($this, $matches[1]) ? (string) call_user_func(array($this, $matches[1])) : '';
|
|
}, $dir
|
|
);
|
|
|
|
return (is_string($transform_dir))?$transform_dir:$dir;
|
|
}
|
|
|
|
/**
|
|
* Upload the thumbnail field
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function uploadThumbnail()
|
|
{
|
|
if (null === $this->thumbnail && !$this->delete_thumbnail && null !== $this->rollback_thumbnail) {
|
|
$this->thumbnail = $this->rollback_thumbnail; // keep the file
|
|
return true;
|
|
}
|
|
|
|
if (null === $this->thumbnail || !is_object($this->thumbnail)) {
|
|
return true; // no file to upload
|
|
}
|
|
|
|
//Delete old file on overload
|
|
$this->removeThumbnail();
|
|
|
|
$filename = $this->getNewFilename($this->thumbnail->guessExtension());
|
|
|
|
$this->thumbnail_file = $this->thumbnail;
|
|
|
|
$this->has_uploaded_thumbnail = true;
|
|
|
|
$this->thumbnail = $filename;
|
|
}
|
|
|
|
/**
|
|
* Upload the icon field
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function uploadIcon()
|
|
{
|
|
if (null === $this->icon && !$this->delete_icon && null !== $this->rollback_icon) {
|
|
$this->icon = $this->rollback_icon; // keep the file
|
|
return true;
|
|
}
|
|
|
|
if (null === $this->icon || !is_object($this->icon)) {
|
|
return true; // no file to upload
|
|
}
|
|
|
|
//Delete old file on overload
|
|
$this->removeIcon();
|
|
|
|
$filename = $this->getNewFilename($this->icon->guessExtension());
|
|
|
|
$this->icon_file = $this->icon;
|
|
|
|
$this->has_uploaded_icon = true;
|
|
|
|
$this->icon = $filename;
|
|
}
|
|
|
|
/**
|
|
* Save the file link to the thumbnail field
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function saveThumbnailFile()
|
|
{
|
|
if($this->hasUploadedThumbnail()){
|
|
$this->thumbnail_file->move($this->getUploadDir('thumbnail'), $this->thumbnail);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save the file link to the icon field
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function saveIconFile()
|
|
{
|
|
if($this->hasUploadedIcon()){
|
|
$this->icon_file->move($this->getUploadDir('icon'), $this->icon);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the thumbnail file present in 'rollback_thumbnail'
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function removeThumbnail()
|
|
{
|
|
if (!empty($this->rollback_thumbnail)) {
|
|
$file = $this->getUploadDir('thumbnail').DIRECTORY_SEPARATOR.$this->rollback_thumbnail;
|
|
|
|
if (file_exists($file) && is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the icon file present in 'rollback_icon'
|
|
*
|
|
* @return void
|
|
*
|
|
*/
|
|
public function removeIcon()
|
|
{
|
|
if (!empty($this->rollback_icon)) {
|
|
$file = $this->getUploadDir('icon').DIRECTORY_SEPARATOR.$this->rollback_icon;
|
|
|
|
if (file_exists($file) && is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function setDeleteThumbnail($v)
|
|
{
|
|
$this->delete_thumbnail = $v;
|
|
}
|
|
|
|
public function getDeleteThumbnail()
|
|
{
|
|
return $this->delete_thumbnail;
|
|
}
|
|
|
|
public function setDeleteIcon($v)
|
|
{
|
|
$this->delete_icon = $v;
|
|
}
|
|
|
|
public function getDeleteIcon()
|
|
{
|
|
return $this->delete_icon;
|
|
}
|
|
|
|
public function getWebPathForThumbnail()
|
|
{
|
|
return null === $this->thumbnail ? null : $this->getUploadDir('thumbnail').DIRECTORY_SEPARATOR.$this->thumbnail;
|
|
}
|
|
|
|
public function getWebPathForIcon()
|
|
{
|
|
return null === $this->icon ? null : $this->getUploadDir('icon').DIRECTORY_SEPARATOR.$this->icon;
|
|
}
|
|
|
|
public function hasUploadedThumbnail()
|
|
{
|
|
return $this->has_uploaded_thumbnail;
|
|
}
|
|
|
|
public function hasUploadedIcon()
|
|
{
|
|
return $this->has_uploaded_icon;
|
|
}
|
|
|
|
/**
|
|
* Return the $field value as File object
|
|
*
|
|
* @return \Symfony\Component\HttpFoundation\File\File
|
|
*
|
|
*/
|
|
public function getThumbnailAsFile()
|
|
{
|
|
$v = $this->getWebPathForThumbnail();
|
|
|
|
if (!empty($v) && file_exists($v) && is_file($v)) {
|
|
return new \Symfony\Component\HttpFoundation\File\File($v);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return the $field value as File object
|
|
*
|
|
* @return \Symfony\Component\HttpFoundation\File\File
|
|
*
|
|
*/
|
|
public function getIconAsFile()
|
|
{
|
|
$v = $this->getWebPathForIcon();
|
|
|
|
if (!empty($v) && file_exists($v) && is_file($v)) {
|
|
return new \Symfony\Component\HttpFoundation\File\File($v);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
}
|