Exemplo n.º 1
0
 protected function insert($connection = null)
 {
     if (!isset($this->slug)) {
         $this->slug = QubitSlug::slugify($this->__get('name', array('sourceCulture' => true)));
     }
     return parent::insert($connection);
 }
Exemplo n.º 2
0
 protected function insert($connection = null)
 {
     $this->identifier = self::generateAccessionIdentifier(true);
     if (!isset($this->slug)) {
         $this->slug = QubitSlug::slugify($this->__get('identifier', array('sourceCulture' => true)));
     }
     parent::insert($connection);
 }
Exemplo n.º 3
0
 protected function update($connection = null)
 {
     if (!isset($connection)) {
         $connection = QubitTransactionFilter::getConnection(QubitStaticPage::DATABASE_NAME);
     }
     $statement = $connection->prepare('
   UPDATE ' . QubitSlug::TABLE_NAME . '
   SET ' . QubitSlug::SLUG . ' = ?
   WHERE ' . QubitSlug::OBJECT_ID . ' = ?');
     if (1 > strlen($this->slug)) {
         $statement->execute(array(QubitSlug::random(), $this->id));
         return;
     }
     try {
         $statement->execute(array($this->slug, $this->id));
     } catch (PDOException $e) {
         $statement->execute(array(QubitSlug::random(), $this->id));
     }
     return parent::update($connection);
 }
Exemplo n.º 4
0
 public function insertSlug($connection)
 {
     if (!isset($connection)) {
         $connection = QubitTransactionFilter::getConnection(QubitObject::DATABASE_NAME);
     }
     if (isset($this->slug)) {
         $statement = $connection->prepare('
     INSERT INTO ' . QubitSlug::TABLE_NAME . ' (' . QubitSlug::OBJECT_ID . ', ' . QubitSlug::SLUG . ')
     VALUES (?, ?)');
         // Unless it is set, get random, digit and letter slug
         if (1 > strlen($this->slug)) {
             $statement->execute(array($this->id, QubitSlug::getUnique($connection)));
             return $this;
         }
         // Compute unique slug adding contiguous numeric suffix
         $suffix = 2;
         do {
             try {
                 $statement->execute(array($this->id, $this->slug));
                 unset($suffix);
             } catch (PDOException $e) {
                 if (2 == $suffix) {
                     $this->slug .= "-{$suffix}";
                 } else {
                     $this->slug = preg_replace('/-\\d+$/', '', $this->slug, 1) . "-{$suffix}";
                 }
                 $suffix++;
             }
         } while (isset($suffix));
     }
     return $this;
 }
 /**
  * @see sfTask
  */
 public function execute($arguments = array(), $options = array())
 {
     $databaseManager = new sfDatabaseManager($this->configuration);
     $conn = $databaseManager->getDatabase('propel')->getConnection();
     $tables = array('actor' => 'QubitActor', 'information_object' => 'QubitInformationObject', 'term' => 'QubitTerm', 'event' => 'QubitEvent');
     // Create hash of slugs already in database
     $sql = "SELECT slug FROM slug ORDER BY slug";
     foreach ($conn->query($sql, PDO::FETCH_NUM) as $row) {
         $slugs[$row[0]] = true;
     }
     foreach ($tables as $table => $classname) {
         $this->logSection('propel', "Generate {$table} slugs...");
         $newRows = array();
         // reset
         switch ($table) {
             case 'actor':
                 $sql = 'SELECT base.id, i18n.authorized_form_of_name';
                 break;
             case 'information_object':
                 $sql = 'SELECT base.id, i18n.title';
                 break;
             default:
                 $sql = 'SELECT base.id, i18n.name';
         }
         $sql .= ' FROM ' . constant($classname . '::TABLE_NAME') . ' base';
         $sql .= ' INNER JOIN ' . constant($classname . 'I18n::TABLE_NAME') . ' i18n';
         $sql .= '  ON base.id = i18n.id';
         $sql .= ' LEFT JOIN ' . QubitSlug::TABLE_NAME . ' sl';
         $sql .= '  ON base.id = sl.object_id';
         $sql .= ' WHERE base.id > 3';
         $sql .= '  AND base.source_culture = i18n.culture';
         $sql .= '  AND sl.id is NULL';
         foreach ($conn->query($sql, PDO::FETCH_NUM) as $row) {
             // Get unique slug
             if (null !== $row[1]) {
                 $slug = QubitSlug::slugify($row[1]);
                 // Truncate at 250 chars
                 if (250 < strlen($slug)) {
                     $slug = substr($slug, 0, 250);
                 }
                 $count = 0;
                 $suffix = '';
                 while (isset($slugs[$slug . $suffix])) {
                     $count++;
                     $suffix = '-' . $count;
                 }
                 $slug .= $suffix;
             } else {
                 $slug = QubitSlug::random();
                 while (isset($slugs[$slug])) {
                     $slug = QubitSlug::random();
                 }
             }
             $slugs[$slug] = true;
             // Add to lookup table
             $newRows[] = array($row[0], $slug);
             // To add to slug table
         }
         // Do inserts
         $inc = 1000;
         for ($i = 0; $i < count($newRows); $i += $inc) {
             $sql = "INSERT INTO slug (object_id, slug) VALUES ";
             $last = min($i + $inc, count($newRows));
             for ($j = $i; $j < $last; $j++) {
                 $sql .= '(' . $newRows[$j][0] . ', \'' . $newRows[$j][1] . '\'), ';
             }
             $sql = substr($sql, 0, -2) . ';';
             $conn->exec($sql);
         }
     }
     $this->logSection('propel', 'Done!');
 }
Exemplo n.º 6
0
 protected function insert($connection = null)
 {
     $this->slug = QubitSlug::slugify($this->slug);
     return parent::insert($connection);
 }
Exemplo n.º 7
0
 public static function getslugsById($id, array $options = array())
 {
     $criteria = new Criteria();
     self::addslugsCriteriaById($criteria, $id);
     return QubitSlug::get($criteria, $options);
 }