Author: xavier
Date: 2010-05-15 17:20:12 +0200 (Sat, 15 May 2010)
New Revision: 29478

Added:
   plugins/sfDoctrineShortUrlPlugin/trunk/config/
   plugins/sfDoctrineShortUrlPlugin/trunk/config/config.php
   plugins/sfDoctrineShortUrlPlugin/trunk/config/doctrine/
   plugins/sfDoctrineShortUrlPlugin/trunk/config/doctrine/schema.yml
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/doctrine/
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/doctrine/PluginsfShortUrlFormFilter.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/doctrine/
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/doctrine/PluginsfShortUrlForm.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrl.class.php
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/lib/routing/
   
plugins/sfDoctrineShortUrlPlugin/trunk/lib/routing/sfDoctrineShortUrlRouting.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/modules/
   plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/
   plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/actions/
   
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/actions/actions.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/lib/
   
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/lib/BasesfShortUrlActions.class.php
   plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/templates/
   
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/templates/indexSuccess.php
Log:
[wiki:sfDoctrineShortUrlPlugin]: added the code for this plugin (moved from 
opensource.lacot.org)

Added: plugins/sfDoctrineShortUrlPlugin/trunk/config/config.php
===================================================================
--- plugins/sfDoctrineShortUrlPlugin/trunk/config/config.php                    
        (rev 0)
+++ plugins/sfDoctrineShortUrlPlugin/trunk/config/config.php    2010-05-15 
15:20:12 UTC (rev 29478)
@@ -0,0 +1,19 @@
+<?php
+/*
+ * This file is part of the sfDoctrineShortUrlPlugin package.
+ *
+ * (c) 2009 Xavier Lacot <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+// add routing rules
+if (sfConfig::get('app_sfDoctrineShortUrlPlugin_use_routes', true)
+    && in_array('sfShortUrl', sfConfig::get('sf_enabled_modules', array())))
+{
+  $this->dispatcher->connect(
+    'routing.load_configuration',
+    array('sfDoctrineShortUrlRouting', 'listenToRoutingLoadConfigurationEvent')
+  );
+}
\ No newline at end of file

Added: plugins/sfDoctrineShortUrlPlugin/trunk/config/doctrine/schema.yml
===================================================================
--- plugins/sfDoctrineShortUrlPlugin/trunk/config/doctrine/schema.yml           
                (rev 0)
+++ plugins/sfDoctrineShortUrlPlugin/trunk/config/doctrine/schema.yml   
2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,17 @@
+sfShortUrl:
+  actAs: [Timestampable]
+  columns:
+    id:               { type: integer, primary: true, autoincrement: true }
+    shorturl:         { type: varchar, size: 255, unique: true }
+    longurl:          { type: clob }
+    viewcount:        { type: integer, default: 0 }
+    last_visited_at:  { type: timestamp }
+    is_enabled:       { type: boolean, default: true }
+  indexes:
+    uriindex:
+      fields:
+        longurl:
+          length:          "255"
+    shorturlindex:
+      fields:         [shorturl]
+      type:           unique
\ No newline at end of file

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/doctrine/PluginsfShortUrlFormFilter.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/doctrine/PluginsfShortUrlFormFilter.class.php
                             (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/filter/doctrine/PluginsfShortUrlFormFilter.class.php
     2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * PluginsfShortUrl form.
+ *
+ * @package    ##PROJECT_NAME##
+ * @subpackage filter
+ * @author     ##AUTHOR_NAME##
+ * @version    SVN: $Id: sfDoctrineFormFilterPluginTemplate.php 23810 
2009-11-12 11:07:44Z Kris.Wallsmith $
+ */
+abstract class PluginsfShortUrlFormFilter extends BasesfShortUrlFormFilter
+{
+}

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/doctrine/PluginsfShortUrlForm.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/doctrine/PluginsfShortUrlForm.class.php
                             (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/form/doctrine/PluginsfShortUrlForm.class.php
     2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * PluginsfShortUrl form.
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage form
+ * @author     Xavier Lacot <[email protected]>
+ */
+abstract class PluginsfShortUrlForm extends BasesfShortUrlForm
+{
+  public function setup()
+  {
+    parent::setup();
+    unset($this['id']);
+    unset($this['created_at']);
+    unset($this['updated_at']);
+    unset($this['viewcount']);
+    unset($this['is_enabled']);
+    unset($this['last_visited_at']);
+    $this->setWidget('longurl', new sfWidgetFormInputText());
+
+    $this->validatorSchema['longurl'] = new sfValidatorAnd(
+      array(
+        new sfValidatorUrl(
+          array('required' => true),
+          array(
+            'invalid'  => 'This is not a valid url',
+            'required' => 'Please type in a url'
+          )),
+        new sfValidatorRegex(
+          array(
+            'pattern'  => 
'~^(?:(?!('.sfContext::getInstance()->getRequest()->getHost().')).)*$~ix'
+          ),
+          array(
+            'invalid'  => 'This url is not allowed.'
+          )
+        )
+      ),
+      array('required' => true),
+      array(
+        'required' => 'Please type in a url'
+      )
+    );
+    $this->validatorSchema['shorturl'] = new sfValidatorAnd(
+      array(
+        new sfValidatorString(),
+        new sfValidatorRegex(
+          array(
+            'pattern'    => sprintf(
+              '~^((%s)|(\s+)(.*)|(.*)(\s+)|(\s+)(.*)(\s+))$~ix',
+              implode('|', 
sfConfig::get('app_sfDoctrineShortUrlPlugin_forbidden_keywords', 
array('about')))
+            ),
+            'must_match' => false
+          ),
+          array(
+            'invalid'  => 'This shorturl is not allowed.'
+          )
+        )
+      ),
+      array('required' => false)
+    );
+
+    $this->widgetSchema->setLabels(array(
+      'longurl'   => 'Enter a long url',
+      'shorturl'  => 'Optionnaly, define your own short url'
+    ));
+
+    $this->setDefault('longurl', 'http://');
+
+    $this->validatorSchema->setPostValidator(
+      new sfValidatorDoctrineUnique(
+        array(
+          'required' => false,
+          'model'    => 'sfShortUrl',
+          'column'   => 'shorturl',
+        ),
+        array(
+          'invalid'  => 'This short url is already used!'
+        )
+      )
+    );
+  }
+
+  protected function doSave($con = null)
+  {
+    $this->object = Doctrine::getTable('sfShortUrl')->generate(
+      $this->getValue('longurl'),
+      $this->getValue('shorturl')
+    );
+    return $this->object;
+  }
+}

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrl.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrl.class.php
                                (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrl.class.php
        2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,15 @@
+<?php
+
+/**
+ * PluginsfShortUrl
+ *
+ * This class has been auto-generated by the Doctrine ORM Framework
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage model
+ * @author     Xavier Lacot <[email protected]>
+ */
+abstract class PluginsfShortUrl extends BasesfShortUrl
+{
+
+}
\ No newline at end of file

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
                           (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/model/doctrine/PluginsfShortUrlTable.class.php
   2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,68 @@
+<?php
+/**
+ * PluginsfShortUrlTable
+ *
+ * This class has been auto-generated by the Doctrine ORM Framework
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage model
+ * @author     Xavier Lacot <[email protected]>
+ */
+class PluginsfShortUrlTable extends Doctrine_Table
+{
+  /**
+   * Generates a short url
+   */
+  public function acceptOrCreateSfShortUrl($shorturl = null)
+  {
+    $acceptable = false;
+
+    while (!$acceptable)
+    {
+      if ($shorturl)
+      {
+        $acceptable = (count($this->findByShorturl($shorturl)) == 0);
+      }
+
+      if (!$acceptable)
+      {
+        $shorturl = substr(md5(microtime()), 0, 5);
+      }
+    }
+
+    return $shorturl;
+  }
+
+  public function generate($longurl, $shorturl = '')
+  {
+    if ('' == $shorturl)
+    {
+      // try to retrieve a corresponding url within the public ones
+      $shorturl_object = $this->findOneByLongurl($longurl);
+    }
+    else
+    {
+      // try to retrieve a corresponding couple (shorturl, longurl)
+      $objects = Doctrine_Query::create()
+        ->from('sfShortUrl u')
+        ->where('u.longurl = ?', $longurl)
+        ->andWhere('u.shorturl = ?', $shorturl)
+        ->execute();
+      $shorturl_object = isset($objects[0]) ? $objects[0] : null;
+    }
+
+    if (!isset($shorturl_object) || !$shorturl_object)
+    {
+      // no acceptable object => create one
+      $shorturl = $this->acceptOrCreateSfShortUrl($shorturl);
+      $shorturl_object = new sfShortUrl();
+      $shorturl_object->setLongurl($longurl);
+      $shorturl_object->setShorturl($shorturl);
+      $shorturl_object->setIsEnabled(true);
+      $shorturl_object->setViewcount(0);
+      $shorturl_object->save();
+    }
+
+    return $shorturl_object;
+  }
+}
\ No newline at end of file

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/routing/sfDoctrineShortUrlRouting.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/routing/sfDoctrineShortUrlRouting.class.php
                              (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/lib/routing/sfDoctrineShortUrlRouting.class.php
      2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * Routing configuration
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage routing
+ * @author     Xavier Lacot <[email protected]>
+ * @see        http://www.symfony-project.org/plugins/sfDoctrineShortUrlPlugin
+ */
+class sfDoctrineShortUrlRouting
+{
+  /**
+   * Listens to the routing.load_configuration event.
+   *
+   * @param sfEvent An sfEvent instance
+   */
+  static public function listenToRoutingLoadConfigurationEvent(sfEvent $event)
+  {
+    $r = $event->getSubject();
+    $r->prependRoute(
+      'sfShorturl',
+      new sfRoute(
+        '/:shorturl',
+        array('module' => 'sfShortUrl', 'action' => 'shorturl')
+      )
+    );
+    $r->prependRoute(
+      'sfShorturl_homepage',
+      new sfRoute(
+        '/',
+        array('module' => 'sfShortUrl', 'action' => 'index')
+      )
+    );
+  }
+}

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/actions/actions.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/actions/actions.class.php
                         (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/actions/actions.class.php
 2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,13 @@
+<?php
+require_once(dirname(__FILE__).'/../lib/BasesfShortUrlActions.class.php');
+
+/**
+ * shorturl actions.
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage sfShortUrl
+ * @author     Xavier Lacot <[email protected]>
+ */
+class sfShortUrlActions extends BasesfShortUrlActions
+{
+}

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/lib/BasesfShortUrlActions.class.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/lib/BasesfShortUrlActions.class.php
                               (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/lib/BasesfShortUrlActions.class.php
       2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,45 @@
+<?php
+
+/**
+ * shorturl actions.
+ *
+ * @package    sfDoctrineShortUrlPlugin
+ * @subpackage sfShortUrl
+ * @author     Xavier Lacot <[email protected]>
+ */
+class BasesfShortUrlActions extends sfActions
+{
+ /**
+  * Executes index action
+  *
+  * @param sfRequest $request A request object
+  */
+  public function executeIndex(sfWebRequest $request)
+  {
+    $form = new sfShortUrlForm();
+
+    if ($request->getMethod() === sfRequest::POST)
+    {
+      $form->bind(
+        $request->getParameter($form->getName())
+      );
+
+      if ($form->isValid())
+      {
+        $this->shorturl_object = $form->save();
+      }
+    }
+
+    $this->form = $form;
+  }
+
+  public function executeShorturl(sfWebRequest $request)
+  {
+    $shorturl = 
Doctrine::getTable('sfShortUrl')->findOneByShorturl($request->getParameter('shorturl'));
+    $this->forward404Unless($shorturl && $shorturl->getIsEnabled());
+    $shorturl->setViewcount($shorturl->getViewcount() + 1);
+    $shorturl->setLastVisitedAt(date('Y-m-d H:i:s', time()));
+    $shorturl->save();
+    $this->redirect($shorturl->getLongurl(), 301);
+  }
+}

Added: 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/templates/indexSuccess.php
===================================================================
--- 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/templates/indexSuccess.php
                                (rev 0)
+++ 
plugins/sfDoctrineShortUrlPlugin/trunk/modules/sfShortUrl/templates/indexSuccess.php
        2010-05-15 15:20:12 UTC (rev 29478)
@@ -0,0 +1,43 @@
+<?php use_helper('I18N'); ?>
+
+<?php if (isset($shorturl_object)): ?>
+  <p class="notification">
+    <?php
+    $link = url_for('@sfShorturl?shorturl='.$shorturl_object->getShorturl(), 
true);
+    echo __('The short url is available at %1%.', array('%1%' => 
link_to($link, $link)));
+    ?>
+  </p>
+<?php endif; ?>
+
+
+<form method="post" action="<?php echo url_for('@sfShorturl_homepage') ?>">
+  <fieldset>
+    <legend><?php echo __('URL details') ?></legend>
+    <?php if ($form->hasGlobalErrors()): ?>
+      <?php echo $form->renderGlobalErrors() ?>
+    <?php endif; ?>
+
+    <div class="required">
+      <?php
+      $attributes = array();
+
+      if ($form['longurl']->hasError())
+      {
+        $attributes['class'] = 'error';
+      }
+      ?>
+      <?php echo __($form['longurl']->renderLabel()) ?>
+      <?php echo $form['longurl']->render($attributes) ?>
+      <?php echo $form['longurl']->renderError() ?>
+    </div>
+    <div>
+      <?php echo __($form['shorturl']->renderLabel()) ?>
+      http://<?php echo sfContext::getInstance()->getRequest()->getHost() ?> 
/<?php echo $form['shorturl']->render() ?>
+      <?php echo $form['shorturl']->renderError() ?>
+    </div>
+    <div>
+      <?php echo $form->renderHiddenFields() ?>
+      <input type="submit" value="<?php echo __('shorten!') ?>" />
+    </div>
+  </fieldset>
+</form>
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to