Quellcode durchsuchen

Merge branch 'feature/factory' into unstable

bastien vor 13 Jahren
Ursprung
Commit
fb76876982
22 geänderte Dateien mit 1925 neuen und 18 gelöschten Zeilen
  1. 48 0
      src/Muzich/CoreBundle/Controller/CoreController.php
  2. 4 4
      src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementData.php
  3. 3 3
      src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementTypeData.php
  4. 144 0
      src/Muzich/CoreBundle/ElementFactory/ElementManager.php
  5. 15 0
      src/Muzich/CoreBundle/ElementFactory/Site/JamendoFactory.php
  6. 15 0
      src/Muzich/CoreBundle/ElementFactory/Site/Son2TeufFactory.php
  7. 15 0
      src/Muzich/CoreBundle/ElementFactory/Site/SoundCloudFactory.php
  8. 17 0
      src/Muzich/CoreBundle/ElementFactory/Site/YoutubeFactory.php
  9. 30 0
      src/Muzich/CoreBundle/ElementFactory/Site/base/BaseFactory.php
  10. 17 0
      src/Muzich/CoreBundle/ElementFactory/Site/base/FactoryInterface.php
  11. 50 0
      src/Muzich/CoreBundle/ElementFactory/Site/base/VideoSiteFactory.php
  12. 1049 0
      src/Muzich/CoreBundle/ElementFactory/lib/Spyc.php
  13. 362 0
      src/Muzich/CoreBundle/ElementFactory/lib/VideoEmbed.php
  14. 48 3
      src/Muzich/CoreBundle/Entity/Element.php
  15. 1 1
      src/Muzich/CoreBundle/Entity/ElementType.php
  16. 41 0
      src/Muzich/CoreBundle/Form/Element/ElementAddForm.php
  17. 1 1
      src/Muzich/CoreBundle/Repository/ElementRepository.php
  18. 8 5
      src/Muzich/CoreBundle/Resources/config/routing.yml
  19. 28 0
      src/Muzich/CoreBundle/Resources/config/video_embed.yaml
  20. 6 0
      src/Muzich/CoreBundle/Resources/views/SearchElement/default.html.twig
  21. 9 1
      src/Muzich/HomeBundle/Controller/HomeController.php
  22. 14 0
      src/Muzich/HomeBundle/Resources/views/Home/index.html.twig

+ 48 - 0
src/Muzich/CoreBundle/Controller/CoreController.php Datei anzeigen

@@ -7,6 +7,9 @@ use Muzich\CoreBundle\lib\Controller;
7 7
 use Muzich\CoreBundle\Entity\FollowUser;
8 8
 use Muzich\CoreBundle\Entity\FollowGroup;
9 9
 //use Doctrine\ORM\Query;
10
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
11
+use Muzich\CoreBundle\ElementFactory\ElementManager;
12
+use Muzich\CoreBundle\Entity\Element;
10 13
 
11 14
 class CoreController extends Controller
12 15
 {
@@ -73,4 +76,49 @@ class CoreController extends Controller
73 76
     }
74 77
   }
75 78
   
79
+  public function elementAddAction()
80
+  {
81
+    $user = $this->getUser();
82
+    $em = $this->getDoctrine()->getEntityManager();
83
+    
84
+    $form = $this->createForm(
85
+      new ElementAddForm(),
86
+      array(),
87
+      array('tags' => $this->getTagsArray())
88
+    );
89
+    
90
+    if ($this->getRequest()->getMethod() == 'POST')
91
+    {
92
+      $form->bindRequest($this->getRequest());
93
+      if ($form->isValid())
94
+      {
95
+        $data = $form->getData();
96
+        $element = new Element();
97
+        
98
+        $factory = new ElementManager($element, $em, $this->container);
99
+        $factory->proceedFill($data, $user);
100
+        
101
+        $em->persist($element);
102
+        $em->flush();
103
+      }
104
+      
105
+    }
106
+    
107
+    if ($this->getRequest()->isXmlHttpRequest())
108
+    {
109
+      
110
+    }
111
+    else
112
+    {
113
+      return $this->redirect($this->generateUrl('home'));
114
+    }
115
+    
116
+  }
117
+  
118
+//  protected function proceedElement(Element $element)
119
+//  {
120
+//    $factory = new ElementFactory();
121
+//    $factory->proceed($element, $form->getData());
122
+//  }
123
+  
76 124
 }

+ 4 - 4
src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementData.php Datei anzeigen

@@ -76,10 +76,10 @@ class LoadElementData  extends AbstractFixture implements OrderedFixtureInterfac
76 76
     $joelle  = $this->entity_manager->merge($this->getReference('user_joelle'));
77 77
 
78 78
     // 'youtube', 'soundclound', 'son2teuf', 'jamendo'
79
-    $youtube     = $this->entity_manager->merge($this->getReference('element_type_youtube'));
80
-    $soundclound = $this->entity_manager->merge($this->getReference('element_type_soundclound'));
81
-    $son2teuf    = $this->entity_manager->merge($this->getReference('element_type_son2teuf'));
82
-    $jamendo     = $this->entity_manager->merge($this->getReference('element_type_jamendo'));
79
+    $youtube     = $this->entity_manager->merge($this->getReference('element_type_youtube.com'));
80
+    $soundclound = $this->entity_manager->merge($this->getReference('element_type_soundcloud.com'));
81
+    $son2teuf    = $this->entity_manager->merge($this->getReference('element_type_son2teuf.org'));
82
+    $jamendo     = $this->entity_manager->merge($this->getReference('element_type_jamendo.com'));
83 83
     
84 84
     $this->createElement('youtube_heretik_1', 'Heretik System Popof - Resistance', 
85 85
       'http://www.youtube.com/watch?v=tq4DjQK7nsM',

+ 3 - 3
src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementTypeData.php Datei anzeigen

@@ -39,10 +39,10 @@ class LoadElementTypeData  extends AbstractFixture implements OrderedFixtureInte
39 39
   public function load($entity_manager)
40 40
   {
41 41
     $this->entity_manager = $entity_manager;
42
-
42
+    
43 43
     foreach (array(
44
-      'youtube' => 'Youtube', 'soundclound' => 'SoundCloud', 
45
-      'son2teuf' => 'Son2Teuf', 'jamendo' => 'jamendo'
44
+      'youtube.com' => 'Youtube', 'soundcloud.com' => 'SoundCloud', 
45
+      'son2teuf.org' => 'Son2Teuf', 'jamendo.com' => 'jamendo'
46 46
       ) as $id => $name)
47 47
     {
48 48
       $this->createElementType($id, $name);

+ 144 - 0
src/Muzich/CoreBundle/ElementFactory/ElementManager.php Datei anzeigen

@@ -0,0 +1,144 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+use Muzich\CoreBundle\Entity\User;
7
+use Doctrine\ORM\EntityManager;
8
+use Symfony\Component\DependencyInjection\Container;
9
+
10
+use Muzich\CoreBundle\ElementFactory\Site\YoutubeFactory;
11
+
12
+/**
13
+ * 
14
+ *
15
+ * @author bux
16
+ */
17
+class ElementManager
18
+{
19
+  
20
+  protected $types = array(
21
+    'youtube.com', 
22
+    'soundcloud.com', 
23
+    'son2teuf.org', 
24
+    'jamendo.com'
25
+  );
26
+  
27
+  protected $em;
28
+  protected $element;
29
+  protected $container;
30
+  
31
+  /**
32
+   * Procédure chargé de retourner des information destiné au 
33
+   * formulaire d'ajout d'un element.
34
+   * 
35
+   * @param string $url
36
+   * @return array
37
+   */
38
+  public static function collect($url)
39
+  {
40
+    
41
+    return array(
42
+      'name' => null,
43
+      'tags' => array()
44
+    );
45
+  }
46
+  
47
+  public function __construct(Element $element, EntityManager $em, Container $container)
48
+  {
49
+    $this->element = $element;
50
+    $this->em = $em;
51
+    $this->container = $container;
52
+    
53
+    $evm = new \Doctrine\Common\EventManager();
54
+    $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
55
+    $evm->addEventSubscriber($timestampableListener);
56
+    
57
+    $this->em->getEventManager()->addEventSubscriber($timestampableListener);
58
+  }
59
+  
60
+  /**
61
+   * Procédure chargé de construire le contenu de l'élément.
62
+   *  nom, Code d'embed, [...]
63
+   * 
64
+   * @param Element $element
65
+   * @param array $params
66
+   * @param User $owner
67
+   * @return Element 
68
+   */
69
+  public function proceedFill($params, User $owner)
70
+  {
71
+    $this->element->setName($params['name']);
72
+    $this->element->setUrl($params['url']);
73
+    $this->element->setOwner($owner);
74
+    $this->element->setTagsWithIds($this->em, $params['tags']);
75
+    $this->determineType();
76
+    $this->proceedExtraFields();
77
+  }
78
+  
79
+  /**
80
+   * Determine le type d'objet auquel on a affaire.
81
+   */
82
+  protected function determineType()
83
+  {
84
+    preg_match("/^(http:\/\/)?([^\/]+)/i", $this->element->getUrl(), $chaines);
85
+    $host = $chaines[2];
86
+    // Repérer les derniers segments
87
+    preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$chaines);
88
+    
89
+    $type = null;
90
+    
91
+    
92
+    if (in_array($chaines[0], $this->types))
93
+    {
94
+      $type = $this->em->getRepository('MuzichCoreBundle:ElementType')->find($chaines[0]);
95
+    }
96
+    
97
+    $this->element->setType($type);
98
+  }
99
+  
100
+  /**
101
+   * Construction des autres champs tel que embed.
102
+   * 
103
+   * 
104
+   */
105
+  protected function proceedExtraFields()
106
+  {
107
+   
108
+    // Instanciation d'un objet factory correspondant au type, par exemple
109
+    // YoutubeFactory, qui répondant a une implementation retournera ces infos.
110
+  
111
+    if ($this->element->getType())
112
+    {
113
+      $site_factory = $this->getFactory();
114
+      $this->element->setEmbed($site_factory->getEmbedCode());
115
+    }
116
+    
117
+  }
118
+  
119
+  protected function getFactory()
120
+  { 
121
+    switch ($this->element->getType()->getId())
122
+    {
123
+      case 'youtube.com':
124
+        return new YoutubeFactory($this->element, $this->container);
125
+      break;
126
+      case 'soundcloud.com':
127
+        return new SoundCloudFactory($this->element, $this->container);
128
+      break;
129
+      case 'son2teuf.org':
130
+        return new Son2TeufFactory($this->element, $this->container);
131
+      break;
132
+      case 'jamendo.com':
133
+        return new JamendoFactory($this->element, $this->container);
134
+      break;
135
+    
136
+      default:
137
+        throw new Exception("La Factory n'est pas connu pour ce type.");
138
+      break;
139
+    }
140
+  }
141
+    
142
+}
143
+
144
+?>

+ 15 - 0
src/Muzich/CoreBundle/ElementFactory/Site/JamendoFactory.php Datei anzeigen

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site;
4
+
5
+/**
6
+ * 
7
+ *
8
+ * @author bux
9
+ */
10
+class JamendoFactory extends BaseFactory
11
+{
12
+  
13
+}
14
+
15
+?>

+ 15 - 0
src/Muzich/CoreBundle/ElementFactory/Site/Son2TeufFactory.php Datei anzeigen

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site;
4
+
5
+/**
6
+ * 
7
+ *
8
+ * @author bux
9
+ */
10
+class Son2TeufFactory extends BaseFactory
11
+{
12
+  
13
+}
14
+
15
+?>

+ 15 - 0
src/Muzich/CoreBundle/ElementFactory/Site/SoundCloudFactory.php Datei anzeigen

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site;
4
+
5
+/**
6
+ * 
7
+ *
8
+ * @author bux
9
+ */
10
+class SoundCloudFactory extends BaseFactory
11
+{
12
+  
13
+}
14
+
15
+?>

+ 17 - 0
src/Muzich/CoreBundle/ElementFactory/Site/YoutubeFactory.php Datei anzeigen

@@ -0,0 +1,17 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site;
4
+
5
+use Muzich\CoreBundle\ElementFactory\Site\base\VideoSiteFactory;
6
+
7
+/**
8
+ * 
9
+ *
10
+ * @author bux
11
+ */
12
+class YoutubeFactory extends VideoSiteFactory
13
+{
14
+  
15
+}
16
+
17
+?>

+ 30 - 0
src/Muzich/CoreBundle/ElementFactory/Site/base/BaseFactory.php Datei anzeigen

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site\base;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+use Symfony\Component\DependencyInjection\Container;
7
+use \Exception;
8
+
9
+/**
10
+ *
11
+ * @author bux
12
+ */
13
+class BaseFactory implements FactoryInterface
14
+{
15
+  
16
+  protected $element;
17
+  
18
+  public function __construct(Element $element, Container $container)
19
+  {
20
+    $this->element = $element;
21
+  }
22
+  
23
+  public function getEmbedCode()
24
+  {
25
+    return null;
26
+  }
27
+  
28
+}
29
+
30
+?>

+ 17 - 0
src/Muzich/CoreBundle/ElementFactory/Site/base/FactoryInterface.php Datei anzeigen

@@ -0,0 +1,17 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site\base;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+use Symfony\Component\DependencyInjection\Container;
7
+
8
+interface FactoryInterface
9
+{
10
+  
11
+  public function __construct(Element $element, Container $container);
12
+  
13
+  public function getEmbedCode();
14
+  
15
+}
16
+
17
+?>

+ 50 - 0
src/Muzich/CoreBundle/ElementFactory/Site/base/VideoSiteFactory.php Datei anzeigen

@@ -0,0 +1,50 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\Site\base;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+use Symfony\Component\DependencyInjection\Container;
7
+use Muzich\CoreBundle\ElementFactory\Site\base\BaseFactory;
8
+use Muzich\CoreBundle\ElementFactory\lib\VideoEmbed;
9
+
10
+/**
11
+ * 
12
+ *
13
+ * @author bux
14
+ */
15
+class VideoSiteFactory extends BaseFactory
16
+{
17
+  
18
+  protected $video_engine = null;
19
+  
20
+  public function __construct(Element $element, Container $container)
21
+  {
22
+    parent::__construct($element, $container);
23
+    
24
+    // Configuration de VideoEmbed
25
+    define('SITEBASE', $container->getParameter('sitebase'));
26
+    define('VIDEO_EMBED_CONFIG_FILE', SITEBASE.$container->getParameter('video_embed_config_file'));
27
+    //to activate debug mode and false for production usage. it will write 
28
+    //to a log file when something goes wrong but should not produce 
29
+    //exceptions in production enviroment
30
+    define('DEBUG', $container->getParameter('video_embed_debug')); 
31
+    
32
+    try {
33
+      $this->video_engine =  new VideoEmbed($this->element->getUrl());
34
+    } catch (Exception $exc) {
35
+      
36
+    }
37
+  }
38
+  
39
+  public function getEmbedCode()
40
+  {
41
+    if ($this->video_engine)
42
+    {
43
+      return $this->video_engine->embed;
44
+    }
45
+    return null;
46
+  }
47
+  
48
+}
49
+
50
+?>

Datei-Diff unterdrückt, da er zu groß ist
+ 1049 - 0
src/Muzich/CoreBundle/ElementFactory/lib/Spyc.php


+ 362 - 0
src/Muzich/CoreBundle/ElementFactory/lib/VideoEmbed.php Datei anzeigen

@@ -0,0 +1,362 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory\lib;
4
+
5
+//require_once (SITEBASE . '/src/Muzich/CoreBundle/ElementFactory/lib/spyc.php');
6
+use Muzich\CoreBundle\ElementFactory\lib\Spyc;
7
+use \Exception;
8
+
9
+/**
10
+* Copyright (c) 2008, AF83
11
+*   All rights reserved.
12
+*
13
+*   Redistribution and use in source and binary forms, with or without modification,
14
+*   are permitted provided that the following conditions are met:
15
+*
16
+*   1° Redistributions of source code must retain the above copyright notice,
17
+*   this list of conditions and the following disclaimer.
18
+*
19
+*   2° Redistributions in binary form must reproduce the above copyright notice,
20
+*   this list of conditions and the following disclaimer in the documentation
21
+*   and/or other materials provided with the distribution.
22
+*
23
+*   3° Neither the name of AF83 nor the names of its contributors may be used
24
+*   to endorse or promote products derived from this software without specific
25
+*   prior written permission.
26
+*
27
+*   THIS SOFTWARE IS PROVIDED BY THE COMPANY AF83 AND CONTRIBUTORS "AS IS"
28
+*   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29
+*   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30
+*   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31
+*   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32
+*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33
+*   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34
+*   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
35
+*   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36
+*   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37
+*   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
+*
39
+* @copyright 2008 AF83 http://dev.af83.com
40
+* @author Ori Pekelman
41
+* @license BSD License (3 Clause) http://www.opensource.org/licenses/bsd-license.php
42
+* @package Video
43
+* @version $Id$
44
+* @access public
45
+* @todo thumbnails should be cached locally
46
+*
47
+* VideoEmbed class  can be used to safely and cleanly embed videos from different sources
48
+*
49
+* The different sources are configured in the video_embed.yaml file
50
+*
51
+* NB: The embed code may be also a url
52
+*
53
+* Usage: set the configuration variables (refer to readme for details):
54
+*  VIDEO_EMBED_CONFIG_FILE -- path of video embed config file
55
+*
56
+*                     $videoEmbed = new VideoEmbed($embed); optional width and height may be passed to the constructor
57
+*                      //print($videoEmbed->embed);
58
+*                      $videoEmbed->width = 240; // resize
59
+*                      $videoEmbed->height = 120;
60
+*                      //print($videoEmbed->embed);
61
+*                      //print($videoEmbed->thumb);
62
+
63
+*
64
+* This class requires the SpyC library to read the cobnfiguration file. please adjust the path of the include on the first line of this file
65
+* The video services are configured in the configuration file (video_embed.conf.inc), example for youtube google and daily motion:
66
+*/
67
+
68
+class VideoEmbed {
69
+    private $_video_embed_conf = array();
70
+    private $_embedSource;
71
+    private $_embed;
72
+    private $_id;
73
+    private $_type;
74
+    private $_thumb;
75
+    private $_url;
76
+    private $_width;
77
+    private $_height;
78
+    private $_readOnly = array ('thumb', 'url', 'id', 'type');
79
+    private $_readWrite = array ('width', 'height', 'embed');
80
+
81
+    /**
82
+    * VideoEmbed::__construct() Only embed is mandatory
83
+    *
84
+    * @param mixed $embed
85
+    * @param mixed $width
86
+    * @param mixed $height
87
+    */
88
+    function __construct($embed, $width = null, $height = null)
89
+    {
90
+        // load configuration
91
+//        if (!class_exists('Spyc')) {
92
+//            throw new exception ('Could not find SpyC library ');
93
+//        }
94
+            
95
+        $this->_video_embed_conf = Spyc::YAMLLoad(VIDEO_EMBED_CONFIG_FILE);
96
+        
97
+        if (!$this->_video_embed_conf) {
98
+            debug_log ("Could not read configruation file or config file empty  " . SITE_BASE . VIDEO_EMBED_CONFIG_FILE);
99
+            if (DEBUG) {
100
+                throw new exception ("Could not read configruation file or config file empty " . SITE_BASE . VIDEO_EMBED_CONFIG_FILE);
101
+            }
102
+        }
103
+
104
+        if (!$embed) {
105
+            debug_log ('This must be instantiated with a non empty embed code');
106
+            if (DEBUG) {
107
+                throw new exception ('This must be instantiated with a non empty embed code');
108
+            }
109
+        }
110
+        // load arguments
111
+        $this->_embedSource = $embed;
112
+        $this->_width = $width?$width: $this->_video_embed_conf['defaultWidth'];
113
+        $this->_height = $height?$height:$this->_video_embed_conf['defaultHeight'];
114
+
115
+        $this->setup();
116
+    }
117
+    /**
118
+    * VideoEmbed::__set() Make some variables read only
119
+    *
120
+    * @param mixed $n
121
+    * @param mixed $val
122
+    * @return
123
+    */
124
+    function __set($n, $val)
125
+    {
126
+        if (in_array($n, $this->_readOnly)) {
127
+            debug_log ("Trying to set a read only property $n $val");
128
+            if (DEBUG) {
129
+                throw new exception ("Trying to set  a read only property" . "$n $val");
130
+            }
131
+            return false;
132
+        } elseif (in_array($n, $this->_readWrite)) {
133
+            if ($n == "embed") {
134
+                $property = '_embedSource';
135
+            } else $property = "_" . $n;
136
+
137
+            $this->$property = $val;
138
+            $this->setup(); // recalculate stuff if we changed a RW  property
139
+            return true;
140
+        }
141
+        return false;
142
+    }
143
+
144
+    /**
145
+    * VideoEmbed::__get()
146
+    *
147
+    * @param mixed $n
148
+    * @return
149
+    */
150
+    function __get($n)
151
+    {
152
+        if (in_array($n, array_merge($this->_readOnly, $this->_readWrite))) {
153
+            $propertyName = "_$n";
154
+            return $this->$propertyName;
155
+        } else {
156
+            debug_log('Trying to get a non readble property ' . $n);
157
+            if (DEBUG) {
158
+                throw new exception ('Trying to get a non readble property ' . $n);
159
+            }
160
+            return false;
161
+        }
162
+    }
163
+
164
+    /**
165
+    * VideoEmbed::setup()
166
+    *
167
+    * @return
168
+    */
169
+    private function setup ()
170
+    {      
171
+        if (!$this->video_embed_type()) {
172
+            //debug_log ('Could not get embed type :' . $this->_embedSource);
173
+            if (DEBUG) {
174
+                throw new exception ('Could not get embed type :' . $this->_embedSource);
175
+            }
176
+        }
177
+        if (!$this->video_embed_id()) {
178
+            //debug_log ('Could not get embed id :' . $this->_embedSource);
179
+            if (DEBUG) {
180
+                throw new exception ('Could not get embed id :' . $this->_embedSource);
181
+            }
182
+        }
183
+        if (!$this->video_embed_url()) {
184
+            //debug_log ('Problem generating embed url :' . $this->_embedSource);
185
+            if (DEBUG) {
186
+                throw new exception ('Problem generating embed url :' . $this->_embedSource);
187
+            }
188
+        }
189
+        if (!$this->video_embed_thumb()) {
190
+            //debug_log ('Problem generating thumb code :' . $this->_embedSource);
191
+            if (DEBUG) {
192
+                throw new exception ('Problem generatingembed code :' . $this->_embed);
193
+            }
194
+        }
195
+        if (!$this->video_embed_embed()) {
196
+            //debug_log ('Problem generating embed code :' . $this->_embedSource);
197
+            if (DEBUG) {
198
+                throw new exception ('Problem generatingembed code :' . $this->_embedSource);
199
+            }
200
+        }
201
+    }
202
+
203
+    /**
204
+    * VideoEmbed::video_emebd_type()
205
+    *
206
+    * @param mixed $embed
207
+    * @return
208
+    */
209
+    private function video_embed_type()
210
+    {
211
+        $type = null;
212
+        $this->_type = "";
213
+        foreach($this->_video_embed_conf['services'] as $serviceName => $config) {
214
+            if (strpos(strtolower($this->_embedSource), strtolower($config['urlPattern']))) {
215
+                $type = $serviceName;
216
+            }
217
+        }
218
+        if ($type) {
219
+            $this->_type = $type;
220
+            return $this->_type;
221
+        }
222
+        return false;
223
+    }
224
+
225
+    /**
226
+    * VideoEmbed::video_embed_id()
227
+    *
228
+    * @return
229
+    */
230
+    private function video_embed_id()
231
+    {
232
+        $this->_id = "";
233
+
234
+        if (($this->_type)) {
235
+            $regexp = $this->_video_embed_conf['services'][$this->_type]['extractPattern'];
236
+            preg_match($regexp, $this->_embedSource , $match);
237
+
238
+            if ($match[count($match)-1]) $this->_id = $match[count($match)-1];
239
+            return $this->_id;
240
+        }
241
+        return false;
242
+    }
243
+
244
+    /**
245
+    * VideoEmbed::video_embed_url()
246
+    *
247
+    * @return
248
+    */
249
+    private function video_embed_url()
250
+    {
251
+        $this->_url = "";
252
+        if ($this->_type) {
253
+            if ($this->_id) $url = sprintf($this->_video_embed_conf['services'][$this->_type]['embedUrlTemplate'], $this->_id);
254
+            if ($url) $this->_url = $url;
255
+            return $this->_url;
256
+        }
257
+        return false;
258
+    }
259
+
260
+    /**
261
+    * VideoEmbed::video_embed_thumb()
262
+    *
263
+    * @return
264
+    */
265
+    private function video_embed_thumb()
266
+    {
267
+        $conf = $this->_video_embed_conf['services'][$this->_type]; // just here for readability
268
+        $this->_thumb = "";
269
+        if ($this->_type && $this->_id) {
270
+            if (isset($conf['thumbnailUrlExtractPattern'])) { // if we need to parse the response:
271
+                $thumburl = $this->extractThumbUrl($conf['thumbnailUrlTemplate'], $conf['thumbnailUrlExtractPattern']);
272
+            } else $thumburl = sprintf($conf['thumbnailUrlTemplate'], $this->_id);
273
+
274
+            if ($thumburl) $this->_thumb = $thumburl;
275
+            return $this->_thumb;
276
+        }
277
+        return false;
278
+    }
279
+
280
+    /**
281
+    * VideoEmbed::video_embed_embed()
282
+    *
283
+    * @param mixed $width
284
+    * @param mixed $height
285
+    * @return
286
+    */
287
+    private function video_embed_embed()
288
+    {
289
+        if ($this->_type) {
290
+            $template = isset($this->_video_embed_conf['services'][$this->_type]['embedTemplate']) ? $this->_video_embed_conf['services'][$this->_type]['embedTemplate']:$this->_video_embed_conf['embedTemplate'];
291
+            if ($template && $this->_url) {
292
+                $width = $this->_width?$this->_width:$this->_video_embed_conf['services'][$video_type]['defaultWidth'];
293
+                $height = $this->_height?$this->_height:$this->_video_embed_conf['services'][$video_type]['defaultHeight'];
294
+                $embed = sprintf($template, $this->_url, $width, $height);
295
+                if ($embed) $this->_embed = $embed;
296
+                return $this->_embed;
297
+            }
298
+        }
299
+
300
+        return false;
301
+    }
302
+    /**
303
+    * extractThumbUrl()
304
+    *
305
+    * @param mixed $url
306
+    * @param mixed $videoid
307
+    * @param mixed $extractPattern
308
+    * @return
309
+    */
310
+    private function extractThumbUrl($url, $extractPattern)
311
+    {
312
+        $vrss = file_get_contents(sprintf($url, $this->_id));
313
+        if (!empty($vrss)) {
314
+            preg_match($extractPattern, $vrss, $thumbnail_array);
315
+            $thumbnail = $thumbnail_array[1];
316
+            // Remove amp;
317
+            $thumbnail = str_replace('amp;', '', $thumbnail);
318
+        }
319
+
320
+        return $thumbnail;
321
+    }
322
+}
323
+
324
+/**
325
+* test_VideoEmbed() test all is well
326
+*
327
+* @return
328
+*/
329
+function test_VideoEmbed()
330
+{
331
+    $embeds[] = '<div><object width="420" height="365"><param name="movie" value="http://www.dailymotion.com/swf/x3wq9a&v3=1&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/x3wq9a&v3=1&related=0" type="application/x-shockwave-flash" width="420" height="365" allowFullScreen="true" allowScriptAccess="always"></embed></object><br /><b><a href="http://www.dailymotion.com/video/x3wq9a_defi-decorer-les-camions-de-pq_fun">Defi: décorer les camions de PQ</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/gonzaguetv">gonzaguetv</a></i></div>';
332
+    $embeds[] = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/1mXh_tyLlAY&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/1mXh_tyLlAY&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>';
333
+    $embeds[] = '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-1182786924290841590&hl=fr" flashvars=""> </embed>';
334
+    foreach($embeds as $embed) {
335
+        $videoEmbed = new VideoEmbed($embed, 420, 365);
336
+        
337
+        // Mise en commentaire pour opasser le pre-commit:
338
+        
339
+//        echo "<h2> Type:</h2>\n";
340
+//        print ($videoEmbed->type);
341
+//        echo "<h2> ID:</h2>\n";
342
+//        print($videoEmbed->id);
343
+//        echo "<h2> Video Url:</h2>\n";
344
+//        print($videoEmbed->url);
345
+//        echo "<h2> Embed:</h2>\n";
346
+//        print($videoEmbed->embed);
347
+//        echo "<h2> Embed resized:</h2>\n";
348
+//        $videoEmbed->width = 240;
349
+//        $videoEmbed->height = 120;
350
+//        print($videoEmbed->embed);
351
+//        echo "<h2> thumb url:</h2>\n";
352
+//        print($videoEmbed->thumb);
353
+//        echo "<h2> thumb image:</h2>\n";
354
+//        print('<img src="' . $videoEmbed->thumb . '" />');
355
+        
356
+        
357
+        // this should fail
358
+        // $videoEmbed->thumb = "qsdqsdsq";
359
+        // this should fail
360
+        // print('<img src="' . $videoEmbed->image . '" />');
361
+    }
362
+}

+ 48 - 3
src/Muzich/CoreBundle/Entity/Element.php Datei anzeigen

@@ -5,7 +5,7 @@ namespace Muzich\CoreBundle\Entity;
5 5
 use Doctrine\ORM\Mapping as ORM;
6 6
 use \Doctrine\Common\Collections\ArrayCollection;
7 7
 use Gedmo\Mapping\Annotation as Gedmo;
8
-
8
+use Doctrine\ORM\EntityManager;
9 9
 
10 10
 /**
11 11
  * L'Element est l'Element central de l'application. C'est cet
@@ -84,6 +84,14 @@ class Element
84 84
   protected $name;
85 85
   
86 86
   /**
87
+   * Code d'embed
88
+   * 
89
+   * @ORM\Column(type="text", nullable=true)
90
+   * @var type string
91
+   */
92
+  protected $embed;
93
+  
94
+  /**
87 95
    * @var datetime $created
88 96
    *
89 97
    * @Gedmo\Timestampable(on="create")
@@ -155,7 +163,7 @@ class Element
155 163
    *
156 164
    * @param ElementType $type
157 165
    */
158
-  public function setType(ElementType $type)
166
+  public function setType(ElementType $type = null)
159 167
   {
160 168
     $this->type = $type;
161 169
   }
@@ -171,9 +179,10 @@ class Element
171 179
   }
172 180
   
173 181
   
174
-  public function __construct()
182
+  public function __construct($url = null)
175 183
   {
176 184
     $this->tags = new ArrayCollection();
185
+    $this->url = $url;
177 186
   }
178 187
   
179 188
   /**
@@ -258,6 +267,26 @@ class Element
258 267
   }
259 268
 
260 269
   /**
270
+   * Set embed
271
+   *
272
+   * @param string $code
273
+   */
274
+  public function setEmbed($code)
275
+  {
276
+      $this->embed = $code;
277
+  }
278
+
279
+  /**
280
+   * Get embed
281
+   *
282
+   * @return string 
283
+   */
284
+  public function getEmbed()
285
+  {
286
+      return $this->embed;
287
+  }
288
+
289
+  /**
261 290
    * Set created
262 291
    *
263 292
    * @param date $created
@@ -296,4 +325,20 @@ class Element
296 325
   {
297 326
       return $this->updated;
298 327
   }
328
+  
329
+  /**
330
+   * Ajoute des relation vers des tags.
331
+   * 
332
+   * @param array $ids 
333
+   */
334
+  public function setTagsWithIds(EntityManager $em, $ids)
335
+  {
336
+    $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
337
+
338
+    // Pour les nouveaux ids restants
339
+    foreach ($tags as $tag)
340
+    {      
341
+      $this->addTag($tag);
342
+    }
343
+  }
299 344
 }

+ 1 - 1
src/Muzich/CoreBundle/Entity/ElementType.php Datei anzeigen

@@ -19,7 +19,7 @@ class ElementType
19 19
   
20 20
   /**
21 21
    * @ORM\Id
22
-   * @ORM\Column(type="string", length=12)
22
+   * @ORM\Column(type="string", length=18)
23 23
    * @var type string
24 24
    */
25 25
   protected $id;

+ 41 - 0
src/Muzich/CoreBundle/Form/Element/ElementAddForm.php Datei anzeigen

@@ -0,0 +1,41 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\Element;
4
+
5
+use Symfony\Component\Form\AbstractType;
6
+use Symfony\Component\Form\FormBuilder;
7
+
8
+class ElementAddForm extends AbstractType
9
+{
10
+  public function buildForm(FormBuilder $builder, array $options)
11
+  {
12
+    $builder->add('name', 'text', array(
13
+      'required' => true,
14
+    ));
15
+    
16
+    $builder->add('url', 'text', array(
17
+      'required' => true,
18
+    ));
19
+        
20
+    $builder->add('tags', 'choice', array(
21
+      'choices'           => $options['tags'],
22
+      'expanded'          => true,
23
+      'multiple'          => true
24
+    ));
25
+  }
26
+
27
+  public function getName()
28
+  {
29
+    return 'element_add';
30
+  }
31
+  
32
+  public function getDefaultOptions(array $options)
33
+  {
34
+    return array(
35
+      'name' => '',
36
+      'url' => '',
37
+      'tags' => array(),
38
+      'data_class' => 'Muzich\CoreBundle\Entity\Element'
39
+    );
40
+  }
41
+}

+ 1 - 1
src/Muzich/CoreBundle/Repository/ElementRepository.php Datei anzeigen

@@ -78,7 +78,7 @@ class ElementRepository extends EntityRepository
78 78
     $query_string = "SELECT e, et, t2, eu, g
79 79
       FROM MuzichCoreBundle:Element e 
80 80
       LEFT JOIN e.group g 
81
-      JOIN e.type et 
81
+      LEFT JOIN e.type et 
82 82
       LEFT JOIN e.tags t 
83 83
       LEFT JOIN e.tags t2 
84 84
       JOIN e.owner eu $join_personal

+ 8 - 5
src/Muzich/CoreBundle/Resources/config/routing.yml Datei anzeigen

@@ -1,11 +1,14 @@
1 1
 
2 2
 
3 3
 search_elements:
4
-   pattern:  /search-elements
5
-   defaults: { _controller: MuzichCoreBundle:Search:searchElements }
4
+  pattern:  /search-elements
5
+  defaults: { _controller: MuzichCoreBundle:Search:searchElements }
6 6
    
7 7
 follow:
8
-   pattern:  /follow/{type}/{id}/{token}
9
-   defaults: { _controller: MuzichCoreBundle:Core:follow }
8
+  pattern:  /follow/{type}/{id}/{token}
9
+  defaults: { _controller: MuzichCoreBundle:Core:follow }
10 10
    
11
-     
11
+element_add:
12
+  pattern:  /element/add
13
+  defaults: { _controller: MuzichCoreBundle:Core:elementAdd }
14
+  

+ 28 - 0
src/Muzich/CoreBundle/Resources/config/video_embed.yaml Datei anzeigen

@@ -0,0 +1,28 @@
1
+---
2
+embedTemplate: <object width="%2$s" height="%3$s" ><param name="movie" value="%1$s"></param><param name="wmode" value="transparent"></param><embed src="%1$s" type="application/x-shockwave-flash" wmode="transparent" width="%2$s" height="%3$s"></embed></object>
3
+defaultWidth: 425
4
+defaultHeight: 350
5
+services:
6
+    youtube:
7
+        urlPattern: youtube.com
8
+        embedUrlTemplate: http://www.youtube.com/v/%1$s&rel=1
9
+        thumbnailUrlTemplate: http://i.ytimg.com/vi/%1$s/default.jpg
10
+        extractPattern: /youtube\.com\/(v\/|watch\?v=)([\w\-]+)/
11
+        apiUrl: http://www.youtube.com/api2_rest
12
+        defaultWidth: 425
13
+        defaultHeight: 350
14
+    google:
15
+        urlPattern: video.google
16
+        extractPattern: /docid=([^&]*)/i
17
+        embedUrlTemplate: http://video.google.com/googleplayer.swf?docId=%1$s
18
+        thumbnailUrlTemplate: http://video.google.com/videofeed?docid=%s
19
+        thumbnailUrlExtractPattern: '/<media:thumbnail url="([^"]+)/'
20
+        defaultWidth: 400
21
+        defaultHeight: 326
22
+    dailymotion:
23
+        urlPattern: dailymotion.com
24
+        embedUrlTemplate: http://www.dailymotion.com/swf/%1$s/
25
+        thumbnailUrlTemplate: http://www.dailymotion.com/thumbnail/160x120/video/%1$s/
26
+        extractPattern: '#/(video|swf)/([a-zA-Z0-9]+)[^a-zA-Z0-9]#'
27
+        defaultWidth: 425
28
+        defaultHeight: 350

+ 6 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/default.html.twig Datei anzeigen

@@ -14,6 +14,12 @@
14 14
           {% endfor %} 
15 15
         </ul>
16 16
         
17
+        {% if element.embed %}
18
+          {% autoescape false %}
19
+            {{ element.embed }}
20
+          {% endautoescape %}
21
+        {% endif %}
22
+        
17 23
     </li>
18 24
   {% endfor %} 
19 25
 </ul>

+ 9 - 1
src/Muzich/HomeBundle/Controller/HomeController.php Datei anzeigen

@@ -7,6 +7,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 
8 8
 use Doctrine\ORM\Query;
9 9
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
10
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
10 11
 
11 12
 class HomeController extends Controller
12 13
 {
@@ -21,10 +22,17 @@ class HomeController extends Controller
21 22
     $search_form = $this->createForm(
22 23
       new ElementSearchForm(), 
23 24
       $search_object->getParams(),
24
-      array('tags' => $this->getTagsArray())
25
+      array('tags' => $tags = $this->getTagsArray())
26
+    );
27
+    
28
+    $add_form = $this->createForm(
29
+      new ElementAddForm(),
30
+      array(),
31
+      array('tags' => $tags)
25 32
     );
26 33
         
27 34
     return array(
35
+      'add_form' => $add_form->createView(),
28 36
       'search_object' => $search_object,
29 37
       'search_form'   => $search_form->createView()
30 38
     );

+ 14 - 0
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig Datei anzeigen

@@ -4,6 +4,20 @@
4 4
 
5 5
 {% block content %}
6 6
 
7
+  <form action="{{ path('element_add') }}" method="post" {{ form_enctype(add_form) }}>
8
+    {{ form_errors(add_form) }}
9
+
10
+    {{ form_row(add_form.name) }}
11
+      
12
+    {{ form_row(add_form.url) }}
13
+      
14
+    {{ form_row(add_form.tags) }}
15
+
16
+    {{ form_rest(add_form) }}
17
+
18
+    <input type="submit" />
19
+  </form>
20
+
7 21
   {% include "MuzichCoreBundle:SearchElement:form.html.twig" %}
8 22
 
9 23
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}