Last active
December 19, 2015 12:49
-
-
Save stefanotorresi/5957721 to your computer and use it in GitHub Desktop.
data aware widget with zf2 and doctrine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MySlideshow; | |
return array( | |
'service_manager' => array( | |
'factories' => array( | |
__NAMESPACE__ . '\SlideService' => __NAMESPACE__ . '\Service\SlideServiceFactory', | |
), | |
'aliases' => array( | |
'SlideService' => __NAMESPACE__ . '\SlideService', | |
) | |
), | |
'view_helpers' => array( | |
'factories' => array( | |
'slideShow' => __NAMESPACE__ . '\Service\SlideshowViewHelperFactory', | |
), | |
), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Stefano Torresi (http://stefanotorresi.it) | |
* @license See the file LICENSE.txt for copying permission. | |
* ************************************************ | |
*/ | |
namespace MySlideshow; | |
use MyBase\Entity\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="mss_slides") | |
*/ | |
class Slide extends Entity | |
{ | |
/** | |
* @var string | |
* @ORM\Column(type="string") | |
*/ | |
protected $file; | |
/** | |
* @var string | |
* @ORM\Column(type="string", nullable=true) | |
*/ | |
protected $url; | |
/** | |
* The alternative text | |
* | |
* @var string | |
* @ORM\Column(type="string", nullable=true) | |
*/ | |
protected $alt; | |
/** | |
* @param string $alt | |
* @return $this | |
*/ | |
public function setAlt($alt) | |
{ | |
$this->alt = $alt; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getAlt() | |
{ | |
return $this->alt; | |
} | |
/** | |
* @param string $file | |
* @return $this | |
*/ | |
public function setFile($file) | |
{ | |
$this->file = $file; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
/** | |
* @param string $url | |
* @return $this | |
*/ | |
public function setUrl($url) | |
{ | |
$this->url = $url; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUrl() | |
{ | |
return $this->url; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Stefano Torresi (http://stefanotorresi.it) | |
* @license See the file LICENSE.txt for copying permission. | |
* ************************************************ | |
*/ | |
namespace MySlideshow\Service; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\Common\Persistence\ObjectRepository; | |
use MySlideshow\Slide; | |
class SlideService | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
protected $objectManager; | |
/** | |
* @var ObjectRepository | |
*/ | |
protected $objectRepository; | |
public function __construct(ObjectManager $objectManager) | |
{ | |
$this->objectManager = $objectManager; | |
$this->objectRepository = $objectManager->getRepository(Slide::fqcn()); | |
} | |
/** | |
* @param array $data | |
* @return Slide | |
*/ | |
public function create($data) | |
{ | |
$slide = new Slide(); | |
$slide->setFile(realpath($data['image-file']['tmp_name'])); | |
$slide->setAlt($data['alt']); | |
$slide->setUrl($data['url']); | |
$this->objectManager->persist($slide); | |
$this->objectManager->flush(); | |
return $slide; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function findAll() | |
{ | |
return $this->objectRepository->findAll(); | |
} | |
/** | |
* @param integer $id | |
* @return object | |
*/ | |
public function find($id) | |
{ | |
return $this->objectRepository->find($id); | |
} | |
/** | |
* @param Slide $slide | |
* @return $this | |
*/ | |
public function update(Slide $slide) | |
{ | |
$this->objectManager->flush($slide); | |
return $this; | |
} | |
/** | |
* @param Slide $slide | |
* @return $this | |
*/ | |
public function delete(Slide $slide) | |
{ | |
$this->objectManager->remove($slide); | |
$this->objectManager->flush(); | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Stefano Torresi (http://stefanotorresi.it) | |
* @license See the file LICENSE.txt for copying permission. | |
* ************************************************ | |
*/ | |
namespace MySlideshow\Service; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class SlideServiceFactory implements FactoryInterface | |
{ | |
/** | |
* Create service | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @return SlideService | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
/** @var ObjectManager $objectManager */ | |
$objectManager = $serviceLocator->get('Doctrine\ORM\EntityManager'); | |
$slideService = new SlideService($objectManager); | |
return $slideService; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Stefano Torresi (http://stefanotorresi.it) | |
* @license See the file LICENSE.txt for copying permission. | |
* ************************************************ | |
*/ | |
namespace MySlideshow; | |
use MySlideshow\Service\SlideService; | |
use Zend\View\Helper\AbstractHtmlElement; | |
class SlideshowViewHelper extends AbstractHtmlElement | |
{ | |
/** | |
* @var SlideService | |
*/ | |
protected $slideService; | |
/** | |
* @var array | |
*/ | |
protected $options = array( | |
'attributes' => array( | |
'class' => 'slideshow', | |
), | |
'slide_attributes' => array( | |
'class' => 'slide', | |
), | |
'image_attributes' => array(), | |
'link_attributes' => array(), | |
'resize_options' => array(), | |
); | |
/** | |
* @param SlideService $slideService | |
*/ | |
public function __construct(SlideService $slideService) | |
{ | |
$this->slideService = $slideService; | |
} | |
/** | |
* @param array $options | |
* @return $this | |
*/ | |
public function __invoke($options = array()) | |
{ | |
$this->options = array_merge_recursive($this->options, $options); | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function openTag() | |
{ | |
$this->getView()->inlineScript() | |
->appendFile($this->getView()->basePath('/js/vendor/myslider/jquery.myslider.js')); | |
$this->getView()->headLink() | |
->appendStylesheet($this->getView()->basePath('/js/vendor/myslider/jquery.myslider.css')); | |
$attributes = $this->htmlAttribs($this->options['attributes']); | |
return sprintf("<div%s>", $attributes) . PHP_EOL; | |
} | |
/** | |
* @return string | |
*/ | |
public function slides() | |
{ | |
$slideAttribs = $this->options['slide_attributes']; | |
$linkAttribs = $this->options['link_attributes']; | |
$markup = ''; | |
foreach($this->getSlides() as $slide) /** @var Slide $slide */ { | |
$slideAttribs['data-id'] = $slide->getId(); | |
$imgAttribs['src'] = $this->getView()->imageResize($slide->getFile(), $this->options['resize_options']); | |
$imgAttribs['alt'] = $slide->getAlt(); | |
$linkAttribs['href'] = $slide->getUrl(); | |
// attributes passed via the helper options array do override actual image properties. | |
$imgAttribs = array_merge($imgAttribs, $this->options['image_attributes']); | |
$format = '<figure%s><img%s></figure>'; | |
$args = array( | |
$this->htmlAttribs($slideAttribs), | |
$this->htmlAttribs($imgAttribs) | |
); | |
if(!empty($linkAttribs['href'])) { | |
$format = '<a%3$s>'.$format.'</a>'; | |
$args[] = $this->htmlAttribs($linkAttribs); | |
} | |
$markup .= vsprintf($format, $args) . PHP_EOL; | |
} | |
return $markup; | |
} | |
/** | |
* Return a closing label tag | |
* | |
* @return string | |
*/ | |
public function closeTag() | |
{ | |
return '</div>' . PHP_EOL; | |
} | |
/** | |
* @return array | |
*/ | |
protected function getSlides() | |
{ | |
$slides = $this->slideService->findAll(); | |
if (!$slides) { | |
return array(); | |
} | |
return $slides; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->openTag() . $this->slides() . $this->closeTag(); | |
} | |
/** | |
* @param integer $width | |
* @return $this | |
*/ | |
public function setWidth($width) | |
{ | |
$this->options['resize_options']['width'] = $width; | |
return $this; | |
} | |
/** | |
* @param integer $height | |
* @return $this | |
*/ | |
public function setHeight($height) | |
{ | |
$this->options['resize_options']['height'] = $height; | |
return $this; | |
} | |
/** | |
* @param string $mode | |
* @return $this | |
*/ | |
public function setMode($mode) | |
{ | |
$this->options['resize_options']['mode'] = $mode; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Stefano Torresi (http://stefanotorresi.it) | |
* @license See the file LICENSE.txt for copying permission. | |
* ************************************************ | |
*/ | |
namespace MySlideshow\Service; | |
use MySlideshow\SlideshowViewHelper; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class SlideshowViewHelperFactory implements FactoryInterface | |
{ | |
/** | |
* Create service | |
* | |
* @param ServiceLocatorInterface $serviceLocator | |
* @return SlideshowViewHelper | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
$slideService = $serviceLocator->getServiceLocator()->get('SlideService'); | |
$helper = new SlideshowViewHelper($slideService); | |
return $helper; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment