Development

#2470: sfSimpleCMSSlotMediaLibrary.class.php

You must first sign up to be able to contribute.

Ticket #2470: sfSimpleCMSSlotMediaLibrary.class.php

File sfSimpleCMSSlotMediaLibrary.class.php, 2.9 kB (added by Rob.Sworder, 1 year ago)
Line 
1 <?php
2
3 /**
4  * MediaLibrary slot class, to be used by the sfSimpleCMSHelper.
5  * The slot must contain an array of image parameters, in YAML format
6  * <code>
7  * // If the slot value is
8  * src:    /foo/bar.png
9  * alt:    My alt text
10  * [legend is optional (overrides alt)]
11  * legend: Me, myself and I
12  *
13  * // Then the getSlotValue() method will return
14  * <a href="http://me.com">
15  * <img src="/foo/bar.png" alt="Me, myself and I" />Me, myself and I
16  * <p class="image_legend">Me, myself and I</p>
17  * </a>
18  *
19  * or, if legend omitted :-
20  * <a href="http://me.com">
21  * <img src="/foo/bar.png" alt="My alt text" />
22  * </a>
23  * </code>
24  */
25 class sfSimpleCMSSlotMediaLibrary implements sfSimpleCMSSlotTypeInterface
26 {
27   public function getSlotValue($slot)
28   {
29     sfLoader::loadHelpers(array('Asset', 'Url', 'Tag'));
30     $params = sfYaml::load($slot->getValue());
31     $res = '';
32     if(isset($params['legend']))
33     {
34       $res .= content_tag('p', $params['legend'], array('class' => 'image_legend'));
35       $params['alt'] = $params['legend'];
36       unset($params['legend']);
37     }
38     if(isset($params['url']))
39     {
40       $url = $params['url'];
41       unset($params['url']);
42     }
43     if(!isset($params['src']))
44     {
45       return sprintf('<strong>Error</strong>: The value of slot %s in incorrect. The image has no source', $slot->getName());
46     }
47     $src = $params['src'];
48     unset($params['src']);
49     $res = image_tag($src, $params).$res;
50     if(isset($url))
51     {
52       return link_to($res, $url);
53     }
54     else
55     {
56       return $res;
57     }
58   }
59
60   public function getSlotEditor($slot)
61   {
62     $params = sfYaml::load($slot->getValue());
63     $params['form_name'] = "edit_".$slot->getName();
64     return $this->getControlForAsset('src','MediaLibrary', $params).
65             $this->getControlFor('alt','Alt text', $params).
66             $this->getControlFor('legend','Legend', $params).
67             $this->getControlFor('url','Url', $params);
68   }
69  
70   private function getControlFor($name, $label, $params)
71   {
72     sfLoader::loadHelpers(array('Form', 'Tag', 'I18N'));
73     return content_tag('div', label_for($name, __($label)).input_tag($name, isset($params[$name]) ? $params[$name] : ''), array('class' =>  'image_slot_form'));
74   }
75   private function getControlForAsset($name, $label, $params)
76   {
77     sfLoader::loadHelpers(array('Form', 'Tag', 'I18N', 'sfMediaLibrary'));
78     return content_tag('div', label_for($name, __($label)).input_asset_tag($name, isset($params[$name]) ? $params[$name] : '', array('form_name'=>$params['form_name'])), array('class' =>  'image_slot_form'));
79   }
80  
81   public function getSlotValueFromRequest($request)
82   {
83     $params = array();
84     $params['src'] = $request->getParameter('src');
85     $params['alt'] = $request->getParameter('alt');
86     $params['legend'] = $request->getParameter('legend');
87     $params['url'] = $request->getParameter('url');
88     
89     return sfYaml::dump($params);
90   }
91 }
92