44 *
55 * @author shea@livesource.co.nz
66 **/
7- class ShortcodableController extends Controller
7+ class ShortcodableController extends LeftAndMain
88{
9+ /**
10+ * @var string
11+ */
12+ const URLSegment = 'ShortcodableController ' ;
13+
914 /**
1015 * @var array
1116 */
1217 private static $ allowed_actions = array (
13- 'ShortcodeForm ' ,
14- 'shortcodePlaceHolder ' ,
18+ 'ShortcodeForm ' => 'ADMIN ' ,
19+ 'index ' => 'ADMIN ' ,
20+ 'handleEdit ' => 'ADMIN ' ,
21+ 'shortcodePlaceHolder ' => 'ADMIN '
1522 );
1623
1724 /**
18- * Provides a GUI for the insert/edit shortcode popup.
19- *
20- * @return Form
21- **/
22- public function ShortcodeForm ()
25+ * @var array
26+ */
27+ private static $ url_handlers = array (
28+ 'edit/$ShortcodeType!/$Action//$ID/$OtherID ' => 'handleEdit '
29+ );
30+
31+ /**
32+ * @var string
33+ */
34+ protected $ shortcodableclass ;
35+
36+ /**
37+ * @var boolean
38+ */
39+ protected $ isnew = true ;
40+
41+ /**
42+ * @var array
43+ */
44+ protected $ shortcodedata ;
45+
46+ /**
47+ * Get the shortcodable class by whatever means possible.
48+ * Determine if this is a new shortcode, or editing an existing one.
49+ */
50+ function init ()
2351 {
24- if (!Permission::check ('CMS_ACCESS_CMSMain ' )) {
25- return ;
52+ parent ::init ();
53+ if ($ data = $ this ->getShortcodeData ()) {
54+ $ this ->isnew = false ;
55+ $ this ->shortcodableclass = $ data ['name ' ];
56+ } elseif ($ type = $ this ->request ->requestVar ('ShortcodeType ' )) {
57+ $ this ->shortcodableclass = $ type ;
58+ } else {
59+ $ this ->shortcodableclass = $ this ->request ->param ('ShortcodeType ' );
2660 }
61+ }
2762
28- Config::inst ()->update ('SSViewer ' , 'theme_enabled ' , false );
29-
30- // create a list of shortcodable classes for the ShortcodeType dropdown
31- $ classList = ShortCodable::get_shortcodable_classes ();
32- $ classes = array ();
33- foreach ($ classList as $ class ) {
34- $ classes [$ class ] = singleton ($ class )->hasMethod ('singular_name ' ) ? singleton ($ class )->singular_name () : $ class ;
63+ /**
64+ * Point to edit link, if shortcodable class exists.
65+ */
66+ public function Link ($ action = null )
67+ {
68+ if ($ this ->shortcodableclass ) {
69+ return Controller::join_links (
70+ self ::URLSegment,
71+ 'edit ' ,
72+ $ this ->shortcodableclass
73+ );
3574 }
75+ return Controller::join_links (self ::URLSegment, $ action );
76+ }
3677
37- // load from the currently selected ShortcodeType or Shortcode data
38- $ classname = false ;
39- $ shortcodeData = false ;
40- if ($ shortcode = $ this ->request ->requestVar ('Shortcode ' )) {
41- $ shortcode = str_replace ("\xEF\xBB\xBF" , '' , $ shortcode ); //remove BOM inside string on cursor position...
42- $ shortcodeData = singleton ('ShortcodableParser ' )->the_shortcodes (array (), $ shortcode );
43- if (isset ($ shortcodeData [0 ])) {
44- $ shortcodeData = $ shortcodeData [0 ];
45- $ classname = $ shortcodeData ['name ' ];
78+ /**
79+ * handleEdit
80+ */
81+ public function handleEdit (SS_HTTPRequest $ request )
82+ {
83+ $ this ->shortcodableclass = $ request ->param ('ShortcodeType ' );
84+ return $ this ->handleAction ($ request , $ action = $ request ->param ('Action ' ));
85+ }
86+
87+ /**
88+ * Get the shortcode data from the request.
89+ * @return array shortcodedata
90+ */
91+ protected function getShortcodeData ()
92+ {
93+ if ($ this ->shortcodedata ){
94+ return $ this ->shortcodedata ;
95+ }
96+ $ data = false ;
97+ if ($ shortcode = $ this ->request ->requestVar ('Shortcode ' )){
98+ //remove BOM inside string on cursor position...
99+ $ shortcode = str_replace ("\xEF\xBB\xBF" , '' , $ shortcode );
100+ $ data = singleton ('ShortcodableParser ' )->the_shortcodes (array (), $ shortcode );
101+ if (isset ($ data [0 ])){
102+ $ this ->shortcodedata = $ data [0 ];
103+ return $ this ->shortcodedata ;
46104 }
47- } else {
48- $ classname = $ this ->request ->requestVar ('ShortcodeType ' );
49105 }
106+ }
50107
51- if ($ shortcodeData ) {
108+ /**
109+ * Provides a GUI for the insert/edit shortcode popup.
110+ *
111+ * @return Form
112+ **/
113+ public function ShortcodeForm ()
114+ {
115+ Config::inst ()->update ('SSViewer ' , 'theme_enabled ' , false );
116+ $ classes = Shortcodable::get_shortcodable_classes_fordropdown ();
117+ $ classname = $ this ->shortcodableclass ;
118+
119+ if ($ this ->isnew ) {
52120 $ headingText = _t ('Shortcodable.EDITSHORTCODE ' , 'Edit Shortcode ' );
53121 } else {
54- $ headingText = _t ('Shortcodable.INSERTSHORTCODE ' , 'Insert Shortcode ' );
122+ $ headingText = sprintf (
123+ _t ('Shortcodable.EDITSHORTCODE ' , 'Edit %s Shortcode ' ),
124+ singleton ($ this ->shortcodableclass )->singular_name ()
125+ );
55126 }
56127
57128 // essential fields
@@ -65,30 +136,31 @@ public function ShortcodeForm()
65136 LiteralField::create ('shortcodablefields ' , '<div class="ss-shortcodable content"> ' ),
66137 DropdownField::create ('ShortcodeType ' , 'ShortcodeType ' , $ classes , $ classname )
67138 ->setHasEmptyDefault (true )
68- ->addExtraClass ('shortcode-type ' ),
69-
139+ ->addExtraClass ('shortcode-type ' )
70140 ));
71141
72142 // attribute and object id fields
73- if ($ classname ) {
74- if (class_exists ($ classname )) {
75- $ class = singleton ($ classname );
76- if (is_subclass_of ($ class , 'DataObject ' )) {
77- if (singleton ($ classname )->hasMethod ('getShortcodableRecords ' )) {
78- $ dataObjectSource = singleton ($ classname )->getShortcodableRecords ();
79- } else {
80- $ dataObjectSource = $ classname ::get ()->map ()->toArray ();
81- }
143+ if ($ classname && class_exists ($ classname )) {
144+ $ class = singleton ($ classname );
145+ if (is_subclass_of ($ class , 'DataObject ' )) {
146+ if (singleton ($ classname )->hasMethod ('getShortcodableRecords ' )) {
147+ $ dataObjectSource = singleton ($ classname )->getShortcodableRecords ();
148+ } else {
149+ $ dataObjectSource = $ classname ::get ()->map ()->toArray ();
150+ }
151+ $ fields ->push (
152+ DropdownField::create ('id ' , $ class ->singular_name (), $ dataObjectSource )
153+ ->setHasEmptyDefault (true )
154+ );
155+ }
156+ if (singleton ($ classname )->hasMethod ('getShortcodeFields ' )) {
157+ if ($ attrFields = singleton ($ classname )->getShortcodeFields ()) {
82158 $ fields ->push (
83- DropdownField::create ('id ' , $ class ->singular_name (), $ dataObjectSource )
84- ->setHasEmptyDefault (true )
159+ CompositeField::create ($ attrFields )
160+ ->addExtraClass ('attributes-composite ' )
161+ ->setName ('AttributesCompositeField ' )
85162 );
86163 }
87- if (singleton ($ classname )->hasMethod ('getShortcodeFields ' )) {
88- if ($ attrFields = singleton ($ classname )->getShortcodeFields ()) {
89- $ fields ->push (CompositeField::create ($ attrFields )->addExtraClass ('attributes-composite ' ));
90- }
91- }
92164 }
93165 }
94166
@@ -105,12 +177,21 @@ public function ShortcodeForm()
105177 ->loadDataFrom ($ this )
106178 ->addExtraClass ('htmleditorfield-form htmleditorfield-shortcodable cms-dialog-content ' );
107179
108- if ($ shortcodeData ) {
109- $ form ->loadDataFrom ($ shortcodeData ['atts ' ]);
110- }
111-
112180 $ this ->extend ('updateShortcodeForm ' , $ form );
113181
182+ $ fields ->push (LiteralField::create ('shortcodablefieldsend ' , '</div> ' ));
183+
184+ if ($ data = $ this ->getShortcodeData ()) {
185+ $ form ->loadDataFrom ($ data ['atts ' ]);
186+
187+ // special treatment for setting value of UploadFields
188+ foreach ($ form ->Fields ()->dataFields () as $ field ) {
189+ if (is_a ($ field , 'UploadField ' ) && isset ($ data ['atts ' ][$ field ->getName ()])) {
190+ $ field ->setValue (array ('Files ' => explode (', ' , $ data ['atts ' ][$ field ->getName ()])));
191+ }
192+ }
193+ }
194+
114195 return $ form ;
115196 }
116197
0 commit comments