PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/cms/modules/media/MediaSelector.hx

http://poko.googlecode.com/
Haxe | 204 lines | 167 code | 29 blank | 8 comment | 50 complexity | eff64891a8c38b7e486539d23ea00dc0 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. /**
  2. * ...
  3. * @author Tony Polinelli
  4. */
  5. package site.cms.modules.media;
  6. import haxe.Md5;
  7. import php.FileSystem;
  8. import php.io.File;
  9. import php.Lib;
  10. import php.Session;
  11. import php.Web;
  12. import poko.form.elements.Button;
  13. import site.cms.modules.base.formElements.FileUpload;
  14. import poko.form.elements.Selectbox;
  15. import poko.form.Form;
  16. import poko.js.JsBinding;
  17. import poko.utils.PhpTools;
  18. import site.cms.templates.CmsPopup;
  19. import site.cms.templates.CmsTemplate;
  20. class MediaSelector extends CmsPopup
  21. {
  22. public var elementId:String;
  23. public var form:Form;
  24. public var uploadForm:Form;
  25. public var selector:Selectbox;
  26. public var items:List<String>;
  27. private var gallery:String;
  28. public var jsBind:JsBinding;
  29. public var jsBindTop:JsBinding;
  30. public static var FROM_CMS:String = "cms";
  31. public static var FROM_TINYMCE:String = "tinyMce";
  32. public static var FROM_WYM:String = "wym";
  33. public var from:String;
  34. public var baseUrl:String;
  35. public var currentView:String;
  36. public static var VIEW_LIST:String = "list";
  37. public static var VIEW_THUMBS:String = "thumbs";
  38. public var showOnlyLibraries:Array<String>;
  39. public var allowViewThumb:Bool;
  40. public var allowViewList:Bool;
  41. override public function init()
  42. {
  43. super.init();
  44. if (app.params.get("showOnlyLibraries") != null && app.params.get("showOnlyLibraries") != "") {
  45. showOnlyLibraries = app.params.get("showOnlyLibraries").split(":");
  46. }else {
  47. showOnlyLibraries = [];
  48. }
  49. from = FROM_CMS;
  50. if (app.params.get("from") != null) from = app.params.get("from");
  51. gallery = app.params.get("form1_galleryList");
  52. if (showOnlyLibraries.length == 1) gallery = showOnlyLibraries[0];
  53. if (gallery == null) gallery = Session.get('mediaGalleryLastGallery');
  54. if (gallery != null) Session.set('mediaGalleryLastGallery', gallery);
  55. allowViewThumb = (app.params.get("libraryViewThumb") == "1");
  56. allowViewList = (app.params.get("libraryViewList") == "1");
  57. if (from == FROM_WYM) {
  58. allowViewList = false;
  59. allowViewThumb = true;
  60. currentView = VIEW_THUMBS;
  61. }
  62. }
  63. override public function main()
  64. {
  65. elementId = app.params.get("elementId");
  66. if (app.params.get("viewType") != null)
  67. {
  68. currentView = app.params.get("viewType");
  69. }else {
  70. if (Session.get("mediaGalleryCurrentView") != null) {
  71. currentView = Session.get("mediaGalleryCurrentView");
  72. }else {
  73. currentView = VIEW_THUMBS;
  74. }
  75. }
  76. Session.set("mediaGalleryCurrentView", currentView);
  77. if (currentView == VIEW_LIST && allowViewList == false) currentView = VIEW_THUMBS;
  78. if (currentView == VIEW_THUMBS && allowViewThumb == false) currentView = VIEW_LIST;
  79. baseUrl = "http://" + Web.getHostName() + Web.getURI();
  80. head.css.add("css/cms/media.css");
  81. head.css.add("css/cms/popup.css");
  82. head.js.add("js/cms/jquery.qtip.min.js");
  83. if (from == FROM_TINYMCE) {
  84. head.js.add("js/cms/tiny_mce/tiny_mce_popup.js");
  85. head.js.add("js/cms/tiny_mce_browser.js");
  86. }
  87. jsBind = new JsBinding("site.cms.modules.media.js.JsMediaSelector");
  88. jsBindTop = new JsBinding("site.cms.modules.base.js.JsDatasetItem");
  89. var exclude = [".", "..", ".svn"];
  90. var imageRoot = "./res/media/galleries";
  91. var galleries = new List();
  92. var dir = php.FileSystem.readDirectory(imageRoot);
  93. for (d in dir)
  94. {
  95. if (FileSystem.isDirectory(imageRoot + "/" +d) && !Lambda.has(exclude, d))
  96. {
  97. if(showOnlyLibraries.length == 0 || Lambda.has(showOnlyLibraries, d))
  98. galleries.add( { key:d, value:d } );
  99. }
  100. }
  101. var selector = new Selectbox("galleryList", "Select Gallery", galleries, gallery);
  102. form = new Form("form1");
  103. form.addElement(selector);
  104. form.setSubmitButton(new Button("submit", "submit"));
  105. form.populateElements();
  106. // image upload
  107. if (gallery != ""){
  108. uploadForm = new Form('uploadForm');
  109. var el = new FileUpload('file', 'File Upload');
  110. el.showLibrary = false;
  111. uploadForm.addElement(el);
  112. uploadForm.addElement(new Button('submit', 'Add file'));
  113. if (uploadForm.isSubmitted()) {
  114. // add new files
  115. var files:Hash <Hash<Dynamic>> = PhpTools.getFilesInfo();
  116. // data to insert into DB
  117. var data:Hash<String> = new Hash();
  118. for (file in files.keys())
  119. {
  120. var info:Hash<Dynamic> = files.get(file);
  121. var name = file.substr(form.name.length + 1);
  122. var filename = info.get("name");
  123. var randomString = Md5.encode(Date.now().toString()+Math.random());
  124. // upload file
  125. if (info.get("error") == 0)
  126. {
  127. PhpTools.moveFile(info.get("tmp_name"), './res/media/galleries/' + gallery + '/' + randomString + filename);
  128. data.set(name, randomString + filename);
  129. }
  130. }
  131. }
  132. }
  133. items = new List();
  134. if(gallery != null && gallery != "")
  135. {
  136. var dir = php.FileSystem.readDirectory(imageRoot + "/" + gallery);
  137. for (d in dir)
  138. {
  139. if (!Lambda.has(exclude, d))
  140. {
  141. items.add(d);
  142. }
  143. }
  144. }
  145. }
  146. public function getItem(name:String)
  147. {
  148. var onClick = switch(from)
  149. {
  150. case FROM_CMS:
  151. "window.top."+jsBindTop.getCall('updateItemFromMedia', [elementId, gallery, name]);
  152. case FROM_TINYMCE:
  153. "updateTinyMceWithValue('/res/media/galleries/" + gallery +"/" + name + "');";
  154. case FROM_WYM:
  155. "opener.document.getElementById('filebrowser').value = '"+baseUrl+"res/media/galleries/" + gallery +"/" + name + "'; window.close();";
  156. }
  157. var ext = name.substr(name.lastIndexOf(".") + 1).toLowerCase();
  158. var str = "";
  159. if(currentView == VIEW_THUMBS){
  160. switch(ext) {
  161. case "jpg", "gif", "png":
  162. str += '<img class="qTip" title="File: '+name+'" src="?request=cms.services.Image&preset=thumb&src=../media/galleries/'+gallery+'/'+name+'" onClick="'+onClick+'" />';
  163. case "txt":
  164. str += '<img class="qTip" title="File: '+name+'" src="./res/cms/media/file_txt.png" onClick="'+onClick+'" />';
  165. case "pdf":
  166. str += '<img class="qTip" title="File: '+name+'" src="./res/cms/media/file_pdf.png" onClick="'+onClick+'" />';
  167. default:
  168. str += '<img class="qTip" title="File: '+name+'" src="./res/cms/media/file_misc.png" onClick="'+onClick+'" />';
  169. }
  170. }else {
  171. str += '<div onClick="'+onClick+'">'+name+'</div>';
  172. }
  173. return str;
  174. }
  175. }