PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/server/components/controls/media/single/ArrowMediaConnectorSingle.php

http://arrowplatform.googlecode.com/
PHP | 383 lines | 340 code | 39 blank | 4 comment | 17 complexity | fa45979d2e7bbc89771d57f3a21d1c46 MD5 | raw file
  1. <?php
  2. ArrowController::importModel("org.arrowplatform.model.ArrowDataSource");
  3. ArrowController::importModel("media.MediaFolder");
  4. ArrowController::importModel("media.MediaElement");
  5. ArrowController::importModel("media.MediaTransformationProfile");
  6. ArrowController::importModel("media.MediaElementConnection");
  7. ArrowController::importModel("media.MediaApi");
  8. class ArrowMediaConnectorSingle extends ArrowTagContainer{
  9. protected $dataSource;
  10. private $field = null;
  11. private $currFile = null;
  12. private $currDirId = null;
  13. private $folder = null;
  14. private $parentFolder = null;
  15. public function configure(){
  16. $this->addProperties(array(
  17. "state" => "list",
  18. "model" => null,
  19. "key" => null,
  20. "presentation" => "list",
  21. "files" => "jpg;png",
  22. "transformation" => '',
  23. "single" => false,
  24. "title" => "Tytu?",
  25. "name" => "noname",
  26. "parentSystemName" => false,
  27. "targetSystemName" => false,
  28. "targetName" => false,
  29. "subFolderName" => false
  30. ));
  31. $this->addRequiredProperties(array( "model", "key"));
  32. $this->addStateProperties(array("state", "selected"));
  33. $this->setContentCodeKept(true);
  34. }
  35. public function init(){
  36. //$this->registerJs( get_class($this), dirname(__FILE__)."/ArrowMediaConnector.js", ".ctrl-media-connector");
  37. $key = $this->getProperty("key");
  38. $model = $this->getProperty("model");
  39. ArrowController::importModel($model);
  40. if( $key ){
  41. $targetSysName = $this->getProperty("targetSystemName");
  42. $look = $targetSysName?$targetSysName:$model;
  43. $criteria = new Criteria();
  44. $criteria->addCondition( MediaFolder::F_SYSTEM_NAME,$look );
  45. $folder = MediaFolder::getByCriteria( $criteria, MediaFolder::TCLASS );
  46. if(!empty($folder)){
  47. $this->folder = $folder[0];
  48. }else{
  49. //Target folder creation
  50. $container = MediaApi::getSystemFolder();
  51. $parentSystemName = $this->getProperty("parentSystemName");
  52. if( $parentSystemName ){
  53. $criteria = new Criteria();
  54. $criteria->addCondition( MediaFolder::F_SYSTEM_NAME, $parentSystemName );
  55. $logicalParent = MediaFolder::getByCriteria( $criteria, MediaFolder::TCLASS );
  56. if(empty($logicalParent)){
  57. $container = MediaApi::createFolder( $container->getPKey(), $parentSystemName, $parentSystemName );
  58. }else{
  59. $container = $logicalParent[0];
  60. }
  61. }
  62. $name = $this->getProperty("targetName")?$this->getProperty("targetName"):"Container_{$look}";
  63. $criteria = new Criteria();
  64. $criteria->addCondition( MediaFolder::F_SYSTEM_NAME,$look );
  65. $folder = MediaFolder::getByCriteria( $criteria, MediaFolder::TCLASS );
  66. if(empty($folder)){
  67. $this->folder = MediaApi::createFolder( $container->getPKey(), $name, $look );
  68. }else{
  69. $this->folder = $folder[0];
  70. }
  71. }
  72. $this->parentFolder = $this->folder;
  73. $subFolderName = $this->getProperty("subFolderName");
  74. if(!empty($subFolderName)){
  75. $criteria = new Criteria();
  76. $criteria->addCondition( MediaFolder::F_NAME, StringHelper::toValidFilesystemName( $subFolderName ) );
  77. $criteria->addCondition( MediaFolder::F_PARENT_ID, $this->folder->getPKey() );
  78. $subFolder = MediaFolder::getByCriteria( $criteria, MediaFolder::TCLASS );
  79. if(empty($subFolder)){
  80. $this->folder = MediaApi::createFolder( $this->folder->getPKey(), $subFolderName );
  81. }else{
  82. $this->folder = $subFolder[0];
  83. }
  84. }
  85. //-----------------------
  86. $critConnection = new Criteria("media.MediaElementConnection");
  87. $critConnection->addCondition( MediaElementConnection::F_MODEL, $model );
  88. $critConnection->addCondition( MediaElementConnection::F_OBJECT_ID, $key );
  89. $critConnection->addCondition( MediaElementConnection::F_NAME, $this->getProperty("name") );
  90. $critElement = new Criteria("media.MediaElement");
  91. $critElement->setLimit(0, 1);
  92. $criteria = new ArrowOrmJoinCriteria(array($critElement, $critConnection));
  93. $result = MediaElement::getByCriteria( $criteria, MediaElement::TCLASS );
  94. if(!empty($result))
  95. $this->currFile = $result[0];
  96. }
  97. }
  98. public function generateBlockStart(){
  99. $str ='<div class="'.self::CTRL_NAMESPACE.'-media-connector" >';
  100. $key = $this->getProperty("key");
  101. if(empty($key)){
  102. $str.="Wymagany zapis obiektu aby go edytowa?</div>";
  103. return $str;
  104. }
  105. $content=$this->generateAddForm();
  106. $str.= '<div class="'.self::CTRL_NAMESPACE.'-media-connector-content" >';
  107. $str.=$content;
  108. $str.= '</div>';
  109. $str.="</div>";
  110. return $str;
  111. }
  112. public function generateBlockEnd(){
  113. return "";
  114. }
  115. public function generateAddForm(){
  116. $model = $this->getProperty("model");
  117. $key = $this->getProperty("key");
  118. $name = $this->getProperty("name");
  119. $folderId = $this->folder->getPKey();
  120. $transformation = "0";
  121. if( $this->getProperty("transformation") ){
  122. $criteria = new Criteria();
  123. $criteria->addCondition(MediaTransformationProfile::F_SYSTEM_NAME, $this->getProperty("transformation"));
  124. $profile = MediaTransformationProfile::getByCriteria( $criteria, MediaTransformationProfile::TCLASS );
  125. if( !empty($profile) ){
  126. $profile = $profile[0];
  127. $transformation = $profile->getPKey();
  128. }else{
  129. throw new ArrowException(array("msg" => "Transformation not found", "transformation" => $this->getProperty("transformation")));
  130. }
  131. }
  132. $pFiles = $this->getProperty("files");
  133. if(empty($pFiles)){
  134. $files = "*.*;";
  135. $filesDesc = "Pliki";
  136. }else{
  137. $tmp = explode( ";", $this->getProperty("files"));
  138. $files = "";
  139. foreach($tmp as $ex)
  140. $files.="*.".$ex.";";
  141. $filesDesc = "Pliki: ". $files;
  142. }
  143. $res = ArrowResources::getDefault(ArrowController::$activeProject);
  144. $uploadify = $res->getResource("/js/jquery/uploadify/jquery.uploadify.v2.1.0.min.js")->getRelativePath();
  145. $uploadifyCss = $res->getResource("/js/jquery/uploadify/uploadify.css")->getRelativePath();
  146. $swfObject = $res->getResource("/js/swfobject/swfobject.js")->getRelativePath();
  147. $swf = $res->getResource("/js/jquery/uploadify/uploadify.swf")->getRelativePath();
  148. $deleteIcon = $res->getResource("/graphic/administration/icons/delete.gif")->getRelativePath();
  149. $bean = ArrowTemplateLinker::getDefault()->generateBeanLink( array( "path" => "/controller::save" ), true);
  150. $stateChangeLink = $this->getStateChangeLink(array("state" => "list"),true);
  151. $ajaxCont = $this->getParent("ArrowCtrlAjax")->getId();
  152. $fileName = "";
  153. $preview = "";
  154. if($this->currFile){
  155. $fileName = $this->currFile["name"];
  156. $conn = $this->currFile->getRel("MediaElementConnection");
  157. if(!empty($conn))
  158. $conn = $conn[0]->getPKey();
  159. else
  160. $conn = 0;
  161. $deleteOldLink = ArrowTemplateLinker::getDefault()->generateBeanLink(array(
  162. "path" => "/controller::actionRouter",
  163. "action" => "delete",
  164. "model" => "media.MediaElement",
  165. "key" => $this->currFile->getPKey(),
  166. "_htmlAmp" => 0
  167. ));
  168. if($this->currFile->isImage())
  169. $preview = '<img src="'.$this->currFile->getSystemMiniature().'" />';
  170. //@todo - zoptymalizowa?
  171. $obj = Persistent::getByKey($this->getProperty("key"), ArrowModel::getModelClass( $this->getProperty("model")));
  172. MediaApi::prepareMedia( array($obj) );
  173. $data = $obj["media"][$name];
  174. $links = "";
  175. if(!empty($data[0]["path"])){
  176. $fileName = "<a target=\"blank\" class=\"ctrl-media-preview\" href=\"{$data[0]["path"]}\">{$fileName}</a>";
  177. }
  178. if(isset($data[0]["verPath"])){
  179. foreach($data[0]["verPath"] as $verName => $path){
  180. $links.= "<a target=\"blank\" class=\"ctrl-media-preview\" href=\"{$path}\">{$data[0]["verNames"][$verName]}</a><br />";
  181. }
  182. }
  183. $deleteOld = "<a href=\"#\" onclick=\"ctrlMediaDelete(this, '{$this->currFile->getPKey()}', '$conn');\" \">[usu?]</a>";
  184. }else{
  185. $links = "";
  186. $deleteOldLink = "";
  187. $deleteOld = "";
  188. }
  189. $rand = rand( 1, 100000);
  190. $action = $this->addNode("ui", "action", array(
  191. 'type' => 'dialog', 'name' => 'Wybierz z istniej?cych',
  192. 'width' => 900, 'height' => 600,
  193. 'template' => "/mediafolder::popupBrowser" ,'id' => 'create'
  194. ));
  195. $action->addNode('ui', 'param', array('name' => 'model', 'value' => $this->getProperty("model") ));
  196. $action->addNode('ui', 'param', array('name' => 'key', 'value' => $this->getProperty("key") ));
  197. $action->addNode('ui', 'param', array('name' => 'ctrlId', 'value' => $this->getParent()->getId() ));
  198. $action->addNode('ui', 'param', array('name' => 'name', 'value' => $this->getProperty("name") ));
  199. $action->addNode('ui', 'param', array('name' => 'delete_old', 'value' => 1 ));
  200. $action->addNode('ui', 'param', array('name' => 'changeConnection', 'value' => 1 ));
  201. $action->addNode('ui', 'param', array('name' => 'root', 'value' => $this->parentFolder->getPKey() ));
  202. if( $this->getProperty("transformation") ){
  203. $criteria = new Criteria();
  204. $criteria->addCondition(MediaTransformationProfile::F_SYSTEM_NAME, $this->getProperty("transformation"));
  205. $profile = MediaTransformationProfile::getByCriteria( $criteria, MediaTransformationProfile::TCLASS );
  206. $action->addNode('ui', 'param', array('name' => 'transformationId', 'value' => $profile[0]->getPKey() ));
  207. }
  208. $action->init();
  209. $fromMediaFolder = $action->generate();
  210. $sessionId = session_id();
  211. $str = <<<HTML
  212. <script type="text/javascript" src="{$uploadify}"></script>
  213. <script type="text/javascript" src="{$swfObject}"></script>
  214. <link rel="stylesheet" type="text/css" href="{$uploadifyCss}" />
  215. <style type="text/css">
  216. .uploadifyQueueItem{
  217. width: auto !important;
  218. }
  219. </style>
  220. <script type="text/javascript">
  221. $(function() {
  222. $("#file-single{$rand}").uploadify({
  223. 'uploader' : '{$swf}',
  224. 'script' : '{$bean}',
  225. 'scriptData' : {
  226. object: "media.MediaElement",
  227. 'data[folder_id]': "{$folderId}",
  228. "data[transformation_id]": '{$transformation}',
  229. "connection[model]": '{$model}',
  230. "connection[key]": '{$key}',
  231. "connection[name]": '{$name}',
  232. "connection[delete_old]": '1',
  233. "connection[direct]": '1',
  234. "sessionId": '{$sessionId}'
  235. },
  236. 'cancelImg' : '{$deleteIcon}',
  237. 'folder' : 'uploads',
  238. 'queueID' : 'fileQueue-single{$rand}',
  239. 'buttonText' : 'Z dysku',
  240. 'width' : '78',
  241. 'height' : '21',
  242. 'auto' : true,
  243. 'multi' : false,
  244. 'fileDesc' : '{$filesDesc}',
  245. 'fileExt' : '{$files}',
  246. 'fileDataName' : 'file',
  247. onComplete: function (evt, queueID, fileObj, response)
  248. {
  249. eval('var res = '+response + ' ;');
  250. ArrowResponseReader.read({ response: res });
  251. },
  252. onAllComplete: function(evt, data){
  253. CtrlAjax.changeState( '{$ajaxCont}', '{$stateChangeLink}' );
  254. }
  255. });
  256. });
  257. function ctrlMediaDelete(object, elId, conId) {
  258. var link = $(object);
  259. if(!confirm("Czy napewno usun?? element?"))
  260. return false;
  261. ArrowDataConnector.getObjects(
  262. 'media.MediaElementConnection',
  263. {"criteria": "element_id:==:" + elId},
  264. {},
  265. function(data){
  266. var target = "connection";
  267. if(data.length == 1){
  268. if(confirm("Element nie jest u?ywany nigdzie indziej, usun?? ca?kowicie?"))
  269. target = "element";
  270. }
  271. var model;
  272. var id;
  273. if(target == "connection"){
  274. model = "media.MediaElementConnection";
  275. id = conId;
  276. }else{
  277. model = "media.MediaElement";
  278. id = elId;
  279. }
  280. ArrowDataConnector.deleteObject(model,id, {}, function(){
  281. CtrlAjax.changeStateVars(link,{},{});
  282. });
  283. }
  284. );
  285. return false;
  286. }
  287. $(function(){
  288. $(".ctrl-media-preview").click(function(){
  289. window.top.$.nyroModalManual({ url: $(this).attr("href"), zIndexStart: 999999999 });
  290. });
  291. });
  292. </script>
  293. <table style="width: auto;">
  294. <tr>
  295. <td style=" padding: 10px;"><b style="font-size: 16px;">{$this->getProperty("title")}</b> $deleteOld</td>
  296. <td><input type="file" id="file-single{$rand}" name="file" /></td>
  297. <td>{$fromMediaFolder}</td>
  298. </tr>
  299. <tr>
  300. <td style="padding: 10px;" colspan="3">
  301. <div style="font-size: 16px;">{$fileName}</div>
  302. </td>
  303. </tr>
  304. <tr>
  305. <td align="left">$preview</td>
  306. <td align="left">$links</td>
  307. </tr>
  308. <tr>
  309. <td colspan="3" style="font-size: 16px;"><div id="fileQueue-single{$rand}" ></div></td>
  310. </tr>
  311. </table>
  312. HTML;
  313. /*
  314. * <div style="float: left; margin-right: 10px; background-color: rgb(84, 83, 84); height: 20px; padding: 5px;">
  315. <a style="color: white; font-size: 13px; font-weight: bold;" href="{$connectionLink}">Wybierz z istniej?cych</a>
  316. </div>
  317. */
  318. return $str;
  319. }
  320. }
  321. ?>