PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/bedita-app/views/helpers/be_embed_media.php

http://bedita.googlecode.com/
PHP | 357 lines | 180 code | 27 blank | 150 comment | 54 complexity | 01975ee04bca1fe991f5d7d6e1e68df7 MD5 | raw file
Possible License(s): AGPL-1.0, AGPL-3.0, LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /*-----8<--------------------------------------------------------------------
  3. *
  4. * BEdita - a semantic content management framework
  5. *
  6. * Copyright 2008 ChannelWeb Srl, Chialab Srl
  7. *
  8. * This file is part of BEdita: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published
  10. * by the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. * BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU Lesser General Public License for more details.
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * version 3 along with BEdita (see LICENSE.LGPL).
  17. * If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
  18. *
  19. *------------------------------------------------------------------->8-----
  20. */
  21. /**
  22. * Helper class to embed media contents
  23. *
  24. * @version $Revision: 3905 $
  25. * @modifiedby $LastChangedBy: ste $
  26. * @lastmodified $LastChangedDate: 2012-08-08 18:21:17 +0200 (Wed, 08 Aug 2012) $
  27. *
  28. * $Id: be_embed_media.php 3905 2012-08-08 16:21:17Z ste $
  29. */
  30. class BeEmbedMediaHelper extends AppHelper {
  31. private $_helpername = "BeEmbedMedia Helper";
  32. // private
  33. private $_objects = array ("image", "audio", "video", "flash"); // supported
  34. // private $_output = false;
  35. private $_conf = array ();
  36. private $defaultPresentation = array(
  37. "Image" => "thumb",
  38. "Video" => "full",
  39. "Audio" => "full",
  40. "Application" => "full",
  41. "BEFile" => "link"
  42. );
  43. /**
  44. * Included helpers.
  45. *
  46. * @var array
  47. */
  48. var $helpers = array('Html', 'BeThumb', 'MediaProvider', 'BeEmbedFlash');
  49. function __construct()
  50. {
  51. // get configuration parameters
  52. $this->_conf = Configure::read('media') ;
  53. $this->_conf['root'] = Configure::read('mediaRoot');
  54. $this->_conf['url'] = Configure::read('mediaUrl');
  55. $this->_conf['tmp'] = Configure::read('tmp');
  56. $this->_conf['imgMissingFile'] = Configure::read('imgMissingFile');
  57. }
  58. public function getValidImplementations() {
  59. return $this->BeThumb->getValidImplementations();
  60. }
  61. /**
  62. * object public method: embed a generic bedita multimedia object
  63. * return html for object $obj with options $params and html attributes $htmlAttributes
  64. *
  65. * @param array obj, BEdita Multimedia Object
  66. * @param array params, optional, parameters used by external helpers such as BeThumb->image
  67. * possible value for params:
  68. * "presentation" => "thumb", "full", "link" (default defined by $defaultPresentation attribute)
  69. * "URLonly" => if setted return only url
  70. *
  71. * ## USED only for image thumbnail on filesystem ##
  72. * width, height, longside, at least one required, integer (if longside, w&h are ignored)
  73. * mode, optional, 'crop'/'fill'/'croponly'/'stretch'
  74. * modeparam, optional, depends on mode:
  75. * if fill, string representing hex color, ie 'FFFFFF'
  76. * if croponly, string describing crop zone 'C', 'T', 'B', 'L', 'R', 'TL', 'TR', 'BL', 'BR'
  77. * if stretch, bool to allow upscale (default false)
  78. * type, optional, 'gif'/'png'/'jpg', force image target type
  79. * upscale, optional, bool, allow or not upscale
  80. *
  81. * @param array htmlAttributes, html attributes
  82. *
  83. * @return string, output complete html tag
  84. *
  85. */
  86. public function object ( $obj, $params = null, $htmlAttributes=array() ) {
  87. // get object type
  88. $model = $this->getType ($obj);
  89. $params["presentation"] = (!empty($params["presentation"]))? $params["presentation"] : $this->defaultPresentation[$model];
  90. $method = "show" . ucfirst($params["presentation"]) . $model;
  91. if (method_exists($this, "show" . ucfirst($params["presentation"]) . $model)) {
  92. $output = $this->{"show" . ucfirst($params["presentation"]) . $model}($obj, $params, $htmlAttributes);
  93. } elseif (method_exists($this, "show" . $model)) {
  94. $output = $this->{"show" . $model}($obj, $params, $htmlAttributes);
  95. } else {
  96. $output = "unknown type: " . $model;
  97. }
  98. // output HTML
  99. return $output;
  100. }
  101. /******************************
  102. * private functions
  103. *****************************/
  104. /**
  105. * return object model for $obj, getting it from configuration, by object_type_id
  106. *
  107. * @param array $obj, object
  108. * @return string
  109. */
  110. private function getType ($obj) {
  111. $model = Configure::read("objectTypes." . $obj['object_type_id'] . ".model");
  112. return (!empty($model))? $model : "";
  113. }
  114. /**
  115. * produce html tag for image
  116. *
  117. * if present $params["URLonly"], return uri of $obj
  118. * if present $params["presentation"]=="link", return html link for $obj uri
  119. * else return html image for $obj uri (@see HtmlHelper)
  120. *
  121. * @param array $obj, object
  122. * @param array $params, specific parameters
  123. * @param array $htmlAttributes, html attributes
  124. * @return string
  125. */
  126. private function showImage ($obj, $params, $htmlAttributes) {
  127. $src = $this->getImageSrc($obj, $params);
  128. if (!$src) {
  129. $src = $this->getMediaTypeImage($obj);
  130. }
  131. if (!empty($params["URLonly"])) {
  132. return $src;
  133. } elseif ($params["presentation"] == "link") {
  134. return $this->Html->link($obj['title'],$src, $htmlAttributes);
  135. } else {
  136. if (empty($htmlAttributes["alt"])) {
  137. $htmlAttributes["alt"] = $obj["title"];
  138. }
  139. return $this->Html->image($src, $htmlAttributes);
  140. }
  141. }
  142. /**
  143. * return image $obj uri
  144. * if $params["presentation"] == "thumb", return image thumb (@see BeThumbHelper)
  145. *
  146. * @param array $obj, object
  147. * @param array $params, specific parameters
  148. * @return string
  149. */
  150. private function getImageSrc($obj, $params) {
  151. // not local file
  152. if(preg_match(Configure::read("validate_resource.URL"), $obj["uri"])) {
  153. $src = $obj['uri'];
  154. //local file
  155. } else {
  156. $src = ($params["presentation"] == "thumb")? $this->BeThumb->image ($obj, $params) : $this->_conf['url'] . $obj['uri'];
  157. }
  158. return $src;
  159. }
  160. /**
  161. * html video output
  162. * return html or uri for video $obj, with options $params and html attributes $htmlAttributes
  163. * if $params["presentation"] == "thumb" => return html thumb through MediaProvider ($params["URLonly"] not present) or thumb uri ($params["URLonly"] is present)
  164. * if $params["presentation"] == "full" => return embed object through MediaProvider
  165. * if $params["presentation"] == "link" => return object link through MediaProvider
  166. *
  167. * @see MediaProviderHelper
  168. * @param array $obj, object
  169. * @param array $params, specific parameters
  170. * @param array $htmlAttributes, html attributes
  171. * @return string
  172. */
  173. private function showVideo($obj, $params, $htmlAttributes) {
  174. if (!preg_match(Configure::read("validate_resource.URL"), $obj["uri"])) {
  175. $obj['uri'] = $this->_conf["url"] . $obj["uri"];
  176. }
  177. $URLonly = (!empty($params["URLonly"]))? true : false;
  178. if ($params["presentation"] == "thumb") {
  179. if (empty($htmlAttributes["alt"])) {
  180. $htmlAttributes["alt"] = $obj["title"];
  181. }
  182. if (!empty($obj["thumbnail"]) && preg_match(Configure::read("validate_resource.URL"), $obj["thumbnail"])) {
  183. $output = ($URLonly)? $obj["thumbnail"] : $this->Html->image($obj["thumbnail"], $htmlAttributes);
  184. } else {
  185. $output = $this->MediaProvider->thumbnail($obj, $htmlAttributes, $URLonly);
  186. if (empty($output)) {
  187. $img = $this->getMediaTypeImage($obj);
  188. $output = $this->Html->image($img, $htmlAttributes);
  189. }
  190. }
  191. } elseif ($params["presentation"] == "full") {
  192. $output = $this->MediaProvider->embed($obj, $params, $htmlAttributes);
  193. } elseif ($params["presentation"] == "link") {
  194. $src = $this->MediaProvider->sourceEmbed($obj);
  195. $output = (!empty($URLonly))? $src : $this->Html->link($obj['title'],$src, $htmlAttributes);
  196. }
  197. if (empty($output)) {
  198. $output = $obj['uri'];
  199. }
  200. return $output;
  201. }
  202. /**
  203. * html audio output
  204. * return html or uri for audio $obj, with options $params and html attributes $htmlAttributes
  205. * if present $params["URLonly"], return $obj uri
  206. * if $params["presentation"] == "link" => return html link for $obj
  207. * if $params["presentation"] == "full" => return embed object throgh MediaProvider (@see MediaProviderHelper)
  208. * else return html image for $obj (@see HtmlHelper)
  209. *
  210. * @param array $obj, object
  211. * @param array $params, specific parameters
  212. * @param array $htmlAttributes, html attributes
  213. * @return string
  214. */
  215. private function showAudio($obj, $params, $htmlAttributes) {
  216. if (!preg_match(Configure::read("validate_resource.URL"), $obj["uri"])) {
  217. $obj['uri'] = $this->_conf["url"] . $obj["uri"];
  218. }
  219. if (!empty($params["URLonly"]))
  220. return $obj['uri'];
  221. if ($params["presentation"] == "link") {
  222. return $this->Html->link($obj['title'],$obj['uri'], $htmlAttributes);
  223. } elseif ($params["presentation"] == "full") {
  224. $output = $this->MediaProvider->embed($obj, $params, $htmlAttributes);
  225. if (empty($output)) {
  226. $output = $obj['uri'];
  227. }
  228. return $output;
  229. } else {
  230. $img = $this->getMediaTypeImage($obj);
  231. return $this->Html->image($img, $htmlAttributes);
  232. }
  233. }
  234. /**
  235. * html befile output
  236. * return html or uri for file $obj, with options $params and html attributes $htmlAttributes
  237. * if present $params["URLonly"], return $obj uri
  238. * if $params["presentation"] == "thumb" => return html image for $obj (@see HtmlHelper)
  239. * else return html link for $obj (@see HtmlHelper)
  240. *
  241. * @param array $obj, object
  242. * @param array $params, specific parameters
  243. * @param array $htmlAttributes, html attributes
  244. * @return string, html
  245. */
  246. private function showBEFile($obj, $params, $htmlAttributes) {
  247. if (!preg_match(Configure::read("validate_resource.URL"), $obj["uri"])) {
  248. $obj['uri'] = $this->_conf["url"] . $obj["uri"];
  249. }
  250. if (!empty($params["URLonly"]))
  251. return $obj['uri'];
  252. if ($params["presentation"] == "thumb") {
  253. $img = $this->getMediaTypeImage($obj);
  254. return $this->Html->image($img, $htmlAttributes);
  255. } else {
  256. return $this->Html->link($obj['title'],$obj['uri'], $htmlAttributes);
  257. }
  258. }
  259. /**
  260. * application show for $obj
  261. * if $params["presentation"] == "full" && $obj["application_name"] == "flash" => return embed flash (@see BeEmbedFlashHelper)
  262. * if $params["presentation"] == "thumb" => return html image for $obj (@see HtmlHelper)
  263. *
  264. * @param array $obj, object
  265. * @param array $params, specific parameters
  266. * @param array $htmlAttributes, html attributes
  267. * @return string
  268. */
  269. private function showApplication($obj, $params, $htmlAttributes) {
  270. if ($params["presentation"] == "full") {
  271. if ($obj["application_name"] == "flash") {
  272. if (!preg_match(Configure::read("validate_resource.URL"), $obj["uri"])) {
  273. $obj['uri'] = $this->_conf["url"] . $obj["uri"];
  274. }
  275. if (empty($htmlAttributes["width"]) && !empty($obj["width"])) {
  276. $htmlAttributes["width"] = $obj["width"];
  277. } elseif (empty($htmlAttributes["width"])) {
  278. $htmlAttributes["width"] = 320;
  279. }
  280. if (empty($htmlAttributes["height"]) && !empty($obj["height"])) {
  281. $htmlAttributes["height"] = $obj["height"];
  282. } elseif (empty($htmlAttributes["height"])) {
  283. $htmlAttributes["height"] = 200;
  284. }
  285. if (empty($htmlAttributes["application_version"]) && !empty($obj["application_version"])) {
  286. $htmlAttributes["application_version"] = $obj["application_version"];
  287. }
  288. if (empty($htmlAttributes["dir"]) && !empty($obj["text_dir"])) {
  289. $htmlAttributes["dir"] = $obj["text_dir"];
  290. }
  291. if (empty($htmlAttributes["lang"]) && !empty($obj["text_lang"])) {
  292. $htmlAttributes["lang"] = $obj["text_lang"];
  293. }
  294. $output = $this->BeEmbedFlash->embed($obj, $params, $htmlAttributes);
  295. }
  296. } elseif ($params["presentation"] == "thumb") {
  297. $imgThumb = $this->getMediaTypeImage($obj);
  298. $output = $this->Html->image($imgThumb, $htmlAttributes);
  299. }
  300. return $output;
  301. }
  302. /**
  303. * media type image
  304. * return path for object type image
  305. * if BACKEND_APP => try to find image in /webroot/img/iconset, then /webroot/img/
  306. *
  307. * @param array $obj, object
  308. * @return string, object type image
  309. */
  310. protected function getMediaTypeImage($obj) {
  311. $img = "iconset/88px/";
  312. if ( BACKEND_APP == false ) {
  313. if (is_dir(APP."webroot".DS."img".DS."iconset") ) {
  314. $img = "iconset".DS;
  315. } else {
  316. $img = Configure::read("beditaUrl")."/bedita-app/webroot/img/".$img;
  317. }
  318. }
  319. if (!empty($obj["mediatype"])) {
  320. $img.= $obj["mediatype"] . ".png";
  321. } elseif (!empty($obj["Category"])) {
  322. $imgname = (!is_array($obj["Category"]))? $obj["Category"] : $obj["Category"][0]["name"];
  323. $img.= $imgname . ".png";
  324. }else {
  325. $img.= "notype.png";
  326. }
  327. return $img;
  328. }
  329. }
  330. ?>