PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/webcore/ext/gallery/webcore.gallery.php

#
PHP | 374 lines | 235 code | 51 blank | 88 comment | 13 complexity | b84b488d90b0e1da763585e373c19dcd MD5 | raw file
  1. <?php
  2. /**
  3. * Represents a image gallery
  4. *
  5. * @package WebCore
  6. * @subpackage Gallery
  7. */
  8. class Gallery extends DataRepeaterModelBase implements IRootModel
  9. {
  10. /**
  11. * Create a instance of this class
  12. *
  13. * @param string $name
  14. * @param string $caption
  15. */
  16. public function __construct($name, $caption)
  17. {
  18. parent::__construct($name, $caption);
  19. }
  20. /**
  21. * Binds all IBindingTargetMember controls within the repeater to the given data source.
  22. *
  23. * @param IndexedCollection $dataSource
  24. */
  25. public function dataBind(&$dataSource)
  26. {
  27. if (ObjectIntrospector::isA($dataSource, 'TableAdapter'))
  28. {
  29. if ($this->isPaged === true)
  30. {
  31. if ($this->state->getTotalRecordCount() === -1)
  32. {
  33. $countSource = clone $dataSource;
  34. $recordCount = $countSource->count();
  35. $this->state->setTotalRecordCount($recordCount);
  36. $pageCount = intval(ceil($recordCount / $this->pageSize));
  37. $this->state->setPageCount($pageCount);
  38. if ($this->state->getPageIndex() >= $this->state->getPageCount())
  39. {
  40. $this->state->setPageIndex($this->state->getPageCount() - 1);
  41. }
  42. }
  43. $dataSource->take($this->pageSize)->skip($this->state->getPageIndex() * $this->pageSize);
  44. }
  45. $rows = $dataSource->selectNew()->getArrayReference();
  46. $this->dataItems->addRange($rows);
  47. }
  48. else
  49. {
  50. foreach ($dataSource as $item)
  51. {
  52. $this->dataItems->addItem($item);
  53. }
  54. }
  55. }
  56. /**
  57. * Creates a default instance of this class
  58. *
  59. * @return Gallery
  60. */
  61. public static function createInstance()
  62. {
  63. return new Gallery('Serializable', 'Serializable');
  64. }
  65. }
  66. /**
  67. * Represents a Image entity
  68. *
  69. * @package WebCore
  70. * @subpackage Gallery
  71. */
  72. class GalleryImage extends ObjectBase
  73. {
  74. public $FileName;
  75. public $Thumbnail;
  76. public $Name;
  77. public $Alt = '';
  78. public $Properties;
  79. public $thumbWidth = 320;
  80. public $thumbHeight = 240;
  81. public $writeNameAsHtml = false;
  82. /**
  83. * Creates a new instance of this class
  84. *
  85. * @param string $fileName
  86. * @param string $name
  87. */
  88. public function __construct($fileName, $name)
  89. {
  90. $this->FileName = $fileName;
  91. $this->Thumbnail = $fileName;
  92. $this->Name = $name;
  93. $this->Alt = $name;
  94. $this->Properties = new KeyedCollection();
  95. }
  96. /**
  97. * Creates a thumbnail
  98. *
  99. */
  100. public function createThumbnail()
  101. {
  102. if (file_exists($this->FileName) == false)
  103. throw new SystemException(SystemException::EX_INVALIDPARAMETER, 'Parameter = fileName');
  104. $info = pathinfo($this->FileName);
  105. $baseName = str_replace("." . $info['extension'], '', $info['basename']);
  106. $thumbName = $info['dirname'] . '/' . $baseName . ".thumb.jpg";
  107. if (file_exists($thumbName) == false)
  108. {
  109. $thumbResource = ImageHelper::createThumbnailFromFile($this->FileName, $this->thumbWidth, $this->thumbHeight);
  110. imagejpeg($thumbResource, $thumbName, 90);
  111. }
  112. $this->Thumbnail = $thumbName;
  113. }
  114. }
  115. /**
  116. * Represents a Gallery HTML view
  117. *
  118. * @package WebCore
  119. * @subpackage Gallery
  120. */
  121. class HtmlGalleryView extends HtmlViewBase
  122. {
  123. //This var was introduced in order to make this class
  124. // a little more flexible when implementing
  125. protected $suffixCssClass = '';
  126. protected $enableContextMenu = true;
  127. /**
  128. * Returns whether or not the context menu is able for the view
  129. * @return Boolean
  130. */
  131. public function getEnableContextMenu()
  132. {
  133. return $this->enableContextMenu;
  134. }
  135. /**
  136. * Sets whether or not to enable context menu for the view
  137. * @param <type> $enableContextMenu
  138. */
  139. public function setEnableContextMenu($enableContextMenu)
  140. {
  141. $this->enableContextMenu = $enableContextMenu;
  142. }
  143. public function getSuffixCssClass()
  144. {
  145. return $this->suffixCssClass;
  146. }
  147. public function setSuffixCssClass($class)
  148. {
  149. $this->suffixCssClass = $class;
  150. }
  151. /**
  152. * Creates a new instance of this class based on a gallery model
  153. *
  154. * @param Gallery $model
  155. */
  156. public function __construct(&$model)
  157. {
  158. parent::__construct($model);
  159. $this->cssClass = 'galleryview';
  160. $this->frameWidth = '90%';
  161. $callbacks =& $this->renderCallbacks->getArrayReference();
  162. // Setup the callbacks for each renderable model
  163. $callbacks['Gallery'] = array(
  164. 'HtmlGalleryRenderCallbacks',
  165. 'renderGallery'
  166. );
  167. $this->registerDependencies();
  168. }
  169. /**
  170. * Registers model resources and dependencies on the client-side
  171. *
  172. */
  173. protected function registerDependencies()
  174. {
  175. self::registerCommonDependecies();
  176. $formviewPath = HttpContext::getLibraryRoot() . 'ext/gallery/std.galleryview.js';
  177. $cssPath = HttpContext::getLibraryRoot() . 'ext/gallery/std.galleryview.css';
  178. HtmlViewManager::registerDependency(HtmlDependency::TYPE_JS_FILE, __CLASS__, 'HtmlGalleryView.Js', $formviewPath);
  179. HtmlViewManager::registerDependency(HtmlDependency::TYPE_CSS_FILE, __CLASS__, 'HtmlGalleryView.Css', $cssPath);
  180. }
  181. }
  182. /**
  183. * Represents a Gallery Coverflow-like HTML view
  184. *
  185. * @package WebCore
  186. * @subpackage Gallery
  187. */
  188. class HtmlGalleryFlowView extends HtmlGalleryView
  189. {
  190. public function __construct(&$model)
  191. {
  192. parent::__construct($model);
  193. $callbacks =& $this->renderCallbacks->getArrayReference();
  194. $callbacks['Gallery'] = array(
  195. 'HtmlGalleryRenderCallbacks',
  196. 'renderGalleryFlow'
  197. );
  198. }
  199. protected function registerDependencies()
  200. {
  201. self::registerCommonDependecies();
  202. $formviewPath = HttpContext::getLibraryRoot() . 'ext/gallery/flow.galleryview.js';
  203. $cssPath = HttpContext::getLibraryRoot() . 'ext/gallery/flow.galleryview.css';
  204. HtmlViewManager::registerDependency(HtmlDependency::TYPE_JS_FILE, __CLASS__, 'HtmlGalleryView.Js', $formviewPath);
  205. HtmlViewManager::registerDependency(HtmlDependency::TYPE_CSS_FILE, __CLASS__, 'HtmlGalleryView.Css', $cssPath);
  206. }
  207. }
  208. /**
  209. * Contains static callback methods to render Gallery
  210. *
  211. * @package WebCore
  212. * @subpackage Gallery
  213. */
  214. class HtmlGalleryRenderCallbacks extends HtmlRepeaterRenderCallbacks
  215. {
  216. /**
  217. * Helper method to render the gallery flow
  218. *
  219. * @param Gallery $model
  220. * @param HtmlGalleryView $view
  221. */
  222. public static function renderGalleryFlow(&$model, &$view)
  223. {
  224. $tw = HtmlWriter::getInstance();
  225. $tw->openForm();
  226. $tw->addAttribute('id', HtmlViewBase::getHtmlId($model));
  227. $tw->addAttribute('name', $model->getName());
  228. $tw->addAttribute('class', $view->getCssClass());
  229. $tw->addAttribute('method', 'post');
  230. $tw->addAttribute('action', HttpContext::getInfo()->getScriptVirtualPath());
  231. foreach ($model->getDataItems() as $item)
  232. {
  233. $tw->openDiv();
  234. $tw->openImg();
  235. $tw->addAttribute('class', $view->getCssClass() . '-thumbnail-image');
  236. $tw->addAttribute('alt', $item->Name);
  237. $tw->addAttribute('title', $item->Name);
  238. $tw->addAttribute('src', $item->FileName);
  239. $tw->closeImg();
  240. $tw->closeDiv();
  241. }
  242. $tw->closeForm();
  243. $javascript = "var js_" . HtmlViewBase::getHtmlId($model) . " = null;
  244. window.addEvent('domready', function () { js_" . HtmlViewBase::getHtmlId($model) . " = new MooFlow($('" . HtmlViewBase::getHtmlId($model) . "')); });";
  245. self::renderInitializationScript($javascript);
  246. }
  247. /**
  248. * Helper method to render the gallery
  249. *
  250. * @param Gallery $model
  251. * @param HtmlGalleryView $view
  252. */
  253. public static function renderGallery(&$model, &$view)
  254. {
  255. $tw = HtmlWriter::getInstance();
  256. $tw->openForm();
  257. $tw->addAttribute('id', HtmlViewBase::getHtmlId($model));
  258. $tw->addAttribute('name', $model->getName());
  259. $tw->addAttribute('class', $view->getCssClass().$view->getSuffixCssClass());
  260. $tw->addAttribute('method', 'post');
  261. $tw->addAttribute('action', HttpContext::getInfo()->getScriptVirtualPath());
  262. $tw->openDiv();
  263. $tw->addAttribute('class', $view->getMasterCssClass() . '-caption');
  264. $tw->writeContent($model->getCaption());
  265. $tw->closeDiv();
  266. foreach ($model->getChildren()->getTypedControlNames(true, 'Toolbar') as $control)
  267. {
  268. $currentControl = $model->getChildren()->getControl($control, true);
  269. $controlClassName = $currentControl->getType()->getName();
  270. $renderCallback = $view->getRenderCallbacks()->getValue($controlClassName);
  271. if (is_callable($renderCallback, false))
  272. call_user_func_array($renderCallback, array(
  273. &$currentControl,
  274. &$view
  275. ));
  276. }
  277. $tw->openDiv();
  278. $tw->addAttribute('class', $view->getCssClass() . '-container');
  279. foreach ($model->getDataItems() as $item)
  280. {
  281. $tw->openDiv();
  282. $tw->addAttribute('class', $view->getCssClass() . '-thumbnail');
  283. $tw->openDiv();
  284. $tw->addAttribute('class', $view->getCssClass() . '-thumbnail-caption');
  285. if($item->writeNameAsHtml)
  286. {
  287. $tw->writeRaw(utf8_encode($item->Name));
  288. }
  289. else
  290. {
  291. $tw->writeContent($item->Name);
  292. }
  293. $tw->closeDiv();
  294. $tw->openDiv();
  295. $tw->openImg();
  296. $tw->addAttribute('class', $view->getCssClass() . '-thumbnail-image');
  297. $tw->addAttribute('fullsize', $item->FileName);
  298. $tw->addAttribute('alt', $item->Alt);
  299. $tw->addAttribute('src', $item->Thumbnail);
  300. $tw->closeImg();
  301. $tw->closeDiv();
  302. $tw->closeDiv();
  303. }
  304. $tw->openDiv();
  305. $tw->addAttribute('style', 'clear: both;');
  306. $tw->writeContent(" ");
  307. $tw->closeDiv(true);
  308. if ($model->getIsPaged())
  309. self::renderPager($model, $view);
  310. $tw->closeDiv();
  311. $tw->closeForm();
  312. $enableAsync = ($view->getIsAsynchronous() == true) ? 'true' : 'false';
  313. $javascript = "var js_" . HtmlViewBase::getHtmlId($model) . " = null;
  314. window.addEvent('domready', function () { js_" . HtmlViewBase::getHtmlId($model) . " = new HtmlGalleryView('" . HtmlViewBase::getHtmlId($model) . "', '" . $view->getCssClass() . "', $enableAsync); ";
  315. if(!$view->getEnableContextMenu())
  316. {
  317. $javascript.= "js_" . HtmlViewBase::getHtmlId($model).".disableContextMenu();";
  318. }
  319. $javascript.= "});";
  320. self::renderInitializationScript($javascript);
  321. }
  322. }
  323. ?>