Source for file viewer.php

Documentation is available at viewer.php

  1. <?php
  2.  
  3. /* Processing and output, called by the "component" interface class */
  4.  
  5. class net_nemein_downloads_viewer {
  6.  
  7. var $_debug_prefix;
  8.  
  9. var $_config;
  10. var $_release;
  11. var $_current_release;
  12. var $_topic;
  13. var $_layout;
  14. var $_view;
  15. var $_relocate;
  16. var $_relocate_prefix;
  17.  
  18. function net_nemein_downloads_viewer($topic, $config) {
  19.  
  20. global $midgard;
  21. $this->_debug_prefix = "net.nemein.downloads viewer::";
  22.  
  23. $this->_config = $config;
  24. $this->_topic = $topic;
  25. $this->_release = false;
  26. $this->_current_release = $this->_config->get("current_release");
  27. $this->_layout = false;
  28. $this->_view = false;
  29. $this->_relocate = false;
  30. $this->_relocate_prefix = "";
  31. $this->form_prefix = "net_nemein_downloads_viewer_";
  32.  
  33. // get l10n libraries
  34. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  35. $GLOBALS["view_l10n"] = $i18n->get_l10n("net.nemein.downloads");
  36.  
  37. }
  38.  
  39.  
  40. function can_handle($argc, $argv) {
  41.  
  42. debug_push($this->_debug_prefix . "can_handle");
  43.  
  44. // see if we can handle this request.
  45. if (!$this->_getRelease($argc, $argv)) {
  46. // errstr and errcode are already set by getArticle
  47. debug_add("could not get release. see above.");
  48. debug_pop();
  49. return false;
  50. }
  51. $GLOBALS["net_nemein_downloads_nap_activeid"] = $this->_release->id;
  52.  
  53. debug_pop();
  54. return true;
  55. }
  56.  
  57. function _getRelease($argc, $argv) {
  58. global $net_nemein_downloads_layouts;
  59.  
  60. debug_push($this->_debug_prefix . "_getRelease");
  61.  
  62. $this->_layout = new midcom_helper_datamanager($net_nemein_downloads_layouts);
  63.  
  64. if (! $this->_layout) {
  65. $this->errstr = "Could not create layout, see Debug Log";
  66. $this->errcode = MIDCOM_ERRCRIT;
  67. debug_add($this->errstr, MIDCOM_LOG_CRIT);
  68. return false;
  69. }
  70.  
  71. if ($argc == 0) {
  72. // Try to view current release
  73. if ($this->_current_release) {
  74. $this->_release = mgd_get_object_by_guid($this->_current_release);
  75.  
  76. if (!$this->_release) {
  77. debug_add("current release could not be loaded: " . mgd_errstr(), MIDCOM_LOG_INFO);
  78. $this->errstr = "current release could not be loaded: " . mgd_errstr();
  79. $this->errcode = MIDCOM_ERRNOTFOUND;
  80. return false;
  81. }
  82. } else {
  83. // No release is the default, show index
  84. $this->_view = "index";
  85. return true;
  86. }
  87. } elseif ($argc == 1) {
  88.  
  89. $this->_release = mgd_get_article_by_name($this->_topic->id,$argv[0]);
  90. if (!$this->_release) {
  91. debug_add("release $argv[0] could not be loaded: " . mgd_errstr(), MIDCOM_LOG_INFO);
  92. $this->errstr = "release $argv[0] could not be loaded: " . mgd_errstr();
  93. $this->errcode = MIDCOM_ERRNOTFOUND;
  94. return false;
  95. }
  96. } elseif ($argc == 2) {
  97.  
  98. // Redirections for current release
  99. if ($argv[0] == "latest") {
  100.  
  101. if ($this->_current_release) {
  102. // Use set "current release"
  103. $this->_release = mgd_get_object_by_guid($this->_current_release);
  104. } else {
  105. // Use latest added release
  106. $releases = mgd_list_topic_articles($this->_topic->id);
  107. if ($releases) {
  108. if ($releases->fetch()) {
  109. $this->_release = mgd_get_article($releases->id);
  110. }
  111. }
  112. }
  113.  
  114. if (!$this->_release) {
  115. debug_add("current release could not be loaded: " . mgd_errstr(), MIDCOM_LOG_INFO);
  116. $this->errstr = "current release could not be loaded: " . mgd_errstr();
  117. $this->errcode = MIDCOM_ERRNOTFOUND;
  118. return false;
  119. }
  120.  
  121. // Load needed prefixes for relocations
  122. $host = mgd_get_host($GLOBALS["midgard"]->host);
  123. $host_prefix = $host->prefix."/";
  124.  
  125. switch ($argv[1]) {
  126.  
  127. case "web":
  128. // Relocate to release's web page
  129. $this->_relocate = $this->_release->name.".html";
  130. break;
  131.  
  132. case "file":
  133. // Relocate to first file in release
  134. $files = $this->_release->listattachments();
  135. if ($files) {
  136. if ($files->fetch()) {
  137. $this->_relocate = "midcom-serveattachmentguid-".$files->guid()."/".$files->name;
  138. $this->_relocate_prefix = $host_prefix;
  139. }
  140. }
  141. break;
  142.  
  143. default:
  144. // Relocate to first file in release
  145. $files = $this->_release->listattachments();
  146. if ($files) {
  147. while ($files->fetch()) {
  148. if ($files->name == $argv[1]) {
  149. $this->_relocate = $host_prefix."midcom-serveattachmentguid-".$files->guid()."/".$files->name;
  150. $this->_relocate_prefix = $host_prefix;
  151. }
  152. }
  153. }
  154. break;
  155. }
  156.  
  157. if (!$this->_relocate) {
  158. return false;
  159. }
  160. }
  161.  
  162. }
  163. if ($this->_release) {
  164. $this->_view = "release";
  165. if ($this->_release && ! $this->_layout->init($this->_release)) {
  166. $this->errstr = "Could not initialize layout, see Debug Log";
  167. $this->errcode = MIDCOM_ERRCRIT;
  168. debug_add($this->errstr, MIDCOM_LOG_CRIT);
  169. return false;
  170. }
  171. return true;
  172.  
  173. } else {
  174.  
  175. debug_add("Release could not be loaded", MIDCOM_LOG_DEBUG);
  176. debug_pop();
  177. return false;
  178. }
  179. }
  180.  
  181. function handle() {
  182. global $_REQUEST;
  183. debug_push($this->_debug_prefix . "handle");
  184.  
  185. if (!$this->_relocate_prefix) {
  186. $this->_relocate_prefix = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX);
  187. }
  188. if ($this->_relocate) {
  189. debug_add("Relocating to ".$this->_relocate);
  190. $GLOBALS["midcom"]->relocate($this->_relocate_prefix.$this->_relocate);
  191. }
  192.  
  193. debug_pop();
  194. return true;
  195. }
  196.  
  197. function show() {
  198. global $midcom;
  199. global $view;
  200. debug_push($this->_debug_prefix . "show");
  201. if ($this->_view == "release") {
  202. $view = $this->_layout->get_array();
  203. midcom_show_style("view-release-header");
  204. midcom_show_style("view-release");
  205. midcom_show_style("view-release-footer");
  206. } elseif (!$this->_release) {
  207. // Main topic view / release listing
  208. $releases = mgd_list_topic_articles($this->_topic->id,"reverse created");
  209. if ($releases) {
  210. $view = $this->_topic;
  211. midcom_show_style("view-index-header");
  212. while ($releases->fetch()) {
  213. $release = mgd_get_article($releases->id);
  214. $this->_layout->init($release);
  215. $view = $this->_layout->get_array();
  216. midcom_show_style("view-index-item");
  217. }
  218. midcom_show_style("view-index-footer");
  219. }
  220. }
  221. debug_pop();
  222. return true;
  223. }
  224.  
  225.  
  226. function get_metadata() {
  227.  
  228. // metadata for the current element
  229. /*
  230. return array (
  231. MIDCOM_META_CREATOR => ...,
  232. MIDCOM_META_EDITOR => ...,
  233. MIDCOM_META_CREATED => ...,
  234. MIDCOM_META_EDITED => ...
  235. );*/
  236. }
  237.  
  238. } // viewer
  239.  
  240. ?>

Documentation generated on Mon, 21 Nov 2005 18:22:28 +0100 by phpDocumentor 1.3.0RC3