Source for file _cmdattachment.php

Documentation is available at _cmdattachment.php

  1. <?php
  2.  
  3. class midcom_admin_content__cmdattachment {
  4. var $_argv;
  5. var $_contentadm;
  6. var $_topic;
  7. var $_view;
  8. var $_attachment_id;
  9. function midcom_admin_content__cmdattachment ($argv, &$contentadm) {
  10. $this->_argv = $argv;
  11. $this->_contentadm = &$contentadm;
  12. $this->_topic = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_CONTENTTOPIC);
  13. }
  14.  
  15.  
  16. function execute () {
  17. debug_push("Content Admin, Attachment Command Execute");
  18. if (count ($this->_argv) == 0)
  19. $this->_argv[] = "show";
  20. switch ($this->_argv[0]) {
  21. case "show":
  22. $this->_view = "attachments_index";
  23. debug_pop();
  24. return true;
  25. case "create":
  26. $this->_view = "attachments_create";
  27. debug_pop();
  28. return true;
  29.  
  30. case "createok":
  31. debug_pop();
  32. return $this->_admin_attachments_upload();
  33.  
  34. case "delete":
  35. if (count($this->_argv) != 2) {
  36. $this->_contentadm->errstr = "This Attachnment does not exist.";
  37. $this->_contentadm->errcode = MIDCOM_ERRNOTFOUND;
  38. debug_add($this->_contentadm->errstr);
  39. debug_pop();
  40. return false;
  41. }
  42. $this->_attachment_id = $this->_argv[1];
  43. $this->_view = "attachments_delete";
  44. debug_pop();
  45. return true;
  46.  
  47. case "deleteok":
  48. if (count($this->_argv) != 2) {
  49. $this->_contentadm->errstr = "This Attachnment does not exist.";
  50. $this->_contentadm->errcode = MIDCOM_ERRNOTFOUND;
  51. debug_add($this->_contentadm->errstr);
  52. debug_pop();
  53. return false;
  54. }
  55. $this->_attachment_id = $this->_argv[1];
  56. debug_pop();
  57. return $this->_admin_attachments_delete();
  58.  
  59. default:
  60. $this->_contentadm->errstr = "Unkown Command in Execute Handler, this should not happen.";
  61. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  62. debug_add($this->_contentadm->errstr);
  63. debug_pop();
  64. return false;
  65. };
  66. }
  67.  
  68. function _admin_attachments_upload () {
  69. debug_push ("Data Admin, Attachment Upload");
  70. $this->_view = "attachments_index";
  71.  
  72. if (array_key_exists("f_cancel", $_REQUEST)) {
  73. debug_add("Cancel pressed");
  74. debug_pop();
  75. return true;
  76. }
  77.  
  78. if (!array_key_exists("f_submit", $_REQUEST)) {
  79. $this->_contentadm->errstr = "The submit button was not in the request data.";
  80. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  81. debug_add($this->_contentadm->errstr);
  82. debug_pop();
  83. return false;
  84. }
  85.  
  86. // Uploaded files are in _FILES autoglobal on PHP 4.3+ and _REQUEST on earlier
  87. if (version_compare(phpversion(),"4.3.0",">="))
  88. $the_file = $_FILES["f_file"];
  89. else
  90. $the_file = $_REQUEST["f_file"];
  91.  
  92. if (trim($the_file["name"]) == "") {
  93. $this->_contentadm->msg = "Error: No file was uploaded.";
  94. debug_pop();
  95. return true;
  96. }
  97.  
  98.  
  99. if (trim($_REQUEST["f_filename"]) == "")
  100. $name = trim($the_file["name"]);
  101. else
  102. $name = trim($_REQUEST["f_filename"]);
  103.  
  104. // $topic = mgd_get_topic ($this->_attachment_topic_id);
  105. $attachment = $this->_topic->getattachment($name);
  106. if ($attachment !== false) {
  107. $this->_contentadm->msg = "Error: an Attachment with this name already exists.";
  108. debug_pop();
  109. return true;
  110. }
  111. $id = $this->_topic->createattachment($name, trim($_REQUEST["f_title"]),
  112. $the_file["type"]);
  113. if (!$id) {
  114. $this->_contentadm->msg = "Error: Could not create Attachment: " . mgd_errstr();
  115. return true;
  116. }
  117. $attachment = mgd_get_attachment($id);
  118. $dst = $this->_topic->openattachment($name);
  119. $src = fopen($the_file["tmp_name"], "r");
  120. while (!feof($src))
  121. fwrite($dst,fread($src,65536));
  122. fclose($dst);
  123. fclose($src);
  124. $this->_contentadm->msg = "Attachment created successfully.";
  125. $GLOBALS['midcom']->cache->invalidate($attachment->guid());
  126. debug_add("Invalidated Midcom Cache.");
  127. debug_pop();
  128. return true;
  129. }
  130.  
  131. function _admin_attachments_delete () {
  132. debug_push("Data Admin, Attachment Delete");
  133.  
  134. $this->_view = "attachments_index";
  135. $this->_contentadm->msg = "";
  136. if (array_key_exists("f_cancel", $_REQUEST)) {
  137. debug_add("Cancel pressed");
  138. debug_pop();
  139. return true;
  140. }
  141.  
  142. if (!array_key_exists("f_submit", $_REQUEST)) {
  143. $this->_contentadm->errstr = "The submit button was not in the request data.";
  144. $this->_contentadm->errcode = MIDCOM_ERRINTERNAL;
  145. debug_add($this->_contentadm->errstr);
  146. debug_pop();
  147. return false;
  148. }
  149. $attachment = mgd_get_attachment($this->_attachment_id);
  150. if (! mgd_delete_extensions($attachment)) {
  151. $this->_contentadm->msg = "Error: Could not delete Attachment: " . mgd_errstr();
  152. debug_pop();
  153. return true;
  154. }
  155. if (! mgd_delete_attachment($attachment->id)) {
  156. $this->_contentadm->msg = "Error: Could not delete Attachment: " . mgd_errstr();
  157. debug_pop();
  158. return true;
  159. }
  160. $this->_contentadm->msg = "Done.";
  161. $GLOBALS['midcom']->cache->invalidate($attachment->guid());
  162. debug_add("Invalidated Midcom Cache.");
  163. debug_pop();
  164. return true;
  165. }
  166.  
  167. function show () {
  168. eval ("\$this->_show_$this->_view();");
  169. }
  170.  
  171. function _show_attachments_index () {
  172. global $view;
  173. $attachments = mgd_fetch_to_array($this->_topic->listattachments());
  174. if (!$attachments) {
  175. midcom_show_style("attachments-index-none");
  176. } else {
  177. midcom_show_style("attachments-index");
  178. foreach ($attachments as $id) {
  179. $view = mgd_get_attachment ($id);
  180. midcom_show_style("attachments-index-element");
  181. }
  182. midcom_show_style("attachments-index-");
  183. }
  184. }
  185.  
  186. function _show_attachments_create () {
  187. midcom_show_style("attachments-create");
  188. }
  189. function _show_attachments_delete () {
  190. $GLOBALS["view"] = mgd_get_attachment ($this->_attachment_id);
  191. midcom_show_style("attachments-delete");
  192. }
  193. }
  194.  
  195. ?>

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