Source for file WFileUploadField.class.php

Documentation is available at WFileUploadField.class.php

  1. <?php
  2.  
  3. /**
  4. * @since Jul 21, 2005
  5. * @package polyphony.wizard.components
  6. *
  7. * @copyright Copyright &copy; 2005, Middlebury College
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  9. *
  10. * @version $Id: WFileUploadField.class.php,v 1.5 2007/09/19 14:04:51 adamfranco Exp $
  11. */
  12.  
  13. require_once (POLYPHONY."/main/library/Wizard/WizardComponent.abstract.php");
  14.  
  15. /**
  16. * This adds an input type='text' field to a {@link Wizard}.
  17. *
  18. * @since Jul 21, 2005
  19. * @package polyphony.wizard.components
  20. *
  21. * @copyright Copyright &copy; 2005, Middlebury College
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  23. *
  24. * @version $Id: WFileUploadField.class.php,v 1.5 2007/09/19 14:04:51 adamfranco Exp $
  25. */
  26. class WFileUploadField
  27. extends WizardComponent
  28. {
  29. var $_style = null;
  30. var $_filename = null;
  31. var $_hdfile = null;
  32. var $_size = null;
  33. var $_changed = false;
  34. var $_mimetype = null;
  35.  
  36. var $_errString = null;
  37.  
  38. var $_accept = array ();
  39. var $_startingDisplayFilename;
  40. var $_startingDisplaySize;
  41.  
  42. /**
  43. * Sets the CSS style of this field.
  44. * @param string $style
  45. * @access public
  46. * @return void
  47. */
  48. function setStyle($style) {
  49. $this->_style = $style;
  50. }
  51.  
  52. /**
  53. * Sets the accepted mimetypes (passed to the browser).
  54. * @param array $types
  55. * @access public
  56. * @return void
  57. */
  58. function setAcceptedMimetypes($types) {
  59. $this->_accept = $types;
  60. }
  61.  
  62. /**
  63. * Tells this field to use the passed file on the filesystem as a starting value. If $filename is
  64. * passed, it is used as the display filename instead of the basename of the path.
  65. * @param string $path
  66. * @param optional string $filename
  67. * @access public
  68. * @return void
  69. */
  70. function setFile($path, $filename = null) {
  71. $this->_filename = $filename != null ? $filename : basename($filename);
  72. $this->_size = filesize($filename);
  73. $this->_hdfile = $filename;
  74. }
  75. /**
  76. * Sets the values of the field to display until the user enters the field.
  77. * @param string $text
  78. * @access public
  79. * @return void
  80. */
  81. function setStartingDisplay ($filename, $size) {
  82. $this->_startingDisplayFilename = $filename;
  83. $this->_startingDisplaySize = $size;
  84. }
  85. /**
  86. * Sets the values of this field. This is needed when the field is part of
  87. * a repeatable collection.
  88. *
  89. * @param array $values
  90. * @access public
  91. * @return void
  92. */
  93. function setValue ($values) {
  94. foreach ($values as $key => $value) {
  95. switch ($key) {
  96. case "name":
  97. $this->_filename = $value;
  98. break;
  99. case "type":
  100. $this->_mimetype = $value;
  101. break;
  102. case "size":
  103. $this->_size = $value;
  104. break;
  105. case "starting_name":
  106. $this->_startingDisplayFilename = $value;
  107. break;
  108. case "starting_size":
  109. $this->_startingDisplaySize = $value;
  110. break;
  111. }
  112. }
  113. }
  114.  
  115. /**
  116. * Tells the wizard component to update itself - this may include getting
  117. * form post data or validation - whatever this particular component wants to
  118. * do every pageload.
  119. * @param string $fieldName The field name to use when outputting form data or
  120. * similar parameters/information.
  121. * @access public
  122. * @return boolean - TRUE if everything is OK
  123. */
  124. function update($fieldName) {
  125. $val = RequestContext :: value($fieldName);
  126. if (is_array($val)) {
  127. $uploadFile = $val['tmp_name'];
  128. if (is_uploaded_file($uploadFile)) {
  129. if ($val['error'] == 0) {
  130. // no error!
  131.  
  132. // get a new temp filename so PHP doesn't delete the uploaded file
  133. $tmpDir = dirname($uploadFile);
  134. $newFile = tempnam($tmpDir, "WU");
  135. if (file_exists($newFile))
  136. @ unlink($newFile);
  137.  
  138. if (move_uploaded_file($uploadFile, $newFile)) {
  139. // if we already have an uploaded file, delete it
  140. if ($this->_changed) {
  141. @ unlink($this->_hdfile);
  142. }
  143. $this->_changed = true;
  144. $this->_hdfile = $newFile;
  145. $this->_filename = $val['name'];
  146. $this->_mimetype = $val['type'];
  147. $this->_size = $val['size'];
  148. return true;
  149. }
  150. } else {
  151. // generate an error string for display
  152. $errString = '';
  153. switch ($val['error']) {
  154. case UPLOAD_ERR_INI_SIZE :
  155. case UPLOAD_ERR_FORM_SIZE :
  156. $errString = dgettext("polyphony", "File exceeded maximum allowed size.");
  157. break;
  158. case UPLOAD_ERR_PARTIAL :
  159. $errString = dgettext("polyphony", "An upload error occured. (partial upload)");
  160. break;
  161. case UPLOAD_ERR_NO_FILE :
  162. $errString = dgettext("polyphony", "An upload error occured. (no file)");
  163. break;
  164. case UPLOAD_ERR_NO_TMP_DIR :
  165. $errString = dgettext("polyphony", "An upload error occured. (no temp directory)");
  166. break;
  167. }
  168.  
  169. $this->_errString = $errString;
  170. }
  171. }
  172. }
  173. return false;
  174. }
  175.  
  176. /**
  177. * For file upload, returns an array:
  178. * name => the original name of the file
  179. * size => the size in bytes of the file
  180. * type => the mimetype of the file (if the browser supplied it)
  181. * tmp_name => the location on the hard drive of the file
  182. * @access public
  183. * @return mixed
  184. */
  185. function getAllValues() {
  186. $ar = array ();
  187. $ar['name'] = $this->_filename;
  188. $ar['size'] = $this->_size;
  189. $ar['type'] = $this->_mimetype;
  190. $ar['tmp_name'] = $this->_hdfile;
  191. return $ar;
  192. }
  193.  
  194. /**
  195. * Returns a block of XHTML-valid code that contains markup for this specific
  196. * component.
  197. * @param string $fieldName The field name to use when outputting form data or
  198. * similar parameters/information.
  199. * @access public
  200. * @return string
  201. */
  202. function getMarkup($fieldName) {
  203. $name = RequestContext :: name($fieldName);
  204.  
  205. $m = "";
  206.  
  207. if ($this->_filename) {
  208. $size = ByteSize::withValue($this->_size);
  209. $m .= "<i>". $this->_filename." (".$size->asString().")</i>\n";
  210. } else if ($this->_startingDisplayFilename && $this->_startingDisplaySize) {
  211. $size = ByteSize::withValue($this->_startingDisplaySize);
  212. $m .= "<i>". $this->_startingDisplayFilename
  213. ." (".$size->asString().")</i>\n";
  214. }
  215.  
  216. $m .= "<input type='file' name='$name'";
  217. if (count($this->_accept)) {
  218. $m .= " accept='".implode(", ", $this->_accept)."'";
  219. }
  220. if ($this->_style) {
  221. $m .= " style=\"".addslashes($this->_style)."\"";
  222. }
  223. $m .= " />";
  224.  
  225. if ($this->_errString) {
  226. $m .= "<span style='color: red; font-weight: 900;'>".$this->_errString."</span>";
  227. $this->_errString = null;
  228. }
  229.  
  230. return $m;
  231. }
  232. }
  233. ?>

Documentation generated on Wed, 19 Sep 2007 10:27:58 -0400 by phpDocumentor 1.3.0RC3