PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/db/fields/FileField.php

http://github.com/phpwax/phpwax
PHP | 138 lines | 105 code | 15 blank | 18 comment | 20 complexity | 92bcfc7c2dc362a119e862da4406ac66 MD5 | raw file
  1. <?php
  2. /**
  3. * WaxModelFields class
  4. *
  5. * @package PHP-Wax
  6. **/
  7. class FileField extends WaxModelField {
  8. //default name
  9. public $col_name;
  10. //extra file values
  11. public $file_root = "public/files/";
  12. public $url_root = "files/";
  13. //allowed extensions - array of exts, false means everythings allowed
  14. public $allowed_extensions = false;
  15. public $widget = "FileInput";
  16. public $messages = array(
  17. "format"=> "%s is not a valid format",
  18. "size_large" => "image uploaded is too large (%s or less)",
  19. "size_small" => "%s is too small",
  20. "required" => "%s is a required field"
  21. );
  22. public $data_type = "string";
  23. public $max_size = false;
  24. public $min_size = false;
  25. public function __construct($column, $model, $options = array()) {
  26. if(isset($options['allowed_extensions'])){
  27. if(is_array($options['allowed_extensions']) ){
  28. foreach($options['allowed_extensions'] as $index=> $extension) $this->allowed_extensions[] = $extension;
  29. } else $this->allowed_extensions[] = $options['allowed_extensions'];
  30. unset($options['allowed_extensions']);
  31. }
  32. $this->col_name = $column;
  33. parent::__construct($column, $model, $options);
  34. }
  35. public function setup() {
  36. $this->create_directory(WAX_ROOT.$this->file_root);
  37. parent::setup();
  38. }
  39. public function validate() {
  40. $this->valid_extension();
  41. $this->valid_size();
  42. if($this->required) $this->valid_required();
  43. }
  44. public function valid_extension(){
  45. $file = $_FILES[$this->model->table];
  46. $name= $file['name'][$this->col_name];
  47. if(strlen($name)){
  48. $ext = strtolower(substr($name, strrpos($name, ".") ));
  49. if($this->allowed_extensions && !in_array($ext, $this->allowed_extensions) ) $this->add_error($this->field, sprintf($this->messages["format"], $ext));
  50. }
  51. }
  52. public function valid_required() {
  53. $file = $_FILES[$this->model->table];
  54. $error= $file['error'][$this->col_name];
  55. if($error == 4) $this->add_error($this->field, sprintf($this->messages["required"], $this->label?:$this->field));
  56. }
  57. public function valid_size() {
  58. $file = $_FILES[$this->model->table];
  59. $error= $file['error'][$this->col_name];
  60. if($error == 2) $this->add_error($this->field, sprintf($this->messages["size_large"], $this->max_size / 1024 . "kb"));
  61. //if($this->min_size && $size < $this->min_size) $this->add_error($this->field, sprintf($this->messages["size"], $size));
  62. }
  63. /**** overides *****/
  64. public function get(){
  65. $ret = array("filename"=>$this->filename(), "path"=>$this->path(), "extension"=>$this->extension(), "url"=>$this->url() );
  66. return $ret;
  67. }
  68. public function __toString() {
  69. $file_info = $this->get();
  70. return $file_info["filename"];
  71. }
  72. //save function needs to handle the post upload of a single file
  73. public function save() {
  74. //file is present and has a valid size
  75. if(is_string($_FILES[$this->model->table]['name'][$this->col_name]) && ($_FILES[$this->model->table]['size'][$this->col_name] > 0) ){
  76. //save file to hdd & change col_name value to new_path
  77. $column = $this->col_name;
  78. $path = $this->save_file($_FILES[$this->model->table]['tmp_name'][$this->col_name], $_FILES[$this->model->table]['name'][$this->col_name]);
  79. if($path) $this->model->$column = $path;
  80. //prevent resaves for things like join models
  81. unset($_FILES[$this->model->table]['size'][$this->col_name]);
  82. }
  83. }
  84. /**** EXTRAS *****/
  85. private function create_directory($dir){
  86. if(is_dir($dir)) return true;
  87. if(substr_count($dir, WAX_ROOT)>0){
  88. $new_dir = str_replace(WAX_ROOT, "", $dir);
  89. $path = explode("/", $new_dir);
  90. $dirname = WAX_ROOT;
  91. foreach($path as $depth => $name){
  92. $dirname .= $name . "/";
  93. if(!is_dir($dirname) ) mkdir($dirname);
  94. }
  95. return true;
  96. } else return false;
  97. }
  98. public function save_file($up_tmp_name, $file){
  99. $file_destination = WAX_ROOT.$this->file_root.File::safe_file_save(WAX_ROOT.$this->file_root,$file);
  100. //if(move_uploaded_file($up_tmp_name, $file_destination) ) chmod($file_destination, 0777);
  101. if(rename($up_tmp_name, $file_destination) ) chmod($file_destination, 0777);
  102. else return false;
  103. return $file_destination;
  104. }
  105. //url path
  106. public function url(){
  107. $path = $this->path();
  108. return "/".str_replace(WAX_ROOT.$this->file_root, $this->url_root, $path);
  109. }
  110. //file path
  111. public function path(){
  112. $column = $this->col_name;
  113. return $this->model->row[$column];
  114. }
  115. public function filename(){
  116. $path = $this->path();
  117. return substr($path, strrpos($path, "/")+1 );
  118. }
  119. public function extension(){
  120. $path = $this->path();
  121. return substr($path, strrpos($path, "."));
  122. }
  123. }