| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: |
|
| 7: | namespace Sphp\Comp\Form{
|
| 8: |
|
| 9: |
|
| 10: | class FileUploader extends \Sphp\tools\Component{
|
| 11: | public $maxLen = '';
|
| 12: | public $minLen = '';
|
| 13: | public $valuehid = '';
|
| 14: | private $formName = '';
|
| 15: | private $msgName = '';
|
| 16: | private $defaultImg = '';
|
| 17: | private $req = false;
|
| 18: | private $fileType = '';
|
| 19: | private $fileExtention = '';
|
| 20: | private $filename = '';
|
| 21: | private $fileTypeA = '';
|
| 22: | private $fileSize = '';
|
| 23: | private $fileFrontName = '';
|
| 24: | private $fildName = '';
|
| 25: | private $tablName = '';
|
| 26: | private $fileSavePath = '';
|
| 27: | private $btnDelete = false;
|
| 28: | private $imgTag = false;
|
| 29: | private $errmsg = "";
|
| 30: |
|
| 31: | protected function oncreate($element) {
|
| 32: | $page = \SphpBase::page();
|
| 33: | $tblName = \SphpBase::page()->tblName;
|
| 34: | $JSServer = \SphpBase::JSServer();
|
| 35: | $Client = \SphpBase::sphp_request();
|
| 36: | $mysql = \SphpBase::dbEngine();
|
| 37: | if(!$element->hasAttribute("name")){
|
| 38: | $this->HTMLName = $this->name;
|
| 39: | }else{
|
| 40: | $this->HTMLName = $this->getAttribute("name");
|
| 41: | }
|
| 42: |
|
| 43: | $this->unsetEndTag();
|
| 44: | if(\SphpBase::sphp_request()->isFile("$this->name")){
|
| 45: | $f1 = \SphpBase::sphp_request()->files("$this->name");
|
| 46: | $this->fileType = $f1["type"];
|
| 47: | $this->fileSize = $f1["size"];
|
| 48: | $this->fileFrontName = $f1["tmp_name"];
|
| 49: | if($this->fileFrontName!=''){
|
| 50: | \SphpBase::sphp_request()->request($this->name,false,$f1["name"]);
|
| 51: | $ft = pathinfo($f1["name"]);
|
| 52: | $this->fileExtention = $ft['extension'];
|
| 53: | $this->filename = $ft['filename'];
|
| 54: |
|
| 55: | }
|
| 56: | }
|
| 57: | $this->tagName = "input";
|
| 58: | if(\SphpBase::page()->sact== $this->name.'del'){
|
| 59: | $file = substr(decryptme(\SphpBase::sphp_request()->request('pfn')),3);
|
| 60: | $pt = pathinfo($file);
|
| 61: | if(file_exists($file)){unlink($file);}
|
| 62: | if(file_exists('cache/'.$pt['basename'])){unlink('cache/'.$pt['basename']);}
|
| 63: | if($this->fildName!=''){
|
| 64: | $mysql->executeQueryQuick("UPDATE $tblName SET $this->fildName='' WHERE id='". \SphpBase::page()->getEventParameter() . "'");
|
| 65: | }
|
| 66: | $JSServer->addJSONBlock('html','out'.$this->name,'Pic Deleted!');
|
| 67: | }
|
| 68: | $this->setAttribute('type', 'file');
|
| 69: | if($this->getAttribute('blndontsave')!= "true"){
|
| 70: | $this->saveFile($this->fileSavePath);
|
| 71: | }
|
| 72: |
|
| 73: | }
|
| 74: |
|
| 75: | protected function genhelpPropList() {
|
| 76: | parent::genhelpPropList();
|
| 77: | $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$formid');
|
| 78: | $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
|
| 79: | $this->addHelpPropList('blndontsave','dont save file on server','','$val','select','true,false');
|
| 80: | $this->addHelpPropFunList('setRequired','Can not submit Empty','','');
|
| 81: | $this->addHelpPropFunList('setImage','Set Image Path to display as file icon','','$filepath');
|
| 82: | $this->addHelpPropFunList('setDisplayFile','Show File','','');
|
| 83: | $this->addHelpPropFunList('setDeleteButton','Show File Delete Button','','');
|
| 84: | $this->addHelpPropFunList('setFileMaxLen','Set allow Max file size to upload','','$size');
|
| 85: | $this->addHelpPropFunList('setFileMinLen','Set allow Min file size to upload','','');
|
| 86: | $this->addHelpPropFunList('setFileTypesAllowed','Set MIME types list with comma separated string','','$list');
|
| 87: | $this->addHelpPropFunList('setFileSavePath','Set file save path on server','','$filepath');
|
| 88: | }
|
| 89: |
|
| 90: | public function deleteFile() {
|
| 91: | $file = decryptme(\SphpBase::sphp_request()->request("hid" . $this->name));
|
| 92: | if($file != ""){
|
| 93: | $file = substr($file,3);
|
| 94: | $pt = pathinfo($file);
|
| 95: | if(file_exists($file)){unlink($file);}
|
| 96: | if(file_exists('cache/'.$pt['basename'])){unlink('cache/'.$pt['basename']);}
|
| 97: | }
|
| 98: | }
|
| 99: | public function isUpdateFile() {
|
| 100: | $file = decryptme(\SphpBase::sphp_request()->request("hid" . $this->name));
|
| 101: | if($file != "" && $this->value != ""){
|
| 102: | $file = substr($file,3);
|
| 103: | if($file != $this->value){
|
| 104: | return true;
|
| 105: | }else{
|
| 106: | return false;
|
| 107: | }
|
| 108: | }else{
|
| 109: | return false;
|
| 110: | }
|
| 111: | }
|
| 112: | public function fu_setDisplayFile() {
|
| 113: | $this->imgTag = true;
|
| 114: | }
|
| 115: | public function fu_setDeleteButton() {
|
| 116: | $this->btnDelete = true;
|
| 117: | }
|
| 118: | protected function onit() {
|
| 119: | if($this->getAttribute("placeholder") != ""){
|
| 120: | $this->msgName = $this->getAttribute("placeholder");
|
| 121: | }
|
| 122: |
|
| 123: | }
|
| 124: |
|
| 125: | public function fu_setImage($val) { $this->defaultImg = $val;}
|
| 126: | public function fi_setForm($val) { $this->formName = $val;}
|
| 127: | public function fu_setMsgName($val) { $this->msgName = $val; $this->setAttribute('placeholder', $val);}
|
| 128: | public function fi_setRequired() {
|
| 129: | if($this->issubmit){
|
| 130: | if(strlen($this->value) < 1){
|
| 131: | $this->setErrMsg($this->getAttribute("placeholder") .' ' . "Can not submit Empty");
|
| 132: | }
|
| 133: | }
|
| 134: | $this->req = true;
|
| 135: | }
|
| 136: | public function fi_setFileMaxLen($val)
|
| 137: | {
|
| 138: | $this->maxLen = $val;
|
| 139: | if($this->issubmit){
|
| 140: | if($this->getFileSize() > $val){
|
| 141: | $this->setErrMsg($this->getAttribute("placeholder") .' ' . "Maximum File Length should not be exceed then $val bytes");
|
| 142: | }
|
| 143: | }
|
| 144: | }
|
| 145: | public function getFileMaxLen() { return $this->maxLen; }
|
| 146: | public function fi_setFileMinLen($val)
|
| 147: | {
|
| 148: | $this->minFileLen = $val;
|
| 149: | if($this->issubmit){
|
| 150: | if($this->getValue()!='' && $this->getFileSize() < $val ){
|
| 151: | $this->setErrMsg("Minimum File Length should be $val bytes");
|
| 152: | }
|
| 153: | }
|
| 154: | }
|
| 155: | public function getFileMinLen() { return $this->minLen; }
|
| 156: | public function fi_setFileType($val){$this->fileType = $val;}
|
| 157: | public function getFileType(){return $this->fileType;}
|
| 158: | public function fi_setFileSize($val){$this->fileSize = $val;}
|
| 159: | public function getFileSize(){return $this->fileSize;}
|
| 160: | public function fi_setFileTypesAllowed($val){$this->fileTypeA = $val; $this->findTypeAllowed();}
|
| 161: | public function getFileTypesAllowed(){return $this->fileTypeA;}
|
| 162: | public function getFileFrontName(){return $this->fileFrontName;}
|
| 163: | public function getFileName(){return $this->filename;}
|
| 164: | public function getFilePrevName(){return \SphpBase::sphp_request()->request('hid'.$this->name);}
|
| 165: | public function getFileExtension(){return $this->fileExtention;}
|
| 166: | public function fi_setFileSavePath($val){$this->fileSavePath = $val;}
|
| 167: |
|
| 168: | private function findTypeAllowed(){
|
| 169: | $blnFound = false;
|
| 170: | if($this->issubmit){
|
| 171: | $types = explode(',',$this->getFileTypesAllowed());
|
| 172: | foreach($types as $key=>$val){
|
| 173: | if($val == $this->getFileType()){
|
| 174: | $blnFound = true;
|
| 175: | break;
|
| 176: | }
|
| 177: | }
|
| 178: | if(!$blnFound){
|
| 179: | $this->setErrMsg($this->getAttribute("placeholder") .' ' . $this->getFileType(). ' File Type is not allowed');
|
| 180: | }
|
| 181: | }
|
| 182: |
|
| 183: | }
|
| 184: | public function saveFile($FilePath){
|
| 185: | if ($this->issubmit && !getCheckErr()){
|
| 186: | if (\SphpBase::sphp_request()->isFile($this->name) && \SphpBase::sphp_request()->files($this->name)["error"] > 0)
|
| 187: | {
|
| 188: | $this->setErrMsg(\SphpBase::sphp_request()->files($this->name)["error"]);
|
| 189: | }
|
| 190: | else{
|
| 191: | $p1 = "";
|
| 192: | if(\SphpBase::sphp_request()->isNativeClient){
|
| 193: | $p1 = \SphpBase::sphp_request()->request($this->name);
|
| 194: | if(file_exists($p1) && !rename($p1,$FilePath )){
|
| 195: | $this->setErrMsg("Can not save file on server");
|
| 196: | }else{
|
| 197: | $this->value = $FilePath;
|
| 198: | }
|
| 199: | }else{
|
| 200: | $p1 = \SphpBase::sphp_request()->files($this->name)["tmp_name"];
|
| 201: | if(!move_uploaded_file($p1, $FilePath )){
|
| 202: | $this->setErrMsg("Can not save file on server");
|
| 203: | }else{
|
| 204: | $this->value = $FilePath;
|
| 205: | }
|
| 206: | }
|
| 207: |
|
| 208: | }
|
| 209: |
|
| 210: | }
|
| 211: |
|
| 212: | }
|
| 213: |
|
| 214: |
|
| 215: | protected function onjsrender(){
|
| 216: |
|
| 217: | if($this->formName!=''){
|
| 218: | if($this->req){
|
| 219: | addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
|
| 220: | ctlReq['$this->name']= Array('$this->msgName','TextField');");
|
| 221: | }
|
| 222: | }
|
| 223: |
|
| 224: | }
|
| 225: | public function setErrMsg($msg){
|
| 226: | $this->errmsg .= '<strong class="alert-danger">' . $msg . '! </strong>';
|
| 227: | if(\SphpBase::sphp_request()->isAJAX()){
|
| 228: | \SphpBase::JSServer()->addJSONJSBlock('$("#'. $this->name .'").after("<strong class=\"alert-danger\">' . $msg . '! </strong>");');
|
| 229: | }
|
| 230: | setErr($this->name, $msg);
|
| 231: | }
|
| 232: |
|
| 233: | protected function onrender(){
|
| 234: | $page = \SphpBase::page();
|
| 235: | $imgtag = '';
|
| 236: | $btnd = '';
|
| 237: | if($this->errmsg!=""){
|
| 238: | $this->addPostTag($this->errmsg);
|
| 239: | }
|
| 240: |
|
| 241: | if($this->value !=""){
|
| 242: | $ft = pathinfo($this->value);
|
| 243: | $this->fileExtention = strtolower($ft['extension']);
|
| 244: | if($this->imgTag || $this->fileExtention == "jpeg" || $this->fileExtention == "jpg" || $this->fileExtention == "gif" || $this->fileExtention == "png"){
|
| 245: | if($this->defaultImg == ''){
|
| 246: | $this->defaultImg = $this->value;
|
| 247: | }
|
| 248: | $imgtag = '<img src="'.$this->defaultImg.'" width="150" height="100" />';
|
| 249: | }
|
| 250: | if($this->btnDelete){
|
| 251: | $btnd = '<a href="javascript: '. \SphpBase::JSServer()->postServer("'".getEventURL($this->name.'del',\SphpBase::page()->evtp,'','pfn='.encryptme('t7i' . $this->value),'',true)."'").'">Delete</a>';
|
| 252: | }
|
| 253: | }
|
| 254: |
|
| 255: | $this->setAttributeDefault("class", "form-control");
|
| 256: | if($this->value!=''){
|
| 257: | $this->setAttribute('value', $this->value);
|
| 258: | if($this->value!=''){
|
| 259: | $this->valuehid = encryptme('t7i'.$this->value);
|
| 260: | $this->addPostTag('<input type="hidden" name="hid'.$this->name.'" value="'. $this->valuehid .'" /><div id="out'.$this->name.'">
|
| 261: | '. $imgtag . $btnd .' </div>');
|
| 262: | }
|
| 263: | }
|
| 264: | switch($this->styler){
|
| 265: | case 1:{
|
| 266: | $this->element->appendPreTag('<div class="form-floating mb-3">');
|
| 267: | $this->element->appendPostTag('<label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label></div>');
|
| 268: | break;
|
| 269: | }case 2:{
|
| 270: | $this->element->appendPreTag('<div class="mb-3">
|
| 271: | <label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label>');
|
| 272: | $this->element->appendPostTag('<div id="'. $this->HTMLID .'Help" class="form-text">'. $this->helptext .'</div></div>');
|
| 273: | break;
|
| 274: | }
|
| 275: | }
|
| 276: |
|
| 277: | }
|
| 278: |
|
| 279: |
|
| 280: | public function getJSValue(){
|
| 281: | return "document.getElementById('$this->name').value" ;
|
| 282: | }
|
| 283: |
|
| 284: | public function setJSValue($exp){
|
| 285: | return "document.getElementById('$this->name').value = $exp;" ;
|
| 286: | }
|
| 287: |
|
| 288: | }
|
| 289: | }
|
| 290: | |