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