nyroFwk  0.2
form/file.class.php
Go to the documentation of this file.
1 <?php
10 class form_file extends form_abstract {
11 
17  protected $deleted = false;
18 
24  protected $keep;
25 
26  protected function beforeInit() {
27  $required = array_key_exists('required', $this->cfg->valid) && $this->cfg->getInArray('valid', 'required');
28 
29  $htVars = http_vars::getInstance();
30  $this->keep = $htVars->getVar($this->name.'NyroKeep');
31  if ($this->keep)
32  $this->cfg->value = $this->keep;
33 
34  $prm = array_merge($this->cfg->fileUploadedPrm, array(
35  'name'=>$this->cfg->name,
36  'current'=>$this->cfg->value,
37  'helper'=>$this->cfg->helper,
38  'helperPrm'=>$this->cfg->helperPrm,
39  'required'=>$required
40  ));
41 
42  if ($this->cfg->dir)
43  $prm['dir'] = $this->cfg->dir;
44  if ($this->cfg->subdir)
45  $prm['subdir'] = $this->cfg->subdir;
46 
47  $this->cfg->value = factory::get('form_fileUploaded', $prm);
48 
49  if ($this->cfg->autoDeleteOnGet && !$this->cfg->value->isSaved(true) && $htVars->getVar($this->name.'NyroDel')) {
50  $this->cfg->value->delete();
51  $this->deleted = true;
52  }
53 
54  $this->cfg->valid = array_merge($this->cfg->valid, array(
55  'callback'=>array($this->cfg->value, 'isValid')
56  ));
57  }
58 
59  protected function afterInit() {
60  parent::afterInit();
61  if (!$this->isValid())
62  $this->cfg->classLine.= ' fileError';
63  }
64 
65  public function getValue() {
66  return $this->cfg->value->getCurrent();
67  }
68 
69  public function setValue($value, $refill = false) {
70  if (!$this->deleted && !$refill) {
71  if ($value || !$this->keep)
72  $this->cfg->value->setCurrent($value, $refill);
73  }
74  }
75 
83  public function uploadify(array $opt = array(), $hideSubmit = true) {
84  $resp = response::getInstance();
85  $resp->addJs('jquery');
86  $resp->addJs('swfobject');
87  $resp->addJs('uploadify');
88  $resp->addCss('uploadify');
89 
90  $uploadifyOpt = array_merge(array(
91  'fileDataName'=>$this->name
92  ), $this->cfg->uploadify, $opt);
93 
94  if (request::isLocal())
95  $uploadifyOpt['scriptAccess'] = 'always';
96 
97  $resp->blockjQuery('$("#'.$this->id.'").uploadify('.utils::jsEncode($uploadifyOpt).');');
98  if ($hideSubmit)
99  $resp->blockjQuery('$("#'.$this->id.'").closest("form").find("fieldset.submit").hide();');
100  }
101 
102 
109  public function plupload(array $opts = array(), $hideSubmit = true) {
110  $resp = response::getInstance();
111  $resp->addJs('jquery');
112  $resp->addJs('plupload');
113  $resp->addJs('nyroPlupload');
114  $resp->addCss('plupload');
115 
116  $pluploadOpts = $this->cfg->plupload;
117  if (count($opts))
118  factory::mergeCfg($pluploadOpts, $opts);
119  $pluploadOpts['file_data_name'] = $this->name;
120 
121  $resp->blockjQuery('$("#'.$this->id.'").nyroPlupload('.utils::jsEncode($pluploadOpts).');');
122  if ($hideSubmit)
123  $resp->blockjQuery('$("#'.$this->id.'").closest("form").find("fieldset.submit").hide();');
124  }
125 
126  public function toHtml() {
127  if ($this->cfg->mode == 'view')
128  return $this->cfg->value->getView();
129 
130  $start = $end = null;
131  if ($this->cfg->showPreview || $this->cfg->showDelete) {
132  $start = '<'.$this->cfg->htmlWrap.'>';
133  if ($this->cfg->value->getCurrent()) {
134  $end.= '<input type="hidden" name="'.$this->name.'NyroKeep" value="'.$this->cfg->value->getCurrent().'" />';
135  $end.= '<span>';
136  if ($this->cfg->showDelete) {
137  $end.= '<a href="#" class="deleteFile" id="'.$this->id.'NyroDel">'.$this->cfg->deleteLabel.'</a>';
138  response::getInstance()->blockJquery('
139  $("#'.$this->id.'NyroDel").click(function(e) {
140  e.preventDefault();
141  $(this).parent("span").replaceWith("<input type=\"hidden\" name=\"'.$this->name.'NyroDel\" value=\"1\" />");
142  });');
143  } else
144  $end.= '<br />';
145  if ($this->cfg->showPreview)
146  $end.= $this->cfg->value->getView();
147  $end.= '</span>';
148  }
149  $end.= '</'.$this->cfg->htmlWrap.'>';
150  }
151  return $start.utils::htmlTag($this->htmlTagName,
152  array_merge($this->html, array(
153  'name'=>$this->name,
154  'id'=>$this->id,
155  ))).$end;
156  }
157 
158  public function toXul() {
159  return utils::htmlTag($this->xulTagName,
160  array_merge($this->xul, array(
161  'id'=>$this->id,
162  )));
163  }
164 
165 }
setValue($value, $refill=false)
static htmlTag($tag, array $attributes, $content=null)
Definition: utils.class.php:46
static jsEncode($vars)
static isLocal()
static getInstance()
static mergeCfg(array &$prm, array $cfg)
uploadify(array $opt=array(), $hideSubmit=true)
plupload(array $opts=array(), $hideSubmit=true)
static getInstance()
Definition: vars.class.php:29
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13