addScript('js/lib/form-element-multifile.js')
->addInitFunction('Qs_Form_Element_MultiFile.init', array($this->getElement()->getId(), $this->_getScriptOptions()));
return $this;
}
public function render($content)
{
$element = $this->getElement();
if (!$element instanceof Zend_Form_Element) {
return $content;
}
$view = $element->getView();
if (!$view instanceof Zend_View_Interface) {
return $content;
}
$name = $element->getName();
$id = $element->getId();
$attribs = $this->getAttribs();
if (!array_key_exists('id', $attribs)) {
$attribs['id'] = $name;
}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$markup = array();
$size = $element->getMaxFileSize();
if ($size > 0) {
$element->setMaxFileSize(0);
$markup[] = $view->formHidden('MAX_FILE_SIZE', $size);
}
if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) {
$markup[] = $view->formHidden('APC_UPLOAD_PROGRESS', uniqid(), array('id' => 'progress_key'));
} else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) {
$markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), array('id' => 'progress_key'));
}
if ($element->isArray()) {
$multiValues = $this->_getMultiValues();
$name .= "[]";
$_content = '
';
$_content .= ''
. ''
. 'File | '
. 'Size | '
. 'Options | '
. '
'
. '';
if (empty($multiValues)) {
$_content .= $this->_renderRow($name, $attribs);
} else {
$uploadedSize = 0;
foreach ($multiValues as $file) {
$uploadedSize += $file['size'];
}
$_content .= ''
. ''
. 'Uploaded Total File(s) Size: | '
. '' . Qs_Math::sizeToByteString($uploadedSize) . ' | '
. ' | '
. '
'
. '';
$_content .= '';
foreach ($multiValues as $value) {
$_content .= $this->_renderRow($name, $attribs, $value);
}
}
$_content .= '
' . $this->_renderAddLink();
} else {
$markup[] = $view->formFile($name, $attribs);
$_content = implode($separator, $markup);
}
switch ($placement) {
case self::PREPEND:
return $_content . $separator . $content;
case self::APPEND:
default:
return $content . $separator . $_content;
}
}
protected function _getMultiValues()
{
$multiValues = $this->getElement()->getValue();
if (empty($multiValues) || !is_array($multiValues)) {
$multiValues = array();
return $multiValues;
}
foreach ($multiValues as $index => $value) {
if (is_string($value)) {
$dir = $this->getElement()->getDestination($this->getElement()->getName());
if (is_array($dir)) {
$dir = current($dir);
}
$fileName = $dir . '/' . $value;
$file = array(
'name' => $value,
'title' => preg_replace('/(.*)(\(\d+\)o?)(\.[^\.]+)$/', '$1$3', $value),
'size' => filesize($fileName),
);
$multiValues[$index] = $file;
}
}
return $multiValues;
}
protected function _renderRow($name, $attribs, $value = null)
{
$_attribs = $attribs;
$_attribs['id'] = $attribs['id']. '-' . $this->_rowIndex++;
$view = $this->getElement()->getView();
$content = ''
. '';
if (null === $value) {
$content .= $view->formFile($name, $_attribs);
$content .= ' | | ';
} else {
$content .= $view->formHidden($name, $value['name'])
. ''
. htmlspecialchars($value['title'])
. '';
$content .= ''
. (($value['size']) ? Qs_Math::sizeToByteString($value['size']) : '')
. ' | ';
}
$content .= ''
. '' . $this->_renderOptions() . ' | '
. '
';
return $content;
}
protected function _renderOptions()
{
$id = $this->getElement()->getId();
$content = $this->_renderLink(
'delete',
array(
'class' => 'del',
'href' => '#',
'onclick' => 'return Qs_Form_Element_MultiFile.del(this, \'' . $id . '\');'
)
);
return $content;
}
protected function _renderLink($text, $attribs, $separator = '')
{
$content = $this->_getOpenTag('a', $attribs)
. $this->_getText($text)
. $this->_getCloseTag('a')
. '' . $separator . '';
return $content;
}
protected function _getOpenTag($tag, array $attribs = null)
{
$html = '<' . $tag;
if (null !== $attribs) {
$html .= $this->_htmlAttribs($attribs);
}
$html .= '>';
return $html;
}
protected function _getCloseTag($tag)
{
return '' . $tag . '>';
}
protected function _htmlAttribs(array $attribs)
{
$xhtml = '';
foreach ((array) $attribs as $key => $val) {
$key = htmlspecialchars($key, ENT_COMPAT, 'UTF-8');
if (is_array($val)) {
$val = implode(' ', $val);
}
$val = htmlspecialchars($val, ENT_COMPAT, 'UTF-8');
$xhtml .= " $key=\"$val\"";
}
return $xhtml;
}
protected function _renderScriptOptions()
{
$options = $this->_getScriptOptions();
return json_encode($options);
}
protected function _renderAddLink($hidden = false)
{
$id = $this->getElement()->getId();
$attribs = array(
'id' => $id . '-add',
'class' => 'btn',
'onclick' => "return Qs_Form_Element_MultiFile.add('{$id}')",
'href' => '#'
);
if ($hidden) {
$attribs['style'] = 'display:none;';
}
$content = $this->_getOpenTag('a', $attribs);
$content .= $this->_getText('Add File');
$content .= $this->_getCloseTag('a');
return $content;
}
protected function _getScriptOptions()
{
$options = array(
'name' => $this->getElement()->getFullyQualifiedName(),
'required' => $this->getElement()->isRequired(),
);
$options['itemName'] = $this->_getText('file');
return $options;
}
protected function _getText($text)
{
if ($translator = $this->getElement()->getTranslator()) {
return $translator->translate($text);
}
return $text;
}
}