2 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Form_Geom extends Zend_Form implements Syj_Processor_Interface
7 protected $_decorators = array(
8 'FormElements' => array('decorator' => 'FormElements', 'options' => null),
9 'Form' => array('decorator' => 'Form', 'options' => null)
12 public function __construct($options = null) {
13 $translator = $this->getTranslator();
15 $data = array('Hidden', 'geom_data', array('required' => true, 'decorators' => array('ViewHelper', 'Errors')));
17 $upload = array('File', 'geom_upload', array("label" => $translator->translate("Draw journey on the map or choose one from a file"),
18 'validators' => array(),
19 'ignoreNoFile' => true,
20 'decorators' => array(
23 array('Label', array('separator' => '<br>')),
24 array('HtmlTag', array('tag' => 'div', 'id' => 'geom_upload_container')),
27 $title = array('Text', 'geom_title', array(
28 'label' => __("optional title for this journey"),
29 'attribs' => array('maxlength' => '160', 'size' => '20'),
30 'validators' => array(new Zend_Validate_StringLength(0, 160)),
31 'decorators' => array(
34 array('Label', array('separator' => '<br>')),
35 array('HtmlTag', array('tag' => 'div', 'id' => 'geom_title_container')),
38 $anchor = $this->getView()->Anchor("termsofuse?format=raw",
39 $translator->translate("terms of use"),
40 array('id' => 'geom_termsofuse_anchor'));
41 $text = $translator->translate("I've read and accepted %s");
42 $text = vsprintf($text, $anchor);
43 $touaccept = array('Checkbox', 'geom_accept', array("label" => $text,
44 'helper' => 'SyjFormCheckbox', // similar to FormCheckbox without a hidden input
45 'decorators' => array(
48 array('HtmlTag', array('tag' => 'div', 'id' => 'geom_accept_container', 'class' => 'logged-hide')))));
50 $submit = array('Submit', 'geom_submit', array('label' => __("save"), 'decorators' => array(
53 array('HtmlTag', array('tag' => 'br', 'openOnly' => true)))));
54 $this->addElements(array($data, $upload, $title, $touaccept, $submit));
56 $decorator = $this->geom_accept->getDecorator('Zend_Form_Decorator_Label');
57 $decorator->setOption('escape', false);
59 parent::__construct($options);
62 public function process(&$data) {
64 if ($this->getValue("geom_upload")) {
65 $file = $this->geom_upload;
66 $upload = $file->getDestination() . DIRECTORY_SEPARATOR . $file->getValue();
68 if (!isset($data["geom_data"]) || !$data["geom_data"]) {
69 if (!file_exists($upload)) {
70 throw new Zend_Exception();
72 if (!@filesize($upload)) {
73 throw new Syj_Exception_InvalidGeomUpload();
75 $content = @file_get_contents ($upload);
76 if ($content == false) {
77 throw new Zend_Exception();
79 $data['geom_data'] = $content;
82 } else if (isset($_FILES['geom_upload']) and ($_FILES['geom_upload']['error']) == UPLOAD_ERR_INI_SIZE) {
83 throw new Syj_Exception_ToolargeGeomUpload();
85 $this->removeElement("geom_upload");