]> dev.renevier.net Git - syj.git/blob - application/forms/Geom.php
allow opening terms of use box with space keyboard
[syj.git] / application / forms / Geom.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Form_Geom extends Zend_Form implements Syj_Processor_Interface
6 {
7     protected $_decorators = array(
8                             'FormElements' => array('decorator' => 'FormElements', 'options' => null),
9                             'Form' => array('decorator' => 'Form', 'options' => null)
10                             );
11
12     public function __construct($options = null) {
13         $translator = $this->getTranslator();
14
15         $data = array('Hidden', 'geom_data', array('required' => true, 'decorators' => array('ViewHelper', 'Errors')));
16
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(
21                                             'File',
22                                             'Errors',
23                                             array('Label', array('separator' => '<br>')),
24                                             array('HtmlTag', array('tag' => 'div', 'id' => 'geom_upload_container')),
25                                         )));
26
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(
32                                 'ViewHelper',
33                                 'Errors',
34                                 array('Label', array('separator' => '<br>')),
35                                 array('HtmlTag', array('tag' => 'div', 'id' => 'geom_title_container')),
36                                 )));
37
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(
46                                   'ViewHelper',
47                                   'label',
48                                   array('HtmlTag', array('tag' => 'div', 'id' => 'geom_accept_container', 'class' => 'logged-hide')))));
49
50         $submit = array('Submit', 'geom_submit', array('label' => __("save"), 'decorators' => array(
51                             'ViewHelper',
52                             'Errors',
53                             array('HtmlTag', array('tag' => 'br', 'openOnly' => true)))));
54         $this->addElements(array($data, $upload, $title, $touaccept, $submit));
55
56         $decorator = $this->geom_accept->getDecorator('Zend_Form_Decorator_Label');
57         $decorator->setOption('escape', false);
58
59         parent::__construct($options);
60     }
61
62     public function process(&$data) {
63         $upload = null;
64         if ($this->getValue("geom_upload")) {
65             $file = $this->geom_upload;
66             $upload = $file->getDestination() . DIRECTORY_SEPARATOR . $file->getValue();
67
68             if (!isset($data["geom_data"]) || !$data["geom_data"]) {
69                 if (!file_exists($upload)) {
70                     throw new Zend_Exception();
71                 }
72                 if (!@filesize($upload)) {
73                     throw new Syj_Exception_InvalidGeomUpload();
74                 }
75                 $content = @file_get_contents ($upload);
76                 if ($content == false) {
77                     throw new Zend_Exception();
78                 }
79                 $data['geom_data'] = $content;
80             }
81             @unlink ($upload);
82         } else if (isset($_FILES['geom_upload']) and ($_FILES['geom_upload']['error']) == UPLOAD_ERR_INI_SIZE) {
83             throw new Syj_Exception_ToolargeGeomUpload();
84         } else {
85             $this->removeElement("geom_upload");
86         }
87         return $this;
88     }
89
90 }