Skip to content
Snippets Groups Projects
class.forms.php 128 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
                        'desc'=>__('Enables attachments, regardless of channel'),
    
                    'validators' => function($self, $value) {
                        if (!ini_get('file_uploads'))
                            $self->addError(__('The "file_uploads" directive is disabled in php.ini'));
                    }
    
            + $fileupload_config;
    
    
        function isAttachmentsEnabled() {
            $config = $this->getConfiguration();
            return $config['attachments'];
        }
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function getWidget($widgetClass=false) {
    
            if ($hint = $this->getLocal('hint'))
    
    Peter Rotich's avatar
    Peter Rotich committed
                $this->set('placeholder', $hint);
            $this->set('hint', null);
            $widget = parent::getWidget($widgetClass);
            return $widget;
        }
    
    }
    
    class PriorityField extends ChoiceField {
    
        function getWidget($widgetClass=false) {
            $widget = parent::getWidget($widgetClass);
    
            if ($widget->value instanceof Priority)
                $widget->value = $widget->value->getId();
            return $widget;
        }
    
    
        function hasIdValue() {
            return true;
        }
    
    
        function getChoices($verbose=false) {
    
            $sql = 'SELECT priority_id, priority_desc FROM '.PRIORITY_TABLE
                  .' ORDER BY priority_urgency DESC';
    
            $choices = array('' => '— '.__('Default').' —');
    
            if (!($res = db_query($sql)))
                return $choices;
    
            while ($row = db_fetch_row($res))
                $choices[$row[0]] = $row[1];
            return $choices;
        }
    
        function parse($id) {
            return $this->to_php(null, $id);
        }
    
    
        function to_php($value, $id=false) {
    
            if (is_array($id)) {
                reset($id);
                $id = key($id);
            }
    
            elseif (is_array($value))
                list($value, $id) = $value;
    
            elseif ($id === false)
                $id = $value;
            if ($id)
                return Priority::lookup($id);
    
        }
    
        function to_database($prio) {
            return ($prio instanceof Priority)
                ? array($prio->getDesc(), $prio->getId())
                : $prio;
        }
    
        function toString($value) {
            return ($value instanceof Priority) ? $value->getDesc() : $value;
        }
    
    
        function searchable($value) {
            // Priority isn't searchable this way
            return null;
        }
    
    
        function getConfigurationOptions() {
    
            $choices = $this->getChoices();
            $choices[''] = __('System Default');
    
            return array(
                'prompt' => new TextboxField(array(
    
                    'id'=>2, 'label'=>__('Prompt'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Leading text shown before a value is selected'),
    
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
    
                'default' => new ChoiceField(array(
                    'id'=>3, 'label'=>__('Default'), 'required'=>false, 'default'=>'',
                    'choices' => $choices,
                    'hint'=>__('Default selection for this field'),
                    'configuration'=>array('size'=>20, 'length'=>40),
                )),
    
    
        function getConfiguration() {
            global $cfg;
    
            $config = parent::getConfiguration();
            if (!isset($config['default']))
                $config['default'] = $cfg->getDefaultPriorityId();
            return $config;
        }
    
    FormField::addFieldTypes(/*@trans*/ 'Dynamic Fields', function() {
    
        return array(
    
            'priority' => array(__('Priority Level'), PriorityField),
    
    class DepartmentField extends ChoiceField {
        function getWidget() {
            $widget = parent::getWidget();
            if ($widget->value instanceof Dept)
                $widget->value = $widget->value->getId();
            return $widget;
        }
    
        function hasIdValue() {
            return true;
        }
    
        function getChoices() {
            global $cfg;
    
            $choices = array();
            if (($depts = Dept::getDepartments()))
                foreach ($depts as $id => $name)
                    $choices[$id] = $name;
    
            return $choices;
        }
    
        function parse($id) {
            return $this->to_php(null, $id);
        }
    
        function to_php($value, $id=false) {
            if (is_array($id)) {
                reset($id);
                $id = key($id);
            }
            return $id;
        }
    
        function to_database($dept) {
            return ($dept instanceof Dept)
                ? array($dept->getName(), $dept->getId())
                : $dept;
        }
    
        function toString($value) {
            return (string) $value;
        }
    
        function searchable($value) {
            return null;
        }
    
        function getConfigurationOptions() {
            return array(
                'prompt' => new TextboxField(array(
                    'id'=>2, 'label'=>__('Prompt'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Leading text shown before a value is selected'),
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
            );
        }
    }
    FormField::addFieldTypes(/*@trans*/ 'Dynamic Fields', function() {
        return array(
            'department' => array(__('Department'), DepartmentField),
        );
    });
    
    
    class AssigneeField extends ChoiceField {
    
    Peter Rotich's avatar
    Peter Rotich committed
        var $_choices = array();
        var $_criteria = null;
    
    
        function getWidget() {
            $widget = parent::getWidget();
            if (is_object($widget->value))
                $widget->value = $widget->value->getId();
            return $widget;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getCriteria() {
    
            if (!isset($this->_criteria)) {
                $this->_criteria = array('available' => true);
                if (($c=parent::getCriteria()))
                    $this->_criteria = array_merge($this->_criteria, $c);
            }
    
            return $this->_criteria;
        }
    
    
        function hasIdValue() {
            return true;
        }
    
        function getChoices() {
            global $cfg;
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if (!$this->_choices) {
                $config = $this->getConfiguration();
                $choices = array(
                        __('Agents') => new ArrayObject(),
                        __('Teams') => new ArrayObject());
                $A = current($choices);
                $criteria = $this->getCriteria();
                $agents = array();
                if (($dept=$config['dept']) && $dept->assignMembersOnly()) {
                    if (($members = $dept->getMembers($criteria)))
                        foreach ($members as $member)
                            $agents[$member->getId()] = $member;
                } else {
                    $agents = Staff::getStaffMembers($criteria);
                }
    
    
                foreach ($agents as $id => $name)
    
    Peter Rotich's avatar
    Peter Rotich committed
                next($choices);
                $T = current($choices);
                if (($teams = Team::getTeams()))
                    foreach ($teams as $id => $name)
                        $T['t'.$id] = $name;
    
    Peter Rotich's avatar
    Peter Rotich committed
                $this->_choices = $choices;
            }
    
            return $this->_choices;
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getValue() {
    
            if (($value = parent::getValue()) && ($id=$this->getClean()))
               return $value[$id];
        }
    
    
    
        function parse($id) {
            return $this->to_php(null, $id);
        }
    
        function to_php($value, $id=false) {
            if (is_array($id)) {
                reset($id);
                $id = key($id);
            }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
            if ($id[0] == 's')
                return Staff::lookup(substr($id, 1));
            elseif ($id[0] == 't')
                return Team::lookup(substr($id, 1));
    
    
            return $id;
        }
    
    
        function to_database($value) {
            return (is_object($value))
                ? array($value->getName(), $value->getId())
                : $value;
        }
    
        function toString($value) {
            return (string) $value;
        }
    
        function searchable($value) {
            return null;
        }
    
        function getConfigurationOptions() {
            return array(
                'prompt' => new TextboxField(array(
                    'id'=>2, 'label'=>__('Prompt'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Leading text shown before a value is selected'),
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
            );
        }
    }
    FormField::addFieldTypes(/*@trans*/ 'Dynamic Fields', function() {
        return array(
            'assignee' => array(__('Assignee'), AssigneeField),
        );
    });
    
    
    
    class TicketStateField extends ChoiceField {
    
    
        static $_states = array(
    
                'open' => array(
    
                    'name' => /* @trans, @context "ticket state name" */ 'Open',
                    'verb' => /* @trans, @context "ticket state action" */ 'Open'
    
                    ),
                'closed' => array(
    
                    'name' => /* @trans, @context "ticket state name" */ 'Closed',
                    'verb' => /* @trans, @context "ticket state action" */ 'Close'
    
        // Private states
        static $_privatestates = array(
    
                'archived' => array(
    
                    'name' => /* @trans, @context "ticket state name" */ 'Archived',
                    'verb' => /* @trans, @context "ticket state action" */ 'Archive'
    
                    ),
                'deleted'  => array(
    
                    'name' => /* @trans, @context "ticket state name" */ 'Deleted',
                    'verb' => /* @trans, @context "ticket state action" */ 'Delete'
    
                );
    
        function hasIdValue() {
            return true;
        }
    
        function isChangeable() {
            return false;
        }
    
    
        function getChoices($verbose=false) {
    
            static $_choices;
    
            if (!isset($_choices)) {
                // Translate and cache the choices
    
                foreach (static::$_states as $k => $v)
    
                    $_choices[$k] =  _P('ticket state name', $v['name']);
    
                $this->ht['default'] =  '';
            }
    
            return $_choices;
        }
    
        function getChoice($state) {
    
            if ($state && is_array($state))
                $state = key($state);
    
            if (isset(static::$_states[$state]))
    
                return _P('ticket state name', static::$_states[$state]['name']);
    
    
            if (isset(static::$_privatestates[$state]))
    
                return _P('ticket state name', static::$_privatestates[$state]['name']);
    
            return $state;
    
        }
    
        function getConfigurationOptions() {
            return array(
                'prompt' => new TextboxField(array(
    
                    'id'=>2, 'label'=> __('Prompt'), 'required'=>false, 'default'=>'',
                    'hint'=> __('Leading text shown before a value is selected'),
    
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
            );
        }
    
    
        static function getVerb($state) {
    
            if (isset(static::$_states[$state]))
    
                return _P('ticket state action', static::$_states[$state]['verb']);
    
    
            if (isset(static::$_privatestates[$state]))
    
                return _P('ticket state action', static::$_privatestates[$state]['verb']);
    
    }
    FormField::addFieldTypes('Dynamic Fields', function() {
        return array(
            'state' => array('Ticket State', TicketStateField, false),
        );
    });
    
    class TicketFlagField extends ChoiceField {
    
        // Supported flags (TODO: move to configurable custom list)
        static $_flags = array(
                'onhold' => array(
                    'flag' => 1,
                    'name' => 'Onhold',
                    'states' => array('open'),
                    ),
                'overdue' => array(
                    'flag' => 2,
                    'name' => 'Overdue',
                    'states' => array('open'),
                    ),
                'answered' => array(
                    'flag' => 4,
                    'name' => 'Answered',
                    'states' => array('open'),
                    )
                );
    
        var $_choices;
    
        function hasIdValue() {
            return true;
        }
    
        function isChangeable() {
            return true;
        }
    
    
        function getChoices($verbose=false) {
    
            $this->ht['default'] =  '';
    
            if (!$this->_choices) {
                foreach (static::$_flags as $k => $v)
                    $this->_choices[$k] = $v['name'];
            }
    
            return $this->_choices;
        }
    
        function getConfigurationOptions() {
            return array(
                'prompt' => new TextboxField(array(
                    'id'=>2, 'label'=>'Prompt', 'required'=>false, 'default'=>'',
                    'hint'=>'Leading text shown before a value is selected',
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
            );
        }
    }
    
    FormField::addFieldTypes('Dynamic Fields', function() {
        return array(
            'flags' => array('Ticket Flags', TicketFlagField, false),
        );
    });
    
    
    class FileUploadField extends FormField {
        static $widget = 'FileUploadWidget';
    
        protected $attachments;
    
    
        static function getFileTypes() {
            static $filetypes;
    
    
            if (!isset($filetypes)) {
                if (function_exists('apc_fetch')) {
                    $key = md5(SECRET_SALT . GIT_VERSION . 'filetypes');
                    $filetypes = apc_fetch($key);
                }
                if (!$filetypes)
                    $filetypes = YamlDataParser::load(INCLUDE_DIR . '/config/filetype.yaml');
                if ($key)
                    apc_store($key, $filetypes, 7200);
            }
    
        function getConfigurationOptions() {
            // Compute size selections
    
            $sizes = array('262144' => '— '.__('Small').' —');
    
            $next = 512 << 10;
            $max = strtoupper(ini_get('upload_max_filesize'));
            $limit = (int) $max;
            if (!$limit) $limit = 2 << 20; # 2M default value
            elseif (strpos($max, 'K')) $limit <<= 10;
            elseif (strpos($max, 'M')) $limit <<= 20;
            elseif (strpos($max, 'G')) $limit <<= 30;
            while ($next <= $limit) {
                // Select the closest, larger value (in case the
                // current value is between two)
                $sizes[$next] = Format::file_size($next);
                $next *= 2;
            }
            // Add extra option if top-limit in php.ini doesn't fall
            // at a power of two
            if ($next < $limit * 2)
                $sizes[$limit] = Format::file_size($limit);
    
    
            $types = array();
    
            foreach (self::getFileTypes() as $type=>$info) {
    
                $types[$type] = $info['description'];
            }
    
    
            return array(
                'size' => new ChoiceField(array(
    
                    'label'=>__('Maximum File Size'),
                    'hint'=>__('Choose maximum size of a single file uploaded to this field'),
    
                    'default'=>$cfg->getMaxFileSize(),
    
                    'choices'=>$sizes
                )),
    
                'mimetypes' => new ChoiceField(array(
    
                    'label'=>__('Restrict by File Type'),
                    'hint'=>__('Optionally, choose acceptable file types.'),
    
                    'required'=>false,
                    'choices'=>$types,
    
                    'configuration'=>array('multiselect'=>true,'prompt'=>__('No restrictions'))
    
                'extensions' => new TextareaField(array(
    
                    'label'=>__('Additional File Type Filters'),
                    'hint'=>__('Optionally, enter comma-separated list of additional file types, by extension. (e.g .doc, .pdf).'),
    
                    'configuration'=>array('html'=>false, 'rows'=>2),
                )),
                'max' => new TextboxField(array(
    
                    'label'=>__('Maximum Files'),
                    'hint'=>__('Users cannot upload more than this many files.'),
    
                    'default'=>false,
                    'required'=>false,
                    'validator'=>'number',
    
                    'configuration'=>array('size'=>8, 'length'=>4, 'placeholder'=>__('No limit')),
    
        function hasSpecialSearch() {
            return false;
        }
    
    
        /**
         * Called from the ajax handler for async uploads via web clients.
         */
        function ajaxUpload($bypass=false) {
    
            $config = $this->getConfiguration();
    
            $files = AttachmentFile::format($_FILES['upload'],
                // For numeric fields assume configuration exists
    
                !is_numeric($this->get('id')));
    
            if (count($files) != 1)
                Http::response(400, 'Send one file at a time');
            $file = array_shift($files);
            $file['name'] = urldecode($file['name']);
    
    
            if (!$bypass && !$this->isValidFileType($file['name'], $file['type']))
    
                Http::response(415, 'File type is not allowed');
    
            $config = $this->getConfiguration();
            if (!$bypass && $file['size'] > $config['size'])
                Http::response(413, 'File is too large');
    
            if (!($F = AttachmentFile::upload($file)))
    
                Http::response(500, 'Unable to store file: '. $file['error']);
    
        /**
         * Called from FileUploadWidget::getValue() when manual upload is used
         * for browsers which do not support the HTML5 way of uploading async.
         */
        function uploadFile($file) {
    
            if (!$this->isValidFileType($file['name'], $file['type']))
    
                throw new FileUploadError(__('File type is not allowed'));
    
            $config = $this->getConfiguration();
            if ($file['size'] > $config['size'])
                throw new FileUploadError(__('File size is too large'));
    
            return AttachmentFile::upload($file);
        }
    
        /**
         * Called from API and email routines and such to handle attachments
         * sent other than via web upload
         */
        function uploadAttachment(&$file) {
    
            if (!$this->isValidFileType($file['name'], $file['type']))
    
                throw new FileUploadError(__('File type is not allowed'));
    
            if (is_callable($file['data']))
                $file['data'] = $file['data']();
            if (!isset($file['size'])) {
                // bootstrap.php include a compat version of mb_strlen
                if (extension_loaded('mbstring'))
                    $file['size'] = mb_strlen($file['data'], '8bit');
                else
                    $file['size'] = strlen($file['data']);
            }
    
            $config = $this->getConfiguration();
            if ($file['size'] > $config['size'])
                throw new FileUploadError(__('File size is too large'));
    
    
            if (!$F = AttachmentFile::create($file))
    
                throw new FileUploadError(__('Unable to save file'));
    
    
        }
    
        function isValidFileType($name, $type=false) {
            $config = $this->getConfiguration();
    
    
            // Check MIME type - file ext. shouldn't be solely trusted.
            if ($type && $config['__mimetypes']
                    && in_array($type, $config['__mimetypes']))
    
            // Return true if all file types are allowed (.*)
    
            if (!$config['__extensions'] || in_array('.*', $config['__extensions']))
    
            $allowed = $config['__extensions'];
    
            $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
    
            return ($ext && is_array($allowed) && in_array(".$ext", $allowed));
        }
    
    
        function getFiles() {
            if (!isset($this->attachments) && ($a = $this->getAnswer())
                && ($e = $a->getEntry()) && ($e->get('id'))
            ) {
    
                $this->attachments = GenericAttachments::forIdAndType(
    
                    // Combine the field and entry ids to make the key
                    sprintf('%u', crc32('E'.$this->get('id').$e->get('id'))),
                    'E');
            }
    
            return $this->attachments ?: array();
    
        function setAttachments(GenericAttachments $att) {
            $this->attachments = $att;
        }
    
    
        function getConfiguration() {
            $config = parent::getConfiguration();
    
            $_types = self::getFileTypes();
    
            $mimetypes = array();
            $extensions = array();
            if (isset($config['mimetypes']) && is_array($config['mimetypes'])) {
                foreach ($config['mimetypes'] as $type=>$desc) {
                    foreach ($_types[$type]['types'] as $mime=>$exts) {
                        $mimetypes[$mime] = true;
    
                        if (is_array($exts))
                            foreach ($exts as $ext)
                                $extensions['.'.$ext] = true;
    
                    }
                }
            }
            if (strpos($config['extensions'], '.*') !== false)
                $config['extensions'] = '';
    
    
            if (is_string($config['extensions'])) {
                foreach (preg_split('/\s+/', str_replace(',',' ', $config['extensions'])) as $ext) {
                    if (!$ext) {
                        continue;
                    }
                    elseif (strpos($ext, '/')) {
    
                        $mimetypes[$ext] = true;
    
                    }
                    else {
                        if ($ext[0] != '.')
                            $ext = '.' . $ext;
    
    
                        // Ensure that the extension is lower-cased for comparison latr
                        $ext = strtolower($ext);
    
    
                        // Add this to the MIME types list so it can be exported to
                        // the @accept attribute
                        if (!isset($extensions[$ext]))
                            $mimetypes[$ext] = true;
    
                        $extensions[$ext] = true;
                    }
    
                $config['__extensions'] = array_keys($extensions);
            }
            elseif (is_array($config['extensions'])) {
                $config['__extensions'] = $config['extensions'];
    
            }
    
            // 'mimetypes' is the array represented from the user interface,
            // '__mimetypes' is a complete list of supported MIME types.
            $config['__mimetypes'] = array_keys($mimetypes);
            return $config;
        }
    
    
        // When the field is saved to database, encode the ID listing as a json
        // array. Then, inspect the difference between the files actually
        // attached to this field
        function to_database($value) {
            $this->getFiles();
            if (isset($this->attachments)) {
    
                $this->attachments->keepOnlyFileIds($value);
    
            }
            return JsonDataEncoder::encode($value);
        }
    
        function parse($value) {
            // Values in the database should be integer file-ids
            return array_map(function($e) { return (int) $e; },
                $value ?: array());
        }
    
        function to_php($value) {
            return JsonDataParser::decode($value);
        }
    
    
        function to_config($value) {
    
            if ($value && is_array($value))
                $value = array_values($value);
    
            return $value;
        }
    
    
        function display($value) {
            $links = array();
            foreach ($this->getFiles() as $f) {
    
                $links[] = sprintf('<a class="no-pjax" href="%s">%s</a>',
    
                    Format::htmlchars($f->file->getDownloadUrl()),
                    Format::htmlchars($f->file->name));
    
            }
            return implode('<br/>', $links);
        }
    
    
        function toString($value) {
            $files = array();
            foreach ($this->getFiles() as $f) {
    
                $files[] = $f->file->name;
    
            }
            return implode(', ', $files);
        }
    
        function db_cleanup($field=false) {
    
            // Delete associated attachments from the database, if any
            $this->getFiles();
            if (isset($this->attachments)) {
                $this->attachments->deleteAll();
            }
        }
    
    
        function asVar($value, $id=false) {
            return new FileFieldAttachments($this->getFiles());
        }
        function asVarType() {
            return 'FileFieldAttachments';
        }
    }
    
    class FileFieldAttachments {
        var $files;
    
        function __construct($files) {
            $this->files = $files;
        }
    
        function __toString() {
            $files = array();
            foreach ($this->files as $f) {
                $files[] = $f->file->name;
            }
            return implode(', ', $files);
        }
    
        function getVar($tag) {
            switch ($tag) {
    
            case 'names':
                return $this->__toString();
    
            case 'files':
                throw new OOBContent(OOBContent::FILES, $this->files->all());
            }
        }
    
        static function getVarScope() {
            return array(
    
                'names' => __('List of file names'),
    
                'files' => __('Attached files'),
            );
        }
    
    class InlineFormData extends ArrayObject {
        var $_form;
    
        function __construct($form, array $data=array()) {
            parent::__construct($data);
            $this->_form = $form;
        }
    
        function getVar($tag) {
            foreach ($this->_form->getFields() as $f) {
                if ($f->get('name') == $tag)
                    return $this[$f->get('id')];
            }
        }
    }
    
    
    class InlineFormField extends FormField {
        static $widget = 'InlineFormWidget';
    
        var $_iform = null;
    
        function validateEntry($value) {
            if (!$this->getInlineForm()->isValid()) {
    
                $this->_errors[] = __('Correct errors in the inline form');
    
            }
        }
    
        function parse($value) {
            // The InlineFieldWidget returns an array of cleaned data
            return $value;
        }
    
        function to_database($value) {
            return JsonDataEncoder::encode($value);
        }
    
        function to_php($value) {
            $data = JsonDataParser::decode($value);
            // The InlineFormData helps with the variable replacer API
            return new InlineFormData($this->getInlineForm(), $data);
        }
    
        function display($data) {
            $form = $this->getInlineForm();
            ob_start(); ?>
            <div><?php
            foreach ($form->getFields() as $field) { ?>
                <span style="display:inline-block;padding:0 5px;vertical-align:top">
                    <strong><?php echo Format::htmlchars($field->get('label')); ?></strong>
                    <div><?php
                        $value = $data[$field->get('id')];
                        echo $field->display($value); ?></div>
                </span><?php
            } ?>
            </div><?php
            return ob_get_clean();
        }
    
    
        function getInlineForm($data=false) {
    
            $form = $this->get('form');
            if (is_array($form)) {
    
                $form = new SimpleForm($form, $data ?: $this->value ?: $this->getSource());
    
            }
            return $form;
        }
    }
    
    class InlineDynamicFormField extends FormField {
        function getInlineForm($data=false) {
            if (!isset($this->_iform) || $data) {
                $config = $this->getConfiguration();
                $this->_iform = DynamicForm::lookup($config['form']);
                if ($data)
                    $this->_iform = $this->_iform->getForm($data);
            }
            return $this->_iform;
        }
    
        function getConfigurationOptions() {
            $forms = DynamicForm::objects()->filter(array('type'=>'G'))
                ->values_flat('id', 'title');
            $choices = array();
            foreach ($forms as $row) {
                list($id, $title) = $row;
                $choices[$id] = $title;
            }
            return array(
                'form' => new ChoiceField(array(
                    'id'=>2, 'label'=>'Inline Form', 'required'=>true,
                    'default'=>'', 'choices'=>$choices
                )),
            );
        }
    }
    
    class InlineFormWidget extends Widget {
        function render($mode=false) {
            $form = $this->field->getInlineForm();
            if (!$form)
                return;
            // Handle first-step edits -- load data from $this->value
            if ($form instanceof DynamicForm && !$form->getSource())
                $form = $form->getForm($this->value);
            $inc = ($mode == 'client') ? CLIENTINC_DIR : STAFFINC_DIR;
            include $inc . 'templates/inline-form.tmpl.php';
        }
    
        function getValue() {
            $data = $this->field->getSource();
            if (!$data)
                return null;
            $form = $this->field->getInlineForm($data);
            if (!$form)
                return null;
            return $form->getClean();
        }
    }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class Widget {
    
        static $media = null;
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function __construct($field) {
            $this->field = $field;
            $this->name = $field->getFormName();
    
            $this->id = '_' . $this->name;
    
        }
    
        function parseValue() {
    
            $this->value = $this->getValue();
    
            if (!isset($this->value) && is_object($this->field->getAnswer()))
                $this->value = $this->field->getAnswer()->getValue();
    
            if (!isset($this->value) && isset($this->field->value))
    
                $this->value = $this->field->value;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getValue() {
    
            $data = $this->field->getSource();
    
            // Search for HTML form name first
            if (isset($data[$this->name]))
                return $data[$this->name];
            elseif (isset($data[$this->field->get('name')]))
                return $data[$this->field->get('name')];
    
            elseif (isset($data[$this->field->get('id')]))
                return $data[$this->field->get('id')];
    
            return null;
    
    
        /**
         * getJsValueGetter
         *
         * Used with the dependent fields feature, this function should return a
         * single javascript expression which can be used in a larger expression
         * (<> == true, where <> is the result of this function). The %s token
         * will be replaced with a jQuery variable representing this widget.
         */
        function getJsValueGetter() {
            return '%s.val()';
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class TextboxWidget extends Widget {
    
    Jared Hancock's avatar
    Jared Hancock committed
        static $input_type = 'text';
    
    
        function render($options=array(), $extraConfig=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            if (is_array($extraConfig)) {
                foreach ($extraConfig as $k=>$v)
                    if (!isset($config[$k]) || !$config[$k])
                        $config[$k] = $v;
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (isset($config['size']))
                $size = "size=\"{$config['size']}\"";
    
            if (isset($config['length']) && $config['length'])
    
    Jared Hancock's avatar
    Jared Hancock committed
                $maxlength = "maxlength=\"{$config['length']}\"";
    
            if (isset($config['classes']))
    
                $classes = 'class="'.$config['classes'].'"';
    
            if (isset($config['autocomplete']))
                $autocomplete = 'autocomplete="'.($config['autocomplete']?'on':'off').'"';
    
            if (isset($config['autofocus']))
                $autofocus = 'autofocus';
    
            if (isset($config['disabled']))
                $disabled = 'disabled="disabled"';
    
            if (isset($config['translatable']) && $config['translatable'])
                $translatable = 'data-translate-tag="'.$config['translatable'].'"';
    
            $type = static::$input_type;
            $types = array(
                'email' => 'email',
                'phone' => 'tel',
            );
            if ($type == 'text' && isset($types[$config['validator']]))
                $type = $types[$config['validator']];
    
            $placeholder = sprintf('placeholder="%s"', $this->field->getLocal('placeholder',
                $config['placeholder']));
    
            <input type="<?php echo $type; ?>"
    
                id="<?php echo $this->id; ?>"
    
                <?php echo implode(' ', array_filter(array(
    
                    $size, $maxlength, $classes, $autocomplete, $disabled,
    
                    $translatable, $placeholder, $autofocus))); ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
                name="<?php echo $this->name; ?>"
                value="<?php echo Format::htmlchars($this->value); ?>"/>
            <?php
        }
    }
    
    
    
    class TextboxSelectionWidget extends TextboxWidget {
        //TODO: Support multi-input e.g comma separated inputs
        function render($options=array()) {
    
            if ($this->value && is_array($this->value))
                $this->value = current($this->value);
    
            parent::render($options);
        }
    
        function getValue() {
    
            $value = parent::getValue();
    
            if ($value && ($item=$this->field->lookupChoice((string) $value)))
                $value = $item;