diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 01682d00cf20c9f71976f7821a73076a98881f60..580e631eb02b82a6e729c9edab0207e4d8b14dec 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -36,6 +36,7 @@ class DynamicForm extends VerySimpleModel { static $types = array( 'T' => 'Ticket Information', 'U' => 'User Information', + 'O' => 'Organization Information', ); var $_form; @@ -562,11 +563,21 @@ class DynamicFormEntry extends VerySimpleModel { return DynamicFormEntry::objects() ->filter(array('object_id'=>$user_id, 'object_type'=>'U')); } + function setClientId($user_id) { $this->object_type = 'U'; $this->object_id = $user_id; } + function setObjectId($object_id) { + $this->object_id = $object_id; + } + + function forOrganization($org_id) { + return DynamicFormEntry::objects() + ->filter(array('object_id'=>$org_id, 'object_type'=>'O')); + } + function render($staff=true, $title=false) { return $this->getForm()->render($staff, $title); } diff --git a/include/class.organization.php b/include/class.organization.php index ab70f8fb3ec0fc81e621fedf3c3bd828b06fcad8..7cb66ce9f61e7fd8645765f164e866da1292b344 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -39,6 +39,8 @@ class OrganizationModel extends VerySimpleModel { } class Organization extends OrganizationModel { + var $_entries; + var $_forms; function __construct($ht) { parent::__construct($ht); @@ -57,6 +59,53 @@ class Organization extends OrganizationModel { return $this->created; } + function addDynamicData($data) { + + $of = OrganizationForm::getInstance($this->id); + foreach ($of->getFields() as $f) + if (isset($data[$f->get('name')])) + $of->setAnswer($f->get('name'), $data[$f->get('name')]); + + $of->save(); + + return $of; + } + + function getDynamicData() { + if (!isset($this->_entries)) { + $this->_entries = DynamicFormEntry::forOrganization($this->id)->all(); + if (!$this->_entries) { + $g = OrganizationForm::getInstance($this->id); + $g->save(); + $this->_entries[] = $g; + } + } + + return $this->_entries; + } + + function getForms($data=null) { + + if (!isset($this->_forms)) { + $this->_forms = array(); + foreach ($this->getDynamicData() as $cd) { + $cd->addMissingFields(); + if(!$data + && ($form = $cd->getForm()) + && $form->get('type') == 'O' ) { + foreach ($cd->getFields() as $f) { + if ($f->get('name') == 'name') + $f->value = $this->getName(); + } + } + + $this->_forms[] = $cd->getForm(); + } + } + + return $this->_forms; + } + function to_json() { $info = array( @@ -111,6 +160,59 @@ class Organization extends OrganizationModel { } } + +class OragnizationForm extends DynamicForm { + static $instance; + static $form; + + static function objects() { + $os = parent::objects(); + return $os->filter(array('type'=>'O')); + } + + static function getDefaultForm() { + if (!isset(static::$form)) { + if ($o = static::objects()) + static::$form = $o[0]; + else //TODO: Remove the code below and move it to task?? + static::$form = self::__loadDefaultForm(); + } + + return static::$form; + } + + static function getInstance($object_id=0) { + if (!isset(static::$instance)) + static::$instance = static::getDefaultForm()->instanciate(); + + static::$instance->object_type = 'O'; + + if ($object_id) + static::$instance->object_id = $object_id; + + return static::$instance; + } + + static function __loadDefaultForm() { + require_once(INCLUDE_DIR.'class.i18n.php'); + + $i18n = new Internationalization(); + $tpl = $i18n->getTemplate('form.yaml'); + foreach ($tpl->getData() as $f) { + if ($f['type'] == 'O') { + $form = DynamicForm::create($f); + $form->save(); + break; + } + } + + $o =static::objects(); + + return $o[0]; + } + +} + //Organization::_inspect(); ?> diff --git a/include/i18n/en_US/form.yaml b/include/i18n/en_US/form.yaml index 244374279f619144b466304f2d2641810f454564..a5030c0333383fc972837aabe8c48add5550b775 100644 --- a/include/i18n/en_US/form.yaml +++ b/include/i18n/en_US/form.yaml @@ -146,3 +146,26 @@ html: false maxlength: 100 +- type: O # notrans + title: Organization Information + instructions: Details on user organization + deletable: false + fields: + - type: text # notrans + name: name # notrans + label: Name + required: true + sort: 1 + edit_mask: 15 + configuration: + size: 40 + length: 64 + + - type: memo # notrans + name: notes + label: Internal Notes + required: false + sort: 2 + configuration: + rows: 4 + cols: 40