Esempio n. 1
0
File: Server.php Progetto: arhe/pwak
 /**
  * AjaxServer::getCollectionForSelect()
  *
  * @param  string $entity
  * @param  array  $filter
  * @access public
  * @return string json encoded array
  */
 public function getCollectionForSelect($entity, $filter = array())
 {
     $return = array();
     $array = SearchTools::createArrayIDFromCollection($entity, $filter);
     foreach ($array as $id => $toString) {
         $return[] = array('id' => $id, 'toString' => utf8_encode($toString));
     }
     return json_encode($return);
 }
Esempio n. 2
0
 /**
  * Retourne un objet HTML_QuickForm_Element construit avec les paramètres
  * passés.
  *
  * @access private
  * @param string $name le nom de l'attribut
  * @param string $ename le nom de l'élément de formulaire correspondant
  * @param string $type le type de l'attribut
  * @param string $label le label de l'élément de formulaire
  * @param array $opts un tableau d'options pour l'élément de formulaire
  * @param bool $req determine si l'élément est requis
  * @param bool $ipe determine si l'élément fkey doit être édité dans le form
  * @param bool $aeButton determine si l'élément fkey doit utiliser un bouton addedit
  * @return object HTML_QuickForm_Element
  */
 private function _createElement($name, $ename, $type, $label, $opts, $req, $ipe, $aeButton)
 {
     $ret = array();
     $opts['id'] = $ename;
     $labelWithSemiColon = $label . ': ';
     if ($type == Object::TYPE_FKEY) {
         // type fkey: on construit un select
         $objPropertiesArray = $this->object->getProperties();
         $className = $objPropertiesArray[$name];
         require_once MODELS_DIR . '/' . $className . '.php';
         $tmpobject = new $className();
         $getter = 'get' . $name . 'Id';
         $objID = $this->object->{$getter}();
         // denormalized forms
         if ($ipe) {
             $customFileName = CUSTOM_CONTROLLER_DIR . '/' . $className . 'AddEdit.php';
             if (file_exists(PROJECT_ROOT . '/' . LIB_DIR . '/' . $customFileName)) {
                 require_once $customFileName;
                 $class = $className . 'AddEdit';
             } else {
                 $class = 'GenericAddEdit';
             }
             $this->innerForm = new $class(array('clsname' => $className, 'id' => $objID, 'return_url' => $this->guessReturnURL(), 'profiles' => $this->profiles));
             $this->innerForm->parentForm = $this;
             $this->innerForm->initialize();
             $this->innerForm->render();
             $mapping = array_keys($tmpobject->getMapping());
             $this->form->addElement('hidden', $ename . '_ID', $objID);
             $ret[] = HTML_QuickForm::createElement('header', $ename, $label);
             foreach ($this->innerForm->formDefaults as $k => $v) {
                 $elt = $this->innerForm->form->getElement($k . '_Group');
                 if ($elt instanceof HTML_QuickForm_Element) {
                     $ret[] = $elt;
                 }
                 $elt = $this->innerForm->form->getElement($k);
                 if ($elt instanceof HTML_QuickForm_Element) {
                     $ret[] = $elt;
                 }
                 $this->formDefaults[$k] = $v;
             }
         } else {
             $toStringAttribute = call_user_func(array($tmpobject, 'getToStringAttribute'));
             $sortOrderField = is_array($toStringAttribute) ? $toStringAttribute[0] : $toStringAttribute;
             $fGetter = 'getFilterFor' . $name;
             $filter = method_exists($this, $fGetter) ? $this->{$fGetter}() : array();
             $arr = SearchTools::createArrayIDFromCollection($className, $filter, $req ? '' : MSG_SELECT_AN_ELEMENT, 'toString', array($sortOrderField => SORT_ASC));
             $ename .= '_ID';
             $opts['id'] = $ename;
             if ($aeButton && $this->action != self::FEATURE_VIEW) {
                 $opts['class'] = 'select_with_add_button';
                 $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts);
                 $retURL = urlencode(sprintf('%s?action=%s&entity=%s&objID=%d', $_SERVER['PHP_SELF'], $this->action, $this->altname, $this->objID));
                 $url = sprintf('dispatcher.php?entity=%s&action=add&retURL=%s&fromEntity=%s', $className, $retURL, $this->clsname . ':' . $name);
                 $ret[] = HTML_QuickForm::createElement('button', 'addButton' . $name, A_ADD, 'id="addButton' . $name . '" class="button" onclick="' . 'this.form.redirectURL.value=\'' . $url . '\';' . 'this.form.fromAddButton.value=1;this.form.submit();"');
             } else {
                 $opts['class'] = 'select';
                 $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts);
             }
         }
     } else {
         if ($type == Object::TYPE_CONST) {
             // type constante: on construit un select aussi
             $method = sprintf('get%sConstArray', $name);
             $arr = call_user_func(array($this->clsname, $method));
             $opts['class'] = 'select';
             $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts);
         } else {
             if (in_array($type, array(Object::TYPE_DATE, Object::TYPE_DATETIME))) {
                 // type date ou datetime: un calendrier
                 require_once 'HTML/QuickForm/jscalendar.php';
                 $options = array('baseURL' => 'js/jscalendar/', 'styleCss' => JSCALENDAR_DEFAULT_CSS, 'language' => I18N::getLocaleCode(true), 'setup' => array('inputField' => $ename, 'button' => $ename . '_calendar_trigger', 'displayArea' => 'displayed' . $ename, 'ifFormat' => '%Y-%m-%d', 'daFormat' => '\'+Calendar._TT["DEF_DATE_FORMAT"]+\'' . ($type == Object::TYPE_DATETIME ? ' %H:%M' : ''), 'showsTime' => $type == Object::TYPE_DATETIME ? true : false, 'showOthers' => true));
                 $attributes = array('readonly' => 'readonly', 'class' => 'ReadOnlyField', 'rows' => 1, 'style' => 'width:30%;height:14px;', 'id' => 'displayed' . $ename);
                 $ret[] = HTML_QuickForm::createElement('hidden', $ename, null, array('id' => $ename));
                 // vu que jscalendar utilise innerhtml, on ne peut mettre ici un input text
                 // Du coup, ce 'bidouillage' avec un textarea
                 $ret[] = HTML_QuickForm::createElement('textarea', 'displayed' . $ename, null, $attributes);
                 /*$ret[] = HTML_QuickForm::createElement('static', 'displayed'.$ename, null,
                   '<span id="displayed'. $ename . '" name="displayed'. $ename . '" style="border: 1px dotted rgb(0, 0, 0); margin: 3px; padding: 3px; background-color: rgb(225, 232, 239); text-align: right;">toto</span>' );*/
                 $ret[] = HTML_QuickForm::createElement('button', $ename . '_calendar_trigger', '...', array('title' => _('Select a date'), 'id' => $ename . '_calendar_trigger', 'class' => 'button'));
                 $ret[] = HTML_QuickForm::createElement('jscalendar', $ename . '_calendar', null, $options);
             } else {
                 if ($type == Object::TYPE_TIME) {
                     $fmt = array('language' => I18N::getLocaleCode(true), 'format' => 'H:i');
                     $opts['class'] = 'datetime';
                     $ret[] = HTML_QuickForm::createElement('date', $ename, $labelWithSemiColon, $fmt, $opts);
                 } else {
                     if (in_array($type, array(Object::TYPE_TEXT, Object::TYPE_I18N_TEXT, Object::TYPE_LONGTEXT))) {
                         // type texte ou longtext: un textarea
                         $opts['rows'] = 10;
                         $opts['cols'] = 40;
                         $opts['class'] = 'textarea';
                         $ret[] = HTML_QuickForm::createElement('textarea', $ename, $labelWithSemiColon, $opts);
                     } else {
                         if ($type == Object::TYPE_BOOL) {
                             $opts['id'] = $ename . '_0';
                             $ret[] = HTML_QuickForm::createElement('radio', $ename, $labelWithSemiColon, A_YES, 1, $opts);
                             $opts['id'] = $ename . '_1';
                             $ret[] = HTML_QuickForm::createElement('radio', $ename, $labelWithSemiColon, A_NO, 0, $opts);
                         } else {
                             if ($type == Object::TYPE_PASSWORD) {
                                 $opts['value'] = "this.value='" . self::PW_NOCHG . "';";
                                 $ret[] = HTML_QuickForm::createElement('password', $ename, $labelWithSemiColon, $opts);
                                 $opts['id'] = $ename . '_Again';
                                 $label = $label . ' (' . _('confirm') . ')';
                                 $ret[] = HTML_QuickForm::createElement('password', $opts['id'], $labelWithSemiColon, $opts);
                             } elseif ($type == Object::TYPE_MANYTOMANY) {
                                 require_once 'HTML/QuickForm/advmultiselect.php';
                                 // type many2many: on construit deux selects avec advmultiselect
                                 $objPropertiesArray = $this->object->getProperties();
                                 $className = $this->links[$name]['linkClass'];
                                 //objPropertiesArray[$name];
                                 require_once MODELS_DIR . '/' . $className . '.php';
                                 $tmpobject = new $className();
                                 $toStringAttribute = call_user_func(array($tmpobject, 'getToStringAttribute'));
                                 $sortOrderField = is_array($toStringAttribute) ? $toStringAttribute[0] : $toStringAttribute;
                                 $toString = is_array($toStringAttribute) ? 'toString' : $toStringAttribute;
                                 $filterGetter = 'getFilterFor' . $name;
                                 if (method_exists($this, $filterGetter)) {
                                     $filter = $this->{$filterGetter}();
                                 } else {
                                     $filter = array();
                                 }
                                 $arr = SearchTools::createArrayIDFromCollection($className, $filter, '', $toString, array($sortOrderField => SORT_ASC));
                                 $opts['size'] = 8;
                                 $opts['style'] = 'width:100%;';
                                 $ret[] = HTML_QuickForm::createElement('advmultiselect', $ename, array($label, $label, ''), $arr, $opts);
                             } elseif (in_array($type, array(Object::TYPE_FILE, Object::TYPE_IMAGE, Object::TYPE_FILE_UPLOAD))) {
                                 $opts['class'] = 'textfield';
                                 $ret[] = HTML_QuickForm::createElement('file', $ename, $labelWithSemiColon, $opts);
                             } elseif ($type == Object::TYPE_HTML || $type == Object::TYPE_I18N_HTML) {
                                 // type html: un editeur wysiwyg
                                 // lib: HTML_QuickForm_Element_tinymce
                                 require_once 'HTML/QuickForm/tinymce.php';
                                 $options = array('baseURL' => 'js/tinymce/jscripts/tiny_mce/', 'configFile' => 'js/tinymce.inc.js');
                                 $opts['class'] = 'mceEditor';
                                 $ret[] = HTML_QuickForm::createElement('tinymce', $ename, $labelWithSemiColon, $options, $opts);
                             } elseif ($type == Object::TYPE_HEXACOLOR) {
                                 // type hexacolor: un colorpicker
                                 $this->addJSRequirements('js/colorpicker.js');
                                 require_once 'HTML/QuickForm/colorpicker.php';
                                 $ret[] = HTML_QuickForm::createElement('colorpicker', $ename, $labelWithSemiColon, array(), $opts);
                             } else {
                                 // les autres types sont des textfield
                                 $opts['class'] = 'textfield';
                                 $ret[] = HTML_QuickForm::createElement('text', $ename, $labelWithSemiColon, $opts);
                             }
                         }
                     }
                 }
             }
         }
     }
     if (!$ipe) {
         foreach ($ret as $elt) {
             if ($req) {
                 $elt->setAttribute('class', $elt->getAttribute('class') . ' required_element');
             }
         }
     }
     return $ret;
 }