form.php

Go to the documentation of this file.
00001 <?php
00035 //include_once("include/classes/fileuploadform.php");
00036 
00037 class Form {
00038     private $id;                        
00039     private $method;                    
00040     private $action;                    
00041     private $dataform;                  
00042     private $groups = NULL;             
00043     private $readperm;                  
00044     private $writeperm;                 
00045     private $groupreadperm;             
00046     private $groupwriteperm;            
00047     private $form = array();            
00048     private $permvisible = FALSE;       
00049     private $submit = LANG_CONFIRM;     
00050     private $cancelvisible = FALSE;     
00062     function __construct($id, $method, $action = "", $dataform = FALSE) {
00063         global $cfg;
00064 
00065         $this->id = $id;
00066         $this->method = $method;
00067         if (!$action) { $action = $cfg->getConfig("DEFAULT_PAGE"); }
00068         $this->action = $action;
00069         $this->dataform = $dataform;
00070     }
00071 
00077     private function getFormHeader() {
00078         if ($this->dataform) { $enctype = " enctype=\"multipart/form-data\""; }
00079         if ($this->permvisible) {
00080             $javascript =   "<script type=\"text/javascript\">\n//<![CDATA[\n" .
00081                             "function showChkPermsGroup(element, group) {\n" .
00082                             "document.getElementById(group).style.display=((document.getElementById(element).value == 2) ? '' : 'none');\n" .
00083                             "}\n" .
00084                             "//]]>\n</script>\n";
00085         }
00086         return  $javascript . "<form class=\"cmskeyform\" id=\"" . $this->_id .
00087                 "\" method=\"" . $this->method .
00088                 "\"" . $enctype . " action=\"" . $this->action . "\">\n";
00089     }
00090 
00098     private function getFCKEditor($name, $defaulttext = "") {
00099 //      global $lang, $cfg;
00100 //
00101 //      $editor = new FCKeditor($name);
00102 //      $editor->BasePath = "include/fckeditor/";
00103 //      //$editor->DefaultLanguage = $lang->getCurrentLanguage();
00104 //      if (file_exists($cfg->getConfig("DOMAIN_CFG_DIR") . "config/fckeditor/custom.js"))  {
00105 //          $editor->Config['CustomConfigurationsPath'] = "/config/fckeditor/custom.js";
00106 //          $editor->ToolbarSet = "CMSkey";
00107 //      } else {
00108 //          $editor->ToolbarSet = "Basic";
00109 //      }
00110 //      //$editor->AutoDetectLanguage = FALSE;
00111 //      //$editor->DefaultLanguage = "it";
00112 //      $editor->Value = $defaulttext;
00113 //      return $editor->CreateHtml() ;
00114     }
00115 
00124     private function getRenderedElement($label, $element, $tip) {
00125         if ($label) { $item = "<div class=\"formitemcaption\">" . $label . "</div>\n"; }
00126         if ($element) { $item .= "<div class=\"formiteminput\">" . $element . "</div>\n"; }
00127         if ($tip) { $item .= "<div class=\"formitemtip\">" . $tip . "</div>\n"; }
00128         if ($item) { $item = "<div class=\"formitem\">\n" . $item . "</div><!-- formitem -->\n"; }
00129         return $item;
00130     }
00131 
00137     private function getCancelButton() {
00138         if ($this->cancelvisible) {
00139             $qs = Content::getDecodedString($_GET['lp']);
00140             if ($qs) { $qs = "?" . $qs; }
00141             return  "<input type=\"button\" name=\"cancel\" value=\"&nbsp;" . LANG_CANCEL .
00142                     "&nbsp;\" onclick=\"document.location='" . $cfg->getConfig("DEFAULT_PAGE") . $qs . "';\" />\n";
00143         }
00144     }
00145 
00156     public function addElement($type, $name, $label, $value, $attrs, $tips = "") {
00157         $item = array();
00158         $item['type'] = $type;
00159         $item['label'] = $label;
00160         $item['value'] = $value;
00161         $item['default'] = "";
00162         $item['attrs'] = "";
00163         if ($attrs) { $item['attrs'] = " " . $attrs; }
00164         $item['tips'] = $tips;
00165         $this->form[$name] = $item;
00166     }
00167 
00174     public function addHidden($name, $value) {
00175         $this->addElement("hidden", $name, "", $value, "");
00176     }
00177 
00183     public function addHTML($html) {
00184         $this->addElement("html", "", "", $html, "");
00185     }
00186 
00192     public function setSubmitValue($value) {
00193         $this->submit = $value;
00194     }
00195 
00199     public function showPermControls() {
00200         $this->permvisible = TRUE;
00201     }
00202 
00206     public function showCancel() {
00207         $this->cancelvisible = TRUE;
00208     }
00209 
00216     public function setValue($name, $value) {
00217         $this->form[$name]['value'] = $value;
00218     }
00219 
00229     function addFileUploadBox($fieldname, $fieldcaption, $fieldtips, $filecode = "") {
00230         //$this->addElement("file", $fieldname, $fieldcaption, "", "", $fieldtips);
00231         //$this->addElement("text", "filedescription", LANG_FILEDESCRIPTION, "", "", "");
00232         //$this->addElement("checkbox", "filevisible", LANG_FILEVISIBLE, "", "", "");
00233     }
00234 
00241     private function setContentPartForm($tags, $values = NULL) {
00242 //      // FIXME nel seguito, v[0] contiene id e v[1] array di valori
00243 //      if (is_array($values)) { $v = each($values); }
00244 //
00245 //      for ($i = 0; $i < count($tags); $i++) {
00246 //          if ($tags[$i]['field_caption']) { $tags[$i]['field_caption'] = constant("LANG_FIELDCAPTION_" . $tags[$i]['field_caption']); }
00247 //          if ($tags[$i]['field_edit_tips']) { $tags[$i]['field_edit_tips'] = constant("LANG_FIELDTIP_" . $tags[$i]['field_edit_tips']); }
00248 //
00249 //          // FIXME per adesso, solo text e fckeditor
00250 //          switch ($tags[$i]['field_type']) {
00251 //              case FIELDTYPE_SHORTTEXT:
00252 //              case FIELDTYPE_INT:
00253 //              case FIELDTYPE_UINT:
00254 //              case FIELDTYPE_FLOAT:
00255 //                  $this->addElement("text",
00256 //                                    $tags[$i]['field_name'],
00257 //                                    $tags[$i]['field_caption'],
00258 //                                    $v[1]['cnt'][$tags[$i]['field_name']],
00259 //                                    "",
00260 //                                    $tags[$i]['field_edit_tips']);
00261 //                  break;
00262 //
00263 //              case FIELDTYPE_FMTLONGTEXT:
00264 //                  if ($_GET['f'] == "nofck") {
00265 //                      $this->addElement("textarea",
00266 //                                        $tags[$i]['field_name'],
00267 //                                        $tags[$i]['field_caption'],
00268 //                                        $v[1]['cnt'][$tags[$i]['field_name']],
00269 //                                        "rows=\"20\"",
00270 //                                        $tags[$i]['field_edit_tips']);
00271 //                  } else {
00272 //                      $this->addElement("html",
00273 //                                      $tags[$i]['field_name'],
00274 //                                      $tags[$i]['field_caption'],
00275 //                                      $this->_getFCKEditor($tags[$i]['field_name'],
00276 //                                      $v[1]['cnt'][$tags[$i]['field_name']]),
00277 //                                      "",
00278 //                                      $tags[$i]['field_edit_tips']);
00279 //                  }
00280 //                  break;
00281 //
00282 //              case FIELDTYPE_IMAGE:
00283 //                  $this->dataform = TRUE;
00284 //                  // FIXME nella successiva trasmettere l'eventuale codice del file da sostituire
00285 //                  $this->addElement("fileuploadbox", $tags[$i]['field_name'], $tags[$i]['field_caption'], "", $tags[$i]['field_edit_tips']);
00286 //                  break;
00287 //
00288 //              default:
00289 //          }
00290 //      }
00291     }
00292 
00298     private function setPropertiesPartForm($id = 0) {
00299     }
00300 
00308     private function getGroupsCheckBoxes($permtype, $selgroups) {
00309 //      global $cfg;
00310 //
00311 //      if (is_null($this->groups)) {
00312 //          $this->groups = array();
00313 //          $sql = "SELECT id, name FROM groups WHERE id_area = " . $cfg->getAreaId() . " ORDER BY name";
00314 //          $res = mysql_query($sql);
00315 //          while ($line = mysql_fetch_assoc($res)) {
00316 //              $this->groups[count($this->groups)] = $line;
00317 //          }
00318 //          mysql_free_result($res);
00319 //      }
00320 //      foreach ($this->groups as $key => $value) {
00321 //          $checked = (substr($selgroups, $value['id'], 1) == "1") ? " checked=\"checked\"" : "";
00322 //          if ($chk) { $chk .= "<br />\n"; }
00323 //          $chk .= "<input type=\"checkbox\" name=\"" . $permtype . "gid[" . $value['id'] . "]\"" . $checked . " /> " . $value['name'];
00324 //      }
00325 //      return $chk . "\n";
00326     }
00327 
00336     function _getPermsBox($permtype, $value = "", $selgroups = "0") {
00337 //      $type = ($permtype == "r") ? "read" : "write";
00338 //      $chkpermsgroup = "chk" . $type . "permsgroup";
00339 //      $onchange = " onchange=\"javascript:showChkPermsGroup('" . $type . "perm', '" . $chkpermsgroup . "');\"";
00340 //
00341 //      $box =  constant("LANG_" . strtoupper($type) . "PERMS") . "<br />\n<select name=\"" . $type . "perm\" id=\"" . $type . "perm\"" . $onchange . ">\n" .
00342 //              "<option value=\"0\"" . (($value == 0) ? " selected=\"selected\"" : "") . ">" . LANG_PERMSALL . "</option>\n" .
00343 //              "<option value=\"1\"" . (($value == 1) ? " selected=\"selected\"" : "") . ">" . LANG_PERMSLOGGEDIN . "</option>\n" .
00344 //              "<option value=\"2\"" . (($value == 2) ? " selected=\"selected\"" : "") . ">" . LANG_PERMSGROUP . "</option>\n" .
00345 //              "<option value=\"3\"" . (($value == 3) ? " selected=\"selected\"" : "") . ">" . LANG_PERMSADMIN . "</option>\n" .
00346 //              "</select>\n";
00347 //      $chkgroup = "<div id=\"" . $chkpermsgroup . "\"" . (($value == 2) ? "" : " style=\"display: none;\"") . ">\n" .
00348 //                  $this->getGroupsCheckBoxes($permtype, $selgroups) .
00349 //                  "\n</div><!-- " . $chkpermsgroup . " -->\n";
00350 //      return "<div class=\"uploadbox\">\n" . $box . $chkgroup . "</div><!-- uploadbox -->\n";
00351     }
00352 
00361     function setPerms($readperm, $writeperm, $groupreadperm, $groupwriteperm) {
00362 //      $this->readperm = $readperm;
00363 //      $this->writeperm = $writeperm;
00364 //      $this->groupreadperm = $groupreadperm;
00365 //      $this->groupwriteperm = $groupwriteperm;
00366     }
00367 
00375     function setForm($value) {
00376 //      global $cfg;
00377 //
00378 //      /* properties */
00379 //      if (is_array($value)) {         // edit content
00380 //          $v = each($value);
00381 //          // FIXME recupero tipo troppo laborioso: OTTIMIZZARE (anche _setContentPartForm())
00382 //          $id_type = $this->setContentPartForm($cfg->_typedefs[$v[1]['id_type']]['fielddefs'], $value);
00383 //          $this->addHidden("a", "update");
00384 //          $this->addHidden("id", $_GET['id']);
00385 //          $this->addHidden("t", $v[1]['id_type']);
00386 //      } else {                        // add a new content
00387 //          if (!is_array($cfg->_typedefs[$value])) { return FALSE; }
00388 //          if ($cfg->_typedefs[$value]['plugin']) { return FALSE; }
00389 //
00390 //          $this->setContentPartForm($cfg->_typedefs[$value]['fielddefs']);
00391 //          $this->addHidden("a", "add");
00392 //          $this->addHidden("t", $value);
00393 //          $this->addHidden("pid", $_GET['pid']);              // FIXME calcolare pid
00394 //      }
00395 //
00396 //      if ($_GET['lp']) { $this->addHidden("lp", $_GET['lp']); }
00397 //
00398 //      /* submit, cancel */
00399 //      $this->setSubmitValue(LANG_SAVE);
00400 //      $this->showCancel();
00401 //      return TRUE;
00402     }
00403 
00410     public function setDefaultValue($name, $value) {
00411         $this->form[$name]['default'] = $value;
00412     }
00413 
00419     public function getHTML() {
00420         foreach ($this->form as $key => $value) {
00421             switch ($value['type']) {
00422                 case "button":
00423                 case "reset":
00424                 case "hidden":
00425                     if ($hiddenlist) { $hiddenlist .= "\n"; }
00426                     $hiddenlist .= "<input type=\"hidden\" name=\"" . $key . "\" value=\"" . $value['value'] . "\"" . $value['attrs'] . " />";
00427                     break;
00428 
00429                 case "select":
00430                     for ($i = 0; $i < count($value['value']); $i++) {
00431                         $e .= "<option value=\"" . $value['value'][$i][0] . "\"";
00432                         if ($value['value'][$i][0] == $value['default']) { $e .= " selected"; }
00433                         $e .= ">" . $value['value'][$i][1] . "</options>\n";
00434                     }
00435                     $itemlist .= $this->getRenderedElement($value['label'],
00436                                                             "<select name=\"" . $key . "\"" . $value['attrs'] . ">\n" . $e . "</select>",
00437                                                             $value['tips']);
00438                     break;
00439 
00440                 case "textarea":
00441                     $itemlist .= $this->getRenderedElement($value['label'],
00442                                                             "<textarea name=\"" . $key . "\"" . $value['attrs'] . ">" . $value['value'] . "</textarea>",
00443                                                             $value['tips']);
00444                     break;
00445 
00446                 case "checkbox":
00447                     $itemlist .= $this->getRenderedElement("",
00448                                                             "<input type=\"checkbox\" name=\"" . $key . "\" value=\"" . $key . "\" " . ($value['value'] ? " checked=\"checked\"" : "") . " />&nbsp;" .
00449                                                             $value['label'],
00450                                                             $value['tips']);
00451                     break;
00452 
00453                 //case "option":
00454                     //break;
00455 
00456                 case "html":
00457                     $itemlist .= $this->getRenderedElement($value['label'], $value['value'], $value['tips']);
00458                     break;
00459 
00460 //              case "checkboxgroup":
00461 //                  /* build checkbox list */
00462 //                  foreach ($value['value'] as $itemkey => $itemvalue) {
00463 //                      if ($chklist) { $chklist .= "<br />\n"; }
00464 //                      $chklist .= "<input type=\"checkbox\" name=\"" . $key . "[" . $itemkey . "]\" value=\"" . $itemkey . "\"" . ($itemvalue['value'] ? " checked=\"checked\"" : "") . " /> " . $itemvalue['label'];
00465 //                  }
00466 //                  $itemlist .= $this->getRenderedElement($value['label'], "\n" . $chklist . "\n", $value['tips']);
00467 //                  break;
00468 
00469 //              case "fileuploadbox":       // meta-type
00470 //                  $itemlist .= "<div class=\"fileuploadcontrols\">\n" .
00471 //                               $this->getRenderedElement($value['label'],
00472 //                                                          "<input type=\"file\" name=\"" . $key . "\"" . $value['attrs'] . " />",
00473 //                                                          $value['tips']) .
00474 //                               $this->getRenderedElement(LANG_FILEDESCRIPTION,
00475 //                                                          "<input type=\"text\" name=\"" . $key . "_description\" value=\"\" />", "") .
00476 //                               $this->getRenderedElement(LANG_FILEVISIBLE,
00477 //                                                          "<input type=\"checkbox\" name=\"" . $key . "_visible\" value=\"" . $key . "_visible\" />", "") .
00478 //                               "</div><!-- fileuploadbox -->\n";
00479 //                  if ($value['value']) {
00480 //                      $hiddenlist .= "<input type=\"hidden\" name=\"" . $key . "_codename\" value=\"" . $value['value'] . "\" />\n";
00481 //                  }
00482 //                  break;
00483 
00484                 default:    // file, header, image, password, static, text
00485                     $newitem = $this->getRenderedElement($value['label'],
00486                                                             "<input type=\"" . $value['type'] . "\" name=\"" . $key . "\" value=\"" . $value['value'] . "\"" . $value['attrs'] . " />",
00487                                                             $value['tips']);
00488                     //if ($value['type'] == "file") { $newitem = "<div class=\"fileuploadcontrols\">\n" . $newitem . "</div><!-- fileuploadcontrols -->\n"; } 
00489                     $itemlist .= $newitem;
00490             }
00491         }
00492 
00493         /* permission controls */
00494         if ($this->permvisible) {
00495             $permcontrols = $this->getPermsBox("r", $this->readperm, $this->groupreadperm) .
00496                             $this->getPermsBox("w", $this->writeperm, $this->groupwriteperm);
00497         }
00498 
00499         $btnlist = "<input class=\"submitbutton\" type=\"submit\" name=\"submit\" value=\"&nbsp;" . $this->submit . "&nbsp;\" />\n" . $this->getCancelButton();
00500         if ($hiddenlist) { $btnlist = $hiddenlist . "\n" . $btnlist; }
00501         if ($btnlist) { $btnlist = "<div class=\"formbuttons\">\n" . $btnlist . "\n</div><!-- formbuttons -->\n"; }
00502         $f = $itemlist . $permcontrols . $btnlist;
00503         if ($f) { $f = $this->getFormHeader() . "<fieldset>\n" . $f . "</fieldset>\n</form>\n"; }
00504         return $f;
00505     }
00506 }
00507 ?>

Generated on Fri Feb 22 11:19:48 2008 for CMSkey by  doxygen 1.5.3