/** * save a profile field in the database * * @param XoopsObject|ProfileField $obj reference to the object * @param bool $force whether to force the query execution despite security settings * * @internal param bool $checkObject check if the object is dirty and clean the attributes * @return bool FALSE if failed, TRUE if already present and unchanged or successful */ public function insert(XoopsObject $obj, $force = false) { if (!$obj instanceof $this->className) { return false; } $profile_handler = xoops_getModuleHandler('profile', 'profile'); $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name'))); $obj->cleanVars(); $defaultstring = ''; switch ($obj->getVar('field_type')) { case 'datetime': case 'date': $obj->setVar('field_valuetype', XOBJ_DTYPE_INT); $obj->setVar('field_maxlength', 10); break; case 'longdate': $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME); break; case 'yesno': $obj->setVar('field_valuetype', XOBJ_DTYPE_INT); $obj->setVar('field_maxlength', 1); break; case 'textbox': if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); } break; case 'autotext': if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA); } break; case 'group_multi': case 'select_multi': case 'checkbox': $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY); break; case 'language': case 'timezone': case 'theme': $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); break; case 'dhtml': case 'textarea': $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA); break; } if ($obj->getVar('field_valuetype') === '') { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); } if (!in_array($obj->getVar('field_name'), $this->getUserVars()) && isset($_REQUEST['field_required'])) { if ($obj->isNew()) { //add column to table $changetype = 'ADD'; } else { //update column information $changetype = 'CHANGE `' . $obj->getVar('field_name', 'n') . '`'; } $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : ''; //set type switch ($obj->getVar('field_valuetype')) { default: case XOBJ_DTYPE_ARRAY: case XOBJ_DTYPE_UNICODE_ARRAY: $type = 'mediumtext'; break; case XOBJ_DTYPE_UNICODE_EMAIL: case XOBJ_DTYPE_UNICODE_TXTBOX: case XOBJ_DTYPE_UNICODE_URL: case XOBJ_DTYPE_EMAIL: case XOBJ_DTYPE_TXTBOX: case XOBJ_DTYPE_URL: $type = 'varchar'; // varchars must have a maxlength if (!$maxlengthstring) { //so set it to max if maxlength is not set - or should it fail? $maxlengthstring = '(255)'; $obj->setVar('field_maxlength', 255); } break; case XOBJ_DTYPE_INT: $type = 'int'; break; case XOBJ_DTYPE_DECIMAL: $type = 'decimal(14,6)'; break; case XOBJ_DTYPE_FLOAT: $type = 'float(15,9)'; break; case XOBJ_DTYPE_OTHER: case XOBJ_DTYPE_UNICODE_TXTAREA: case XOBJ_DTYPE_TXTAREA: $type = 'text'; $maxlengthstring = ''; break; case XOBJ_DTYPE_MTIME: $type = 'date'; $maxlengthstring = ''; break; } $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL'; $result = $force ? $this->db->queryF($sql) : $this->db->query($sql); if (!$result) { return false; } } //change this to also update the cached field information storage $obj->setDirty(); if (!parent::insert($obj, $force)) { return false; } return $obj->getVar('field_id'); }
/** * save a profile field in the database * * @param XoopsObject|ProfileField $obj reference to the object * @param bool $force whether to force the query execution despite security settings * @return bool FALSE if failed, TRUE if already present and unchanged or successful */ public function insertFields(XoopsObject $obj, $force = false) { $xoops = Xoops::getInstance(); $profile_handler = $xoops->getModuleHandler('profile', 'profile'); $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name'))); $obj->cleanVars(false); //Don't quote switch ($obj->getVar('field_type')) { case "datetime": case "date": $obj->setVar('field_valuetype', XOBJ_DTYPE_INT); $obj->setVar('field_maxlength', 10); break; case "longdate": $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME); break; case "yesno": $obj->setVar('field_valuetype', XOBJ_DTYPE_INT); $obj->setVar('field_maxlength', 1); break; case "textbox": if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); } break; case "autotext": if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA); } break; case "group_multi": case "select_multi": case "checkbox": $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY); break; case "language": case "timezone": case "theme": $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); break; case "dhtml": case "textarea": $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA); break; } if ($obj->getVar('field_valuetype') == "") { $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX); } if (!in_array($obj->getVar('field_name'), $this->getUserVars())) { if ($obj->isNew()) { //add column to table $changetype = "ADD"; } else { //update column information $changetype = "CHANGE `" . $obj->getVar('field_name', 'n') . "`"; } $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? "(" . $obj->getVar('field_maxlength') . ")" : ""; //set type switch ($obj->getVar('field_valuetype')) { default: case XOBJ_DTYPE_ARRAY: case XOBJ_DTYPE_EMAIL: case XOBJ_DTYPE_TXTBOX: case XOBJ_DTYPE_URL: $type = "varchar"; // varchars must have a maxlength if (!$maxlengthstring) { //so set it to max if maxlength is not set - or should it fail? $maxlengthstring = "(255)"; $obj->setVar('field_maxlength', 255); } break; case XOBJ_DTYPE_INT: $type = "int"; break; case XOBJ_DTYPE_DECIMAL: $type = "decimal(14,6)"; break; case XOBJ_DTYPE_FLOAT: $type = "float(15,9)"; break; case XOBJ_DTYPE_OTHER: case XOBJ_DTYPE_TXTAREA: $type = "text"; $maxlengthstring = ""; break; case XOBJ_DTYPE_MTIME: $type = "date"; $maxlengthstring = ""; break; } $sql = "ALTER TABLE `" . $profile_handler->table . "` " . $changetype . " `" . $obj->cleanVars['field_name'] . "` " . $type . $maxlengthstring . ' NULL'; if (!$this->db->query($sql)) { return false; } } //change this to also update the cached field information storage $obj->setDirty(); if (!parent::insert($obj, $force)) { return false; } return $obj->getVar('field_id'); }