/**
 * Converts a date/time/datetime from one format to another
 * @param string $date The date
 * @param string $inFmt The input format (the format of the input date)
 * @param string $outFmt The output format (the format of the output date)
 * @return string the new date
 */
function KT_convertDate($date, $inFmt, $outFmt)
{
    if ($inFmt == '' || $outFmt == '' || $inFmt == $outFmt) {
        return $date;
    }
    if (!isset($date) || $date == '') {
        return;
    }
    if (strpos($inFmt, '%') !== false) {
        $inFmt = KT_format2newDF($inFmt);
    }
    if (strpos($outFmt, '%') !== false) {
        $outFmt = KT_format2newDF($outFmt);
    }
    $inFmtRule = KT_format2rule($inFmt);
    $outFmtRule = KT_format2rule($outFmt);
    $dateArr = KT_applyDate2rule($date, $inFmtRule);
    $outRule = KT_format2outRule($outFmt);
    $outdate = KT_applyOutRule2date($dateArr, $outFmtRule, $outRule);
    return $outdate;
}
 /**
 	NAME:
 		Execute
 	DESCRIPTION:
 		validates the columnsValue based on regExp and required information
 	ARGUMENTS:
 		none - 
 		property used: 
 				$columns
 				$columnsValue
 	RETURN:
 		string - empty on succes , an error message if fails
 		property changed:
 			- none
 	**/
 function Execute()
 {
     $failed = false;
     $errObj = new tNG_error('', array(), array());
     if ($this->mustValidate && count($this->columns) > 0) {
         $columnKeys = array_keys($this->columns);
         $cols = count($columnKeys);
         for ($i = 0; $i < $cols; $i++) {
             $doRequiredVal = true;
             $colIdx = $columnKeys[$i];
             $column =& $this->columns[$colIdx];
             if (!in_array($column['name'], array_keys($this->tNG->columns))) {
                 continue;
             }
             // on update don't require FILE_TYPE and tNG password fields
             if ($this->tNG->getTransactionType() == '_update' || $this->tNG->getTransactionType() == '_multipleUpdate') {
                 if ($this->tNG->getColumnType($column['name']) == 'FILE_TYPE') {
                     $doRequiredVal = false;
                 }
                 if ($this->tNG->getTable() == $GLOBALS['tNG_login_config']["table"] && $column['name'] == $GLOBALS['tNG_login_config']["password_field"]) {
                     $doRequiredVal = false;
                 }
                 // if it is setted to CURRVAL is not required;
                 if ($this->tNG->columns[$column['name']]['method'] == 'CURRVAL') {
                     $doRequiredVal = false;
                 }
             }
             $hasRequiredError = false;
             $hasTypeError = false;
             $tmpFieldValue = $this->tNG->getColumnValue($column['name']);
             if ($column['type'] == 'date' && $column['format'] != '') {
                 if (!in_array($this->tNG->getColumnType($column['name']), array('DATE_TYPE', 'DATE_ACCESS_TYPE'))) {
                     $tmpFieldValue = KT_formatDate2DB($tmpFieldValue);
                 }
             }
             $column['failed'] = false;
             // required parameter validation
             $colCustomMsg = $column['message'];
             if ($doRequiredVal && $column['required']) {
                 if (strlen($colCustomMsg) == 0) {
                     $colCustomMsg = $this->genericValidationMessages['required'];
                 }
                 if ((string) $tmpFieldValue == '') {
                     $failed = true;
                     $hasRequiredError = true;
                     $column['failed'] = true;
                     if ($this->tNG->exportsRecordset() !== true) {
                         $colCustomMsg = KT_DynamicData($colCustomMsg, $this->tNG, '', $this->tNG->transactionType == '_delete');
                         $errObj->addDetails('%s', array($colCustomMsg), array($colCustomMsg));
                     } else {
                         $errObj->setFieldError($column['name'], '%s', array($colCustomMsg));
                     }
                 }
             }
             // type and format validation
             $colCustomMsg = $column['message'];
             if ($tmpFieldValue != '' && $column['type'] != '') {
                 if (strlen($colCustomMsg) == 0) {
                     $colCustomMsgBefore = $this->genericValidationMessages['format'];
                     $colCustomMsgAfter = $this->genericValidationMessages[$column['type'] . '_' . $column['format']];
                     $colCustomMsg = sprintf($colCustomMsgBefore, $colCustomMsgAfter);
                 }
                 $tmpFieldValue = substr($tmpFieldValue, 0, 400);
                 switch ($column['type']) {
                     case 'regexp':
                         $res = @preg_match($column['additional_params'], $tmpFieldValue);
                         if ($res === false) {
                             $hasTypeError = true;
                             $colCustomMsgBefore = $this->genericValidationMessages['format'];
                             $colCustomMsgAfter = $this->genericValidationMessages['regexp_failed'];
                             $colCustomMsg = sprintf($colCustomMsgBefore, $colCustomMsgAfter);
                         }
                         if ($res === 0) {
                             $hasTypeError = true;
                         }
                         break;
                     case 'mask':
                         $myRegexp = $this->mask2regexp($column['additional_params']);
                         if (!preg_match($myRegexp, $tmpFieldValue)) {
                             $hasTypeError = true;
                         }
                         break;
                     case 'text':
                     case 'numeric':
                     case 'double':
                         $type = $column['type'];
                         $format = $column['format'];
                         if (is_array($this->validationRules[$type][$format])) {
                             $myValidationRule =& $this->validationRules[$type][$format];
                             if (isset($myValidationRule['mask'])) {
                                 $myRegexp = $this->mask2regexp($myValidationRule['mask']);
                                 $myValidationRule['regexp'] = $myRegexp;
                             }
                             if (isset($myValidationRule['regexp'])) {
                                 if (!preg_match($myValidationRule['regexp'], $tmpFieldValue)) {
                                     $hasTypeError = true;
                                 }
                             }
                             if (isset($myValidationRule['callback'])) {
                                 $ret = call_user_func(array('tNG_FormValidation', $myValidationRule['callback']), $tmpFieldValue);
                                 if (!$ret) {
                                     $hasTypeError = true;
                                 }
                             }
                         }
                         break;
                     case 'date':
                         $format = $column['format'];
                         $checkFullDateTime = true;
                         switch ($format) {
                             case 'date':
                                 $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format']);
                                 $checkFullDateTime = true;
                                 break;
                             case 'time':
                                 $inFmtRule = KT_format2rule($GLOBALS['KT_db_time_format_internal']);
                                 $checkFullDateTime = false;
                                 break;
                             case 'datetime':
                                 $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format'] . ' ' . $GLOBALS['KT_db_time_format_internal']);
                                 $checkFullDateTime = true;
                                 break;
                             default:
                                 break 2;
                         }
                         $dateArr = KT_applyDate2rule($tmpFieldValue, $inFmtRule);
                         $ret = KT_isValidDate($dateArr, $checkFullDateTime);
                         if (!$ret) {
                             $hasTypeError = true;
                         }
                         break;
                 }
             }
             if (!$hasRequiredError && $hasTypeError) {
                 $column['failed'] = true;
                 $failed = true;
                 if ($this->tNG->exportsRecordset() !== true) {
                     $colCustomMsg = KT_DynamicData($colCustomMsg, $this->tNG, '', $this->tNG->transactionType == '_delete');
                     $errObj->addDetails('%s', array($colCustomMsg), array($colCustomMsg));
                 } else {
                     $errObj->setFieldError($column['name'], '%s', array($colCustomMsg));
                 }
             }
         }
         for ($i = 0; $i < $cols; $i++) {
             $colIdx = $columnKeys[$i];
             $column =& $this->columns[$colIdx];
             if (!in_array($column['name'], array_keys($this->tNG->columns))) {
                 continue;
             }
             $hasMinMaxError = false;
             $tmpFieldValue = $this->tNG->getColumnValue($column['name']);
             if ($column['type'] == 'date' && $column['format'] != '') {
                 if (!in_array($this->tNG->getColumnType($column['name']), array('DATE_TYPE', 'DATE_ACCESS_TYPE'))) {
                     $tmpFieldValue = KT_formatDate2DB($tmpFieldValue);
                 }
             }
             // MIN MAX parameter validation
             $tNG_tNGfield_min = array();
             $tNG_tNGfield_max = array();
             $min = $column['min'];
             $min_placeholders = KT_getReplacementsFromMessage($min);
             if (count($min_placeholders) > 0) {
                 foreach ($min_placeholders as $key => $placeholder) {
                     if (strpos($placeholder, '.') === false) {
                         $tNG_tNGfield_min[] = $placeholder;
                     }
                 }
             }
             $max = $column['max'];
             $max_placeholders = KT_getReplacementsFromMessage($max);
             if (count($max_placeholders) > 0) {
                 foreach ($max_placeholders as $key => $placeholder) {
                     if (strpos($placeholder, '.') === false) {
                         $tNG_tNGfield_max[] = $placeholder;
                     }
                 }
             }
             $min = KT_DynamicData($min, $this->tNG);
             $max = KT_DynamicData($max, $this->tNG);
             // MIN parameter validation
             if ($tmpFieldValue != '' && $min != '') {
                 if ($column['type'] == 'text') {
                     if (strlen($tmpFieldValue) < $min) {
                         $hasMinMaxError = true;
                     }
                 }
                 if (in_array($column['type'], array('numeric', 'double'))) {
                     $evaluateNumeric = true;
                     if (count($tNG_tNGfield_min) > 0) {
                         foreach ($tNG_tNGfield_min as $key => $tNG_tNGfield) {
                             if (!isset($this->columns[$tNG_tNGfield]) || !in_array($this->columns[$tNG_tNGfield]['type'], array('numeric', 'double')) || $this->columns[$tNG_tNGfield]['format'] == '' || $column['failed']) {
                                 $evaluateNumeric = false;
                                 break;
                             }
                         }
                     }
                     $tmpFieldValue = str_replace(',', '.', $tmpFieldValue);
                     $min = str_replace(',', '.', $min);
                     if ($evaluateNumeric) {
                         $min = $this->tNG->evaluateNumeric($min);
                     }
                     if (floatval($tmpFieldValue) < floatval($min)) {
                         $hasMinMaxError = true;
                     }
                 }
                 if ($column['type'] == 'date') {
                     if (count($tNG_tNGfield_min) > 0) {
                         foreach ($tNG_tNGfield_min as $key => $tNG_tNGfield) {
                             if (in_array($this->tNG->getColumnType($tNG_tNGfield), array('DATE_TYPE', 'DATE_ACCESS_TYPE'))) {
                                 $min = KT_formatDate($min);
                                 break;
                             }
                         }
                     }
                     $minDate = KT_formatDate2DB($min);
                     $format = $column['format'];
                     $checkFullDateTime = true;
                     switch ($format) {
                         case 'date':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format']);
                             $checkFullDateTime = true;
                             break;
                         case 'time':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_time_format_internal']);
                             $checkFullDateTime = false;
                             break;
                         case 'datetime':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format'] . ' ' . $GLOBALS['KT_db_time_format_internal']);
                             $checkFullDateTime = true;
                             break;
                         default:
                             break 2;
                     }
                     $dateArr = KT_applyDate2rule($tmpFieldValue, $inFmtRule);
                     $minArr = KT_applyDate2rule($minDate, $inFmtRule);
                     if (KT_isValidDate($minArr, $checkFullDateTime)) {
                         if (KT_compareDates($dateArr, $minArr) === 1) {
                             $hasMinMaxError = true;
                         }
                     }
                 }
             }
             // MAX parameter validation
             if ($tmpFieldValue != '' && $max != '') {
                 if ($column['type'] == 'text') {
                     if (strlen($tmpFieldValue) > $max) {
                         $hasMinMaxError = true;
                     }
                 }
                 if (in_array($column['type'], array('numeric', 'double'))) {
                     $evaluateNumeric = true;
                     if (count($tNG_tNGfield_max) > 0) {
                         foreach ($tNG_tNGfield_max as $key => $tNG_tNGfield) {
                             if (!isset($this->columns[$tNG_tNGfield]) || !in_array($this->columns[$tNG_tNGfield]['type'], array('numeric', 'double')) || $this->columns[$tNG_tNGfield]['format'] == '' || $column['failed']) {
                                 $evaluateNumeric = false;
                                 break;
                             }
                         }
                     }
                     $tmpFieldValue = str_replace(',', '.', $tmpFieldValue);
                     $max = str_replace(',', '.', $max);
                     if ($evaluateNumeric) {
                         $max = $this->tNG->evaluateNumeric($max);
                     }
                     if (floatval($tmpFieldValue) > floatval($max)) {
                         $hasMinMaxError = true;
                     }
                 }
                 if ($column['type'] == 'date') {
                     if (count($tNG_tNGfield_max) > 0) {
                         foreach ($tNG_tNGfield_max as $key => $tNG_tNGfield) {
                             if (in_array($this->tNG->getColumnType($tNG_tNGfield), array('DATE_TYPE', 'DATE_ACCESS_TYPE'))) {
                                 $max = KT_formatDate($max);
                                 break;
                             }
                         }
                     }
                     $maxDate = KT_formatDate2DB($max);
                     $format = $column['format'];
                     $checkFullDateTime = true;
                     switch ($format) {
                         case 'date':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format']);
                             $checkFullDateTime = true;
                             break;
                         case 'time':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_time_format_internal']);
                             $checkFullDateTime = false;
                             break;
                         case 'datetime':
                             $inFmtRule = KT_format2rule($GLOBALS['KT_db_date_format'] . ' ' . $GLOBALS['KT_db_time_format_internal']);
                             $checkFullDateTime = true;
                             break;
                         default:
                             break 2;
                     }
                     $dateArr = KT_applyDate2rule($tmpFieldValue, $inFmtRule);
                     $maxArr = KT_applyDate2rule($maxDate, $inFmtRule);
                     if (KT_isValidDate($maxArr, $checkFullDateTime)) {
                         if (KT_compareDates($dateArr, $maxArr) === -1) {
                             $hasMinMaxError = true;
                         }
                     }
                 }
             }
             $colCustomMsg = $column['message'];
             if (strlen($colCustomMsg) == 0) {
                 $colCustomMsgBefore = $column['type'] == 'text' ? 'text' : 'other';
                 if ($min != '' && $max != '') {
                     $colCustomMsgAfter = 'between';
                     $colCustomMsg = $this->genericValidationMessages[$colCustomMsgBefore . '_' . $colCustomMsgAfter];
                     $colCustomMsg = sprintf($colCustomMsg, $min, $max);
                 } elseif ($min != '') {
                     $colCustomMsgAfter = 'min';
                     $colCustomMsg = $this->genericValidationMessages[$colCustomMsgBefore . '_' . $colCustomMsgAfter];
                     $colCustomMsg = sprintf($colCustomMsg, $min);
                 } else {
                     $colCustomMsgAfter = 'max';
                     $colCustomMsg = $this->genericValidationMessages[$colCustomMsgBefore . '_' . $colCustomMsgAfter];
                     $colCustomMsg = sprintf($colCustomMsg, $max);
                 }
             }
             if ($hasMinMaxError && $column['failed'] == false) {
                 $column['failed'] = true;
                 $failed = true;
                 if ($this->tNG->exportsRecordset() !== true) {
                     $colCustomMsg = KT_DynamicData($colCustomMsg, $this->tNG, '', $this->tNG->transactionType == '_delete');
                     $errObj->addDetails('%s', array($colCustomMsg), array($colCustomMsg));
                 } else {
                     $errObj->setFieldError($column['name'], '%s', array($colCustomMsg));
                 }
             }
         }
     }
     if (!$failed) {
         $errObj = null;
     } else {
         if ($this->tNG->exportsRecordset() === true) {
             $errObj->addDetails('%s', array($this->genericValidationMessages['failed']), array(''));
         }
     }
     return $errObj;
 }