Exemplo n.º 1
0
 function handleAddEditCopy($id = null)
 {
     if (!is_null($id)) {
         $id = $this->dbQuote($id);
     }
     if (!empty($_POST)) {
         $table = $GLOBALS["table"];
         $add = $GLOBALS["add"];
         $edit = $GLOBALS["edit"];
         $idcopy = $GLOBALS["idcopy"];
         $useFunctions = $this->getConfig('useFunctions');
         foreach ($this->fields as $field => $value) {
             if (!empty($value['noEdit']) and !isset($value['default']) or $field == $this->pk and !$this->getConfig('allowPKEditing')) {
                 continue;
             }
             if (!empty($value['noEdit']) and isset($value['default'])) {
                 $unQuoted[$field] = $value['default'];
                 $quoted[$field] = $this->dbQuote($value['default']);
                 continue;
             }
             if ($useFunctions) {
                 $function = $_POST['function'][$field];
                 if (!empty($function) and !empty($this->config['functions'][$function])) {
                     $_POST[$field] = call_user_func($this->config['functions'][$function], $_POST[$field]);
                 }
             }
             //				if(!empty($this->fields[$field]['required']) AND ($_POST[$field]==='' OR !isset($_POST[$field])))
             if (!empty($this->fields[$field]['required']) and ($_POST[$field] === '' and isset($_POST[$field]))) {
                 if ($this->fields[$field]['input'] == 'file' && ($_FILES[$field . 'Change']['tmp_name'] != "" || $_FILES[$field]['tmp_name'] != "")) {
                 } else {
                     $this->setContextualError($field, TERequired . ' - ' . $this->fields[$field]['input'] . ' ' . $_FILES[$field . 'Change']['tmp_name']);
                 }
                 // Raul 26-Dic-2007
             }
             switch ($this->fields[$field]['input']) {
                 case 'file':
                     // Raul 02-Ene-2008
                     $FileName = '';
                     if (isset($_FILES[$field]['tmp_name'])) {
                         $FieldFile = $_FILES[$field]['tmp_name'];
                         $FileName = $_FILES[$field]['name'];
                     } else {
                         $FieldFile = '';
                     }
                     if (isset($_FILES[$field . 'Change']['tmp_name'])) {
                         $FieldFileChange = $_FILES[$field . 'Change']['tmp_name'];
                         $FileName = $_FILES[$field . 'Change']['name'];
                     } else {
                         $FieldFileChange = '';
                     }
                     if (isset($_POST[$field])) {
                         $OldFile = $_POST[$field];
                     } else {
                         $OldFile = '';
                     }
                     if ($FileName) {
                         $DownloadType = 0;
                         $Ext = TEDocumentType($FileName, 1);
                         if ($this->fields[$field]['DownloadType']) {
                             $AcceptedTypes = '';
                             foreach ($this->fields[$field]['DownloadType'] as $Exts) {
                                 if ($Exts == $Ext) {
                                     $DownloadType = 1;
                                     break;
                                 } else {
                                     $AcceptedTypes .= '  .' . $Exts . ';  ';
                                 }
                             }
                         }
                         if ($DownloadType == 0 && $this->fields[$field]['DownloadType']) {
                             $this->setContextualError($field, TEFieldErrorDownloadType . ' ' . $AcceptedTypes . '');
                         }
                     }
                     break;
                 case 'password':
                     // Raul 08-Dic-2007
                     if (isset($_POST[$field])) {
                         $FieldPassword = $_POST[$field];
                     } else {
                         $FieldPassword = '';
                     }
                     if (isset($_POST[$field . 'Change'])) {
                         $FieldPasswordChange = $_POST[$field . 'Change'];
                     } else {
                         $FieldPasswordChange = '';
                     }
                     if (isset($_POST[$field . 'Confirm'])) {
                         $FieldPasswordConfirm = $_POST[$field . 'Confirm'];
                     } else {
                         $FieldPasswordConfirm = '';
                     }
                     if ($add) {
                         if ($FieldPassword != $FieldPasswordConfirm) {
                             $this->setContextualError($field, TEErrorPassword);
                         }
                     } elseif (($FieldPasswordChange || $FieldPasswordConfirm) && $FieldPasswordChange != $FieldPasswordConfirm) {
                         $this->setContextualError($field, TEErrorPassword);
                     }
                     break;
                 case 'email':
                     if ($_POST[$field] && !TEEmailValidate($_POST[$field])) {
                         $this->setContextualError($field, TEErrorEmail);
                     }
                     break;
                     // Raul 08-Dic-2007
                 // Raul 08-Dic-2007
                 case 'int':
                     if ($_POST[$field] && !TEIntValidate($_POST[$field])) {
                         $this->setContextualError($field, TEErrorInt);
                     }
                     break;
                     // Raul 08-Dic-2007
                 // Raul 08-Dic-2007
                 case 'bitmask':
                     $v = 0;
                     if (!empty($_POST[$field])) {
                         foreach ($_POST[$field] as $bit) {
                             $v |= $bit;
                         }
                     }
                     $_POST[$field] = $v;
                     break;
             }
             if (!empty($this->validationCallbacks[$field])) {
                 foreach ($this->validationCallbacks[$field] as $c) {
                     $_POST[$field] = call_user_func($c, &$this, $_POST[$field]);
                 }
             }
             $unQuoted[$field] = $_POST[$field];
             $quoted[$field] = $this->dbQuote($_POST[$field]);
             $fields[] = $field;
         }
         if (!empty($this->errors) or !empty($this->contextErrors)) {
             $this->displayAddEditCopyPage($id);
         }
         foreach ($fields as $f) {
             if ($this->fields[$f]['FieldHidden']) {
                 $sets[] = "{$f} = {$quoted[$f]}";
             } else {
                 switch ($this->fields[$f]['input']) {
                     case 'password':
                         // Raul 08-Dic-2007
                         if ($add) {
                             $sets[] = "{$f} = md5({$quoted[$f]})";
                         } else {
                             if (isset($_POST[$f])) {
                                 $FPassword = $_POST[$f];
                             } else {
                                 $FPassword = '';
                             }
                             if (isset($_POST[$f . 'Change'])) {
                                 $FPasswordChange = $_POST[$f . 'Change'];
                             } else {
                                 $FPasswordChange = '';
                             }
                             if (isset($_POST[$f . 'Confirm'])) {
                                 $FPasswordConfirm = $_POST[$f . 'Confirm'];
                             } else {
                                 $FPasswordConfirm = '';
                             }
                             if (($FPasswordChange || $FPasswordConfirm) && $FPasswordChange == $FPasswordConfirm) {
                                 $PassNew = "'" . md5($FPasswordChange) . "'";
                                 $sets[] = "{$f} = {$PassNew}";
                             } else {
                                 $sets[] = "{$f} = {$quoted[$f]}";
                             }
                         }
                         break;
                     case 'file':
                         // Raul 10-Dic-2007
                         if (isset($_FILES[$f]['tmp_name'])) {
                             $FieldFile = $_FILES[$f]['tmp_name'];
                         } else {
                             $FieldFile = '';
                         }
                         if (isset($_FILES[$f . 'Change']['tmp_name'])) {
                             $FieldFileChange = $_FILES[$f . 'Change']['tmp_name'];
                         } else {
                             $FieldFileChange = '';
                         }
                         if (isset($_POST[$f])) {
                             $OldFile = $_POST[$f];
                         } else {
                             $OldFile = '';
                         }
                         if ($this->fields[$f]['DownloadPath'] && file_exists($this->fields[$f]['DownloadPath'])) {
                             $TEDownloadPath = $this->fields[$f]['DownloadPath'];
                         } else {
                             $TEDownloadPath = TEDownloadsPath;
                         }
                         if ($add) {
                             if ($FieldFile) {
                                 $TableName = str_replace(' ', '', TETablesNames($table));
                                 $CurrenDateTime = str_replace('-', '', str_replace(':', '', str_replace(' ', '', TECurrentDateTime())));
                                 $TEFileNewName = $TableName . $CurrenDateTime;
                                 $TEfile = "'" . TEDownloadFile($f, $TEDownloadPath, $TEFileNewName) . "'";
                                 if ($TEfile) {
                                     $sets[] = "{$f} = {$TEfile}";
                                 }
                             }
                         } elseif ($FieldFileChange) {
                             $TableName = str_replace(' ', '', TETablesNames($table));
                             $CurrenDateTime = str_replace('-', '', str_replace(':', '', str_replace(' ', '', TECurrentDateTime())));
                             $TEFileNewName = $TableName . $CurrenDateTime;
                             $TEfile = "'" . TEDownloadFile($f . 'Change', $TEDownloadPath, $TEFileNewName) . "'";
                             if ($TEfile) {
                                 if ($OldFile && file_exists($TEDownloadPath . $OldFile)) {
                                     unlink($TEDownloadPath . $OldFile);
                                 }
                                 $sets[] = "{$f} = {$TEfile}";
                             }
                         } elseif ($idcopy) {
                             $auxiliar = substr_replace($quoted[$f], "", 0, 1);
                             $auxiliar = substr_replace($auxiliar, "", -1, 1);
                             $extension = substr($auxiliar, -3, 3);
                             $imagen = $this->fields[$f]['DownloadPath'] . $auxiliar;
                             $TableName = str_replace(' ', '', TETablesNames($table));
                             $CurrenDateTime = str_replace('-', '', str_replace(':', '', str_replace(' ', '', TECurrentDateTime())));
                             $TEFileNewName = $TableName . $CurrenDateTime . '.' . $extension;
                             switch ($extension) {
                                 case 'jpg':
                                     $original = imagecreatefromjpeg($imagen);
                                     break;
                                 case 'gif':
                                     $original = imagecreatefromgif($imagen);
                                     break;
                                 case 'png':
                                     $original = imagecreatefrompng($imagen);
                                     break;
                             }
                             $ancho = imagesx($original);
                             $alto = imagesy($original);
                             $copia = $this->fields[$f]['DownloadPath'] . $TEFileNewName;
                             $medio = imagecreatetruecolor($ancho, $alto);
                             imagecopy($medio, $original, 0, 0, 0, 0, $ancho, $alto);
                             switch ($extension) {
                                 case 'jpg':
                                     imagejpeg($medio, $copia, 90);
                                     break;
                                     // 90 es la calidad de compresión
                                 // 90 es la calidad de compresión
                                 case 'gif':
                                     imagegif($medio, $copia);
                                     break;
                                 case 'png':
                                     imagepng($medio, $copia, 0);
                                     break;
                             }
                             $TEfile = "'" . $TEFileNewName . "'";
                             $sets[] = "{$f} = {$TEfile}";
                         } else {
                             $sets[] = "{$f} = {$quoted[$f]}";
                         }
                         if ($TEfile) {
                             $auxiliar = substr_replace($TEfile, "", 0, 1);
                             $auxiliar = substr_replace($auxiliar, "", -1, 1);
                             $extension = substr($auxiliar, -3, 3);
                             $imagen = $this->fields[$f]['DownloadPath'] . $auxiliar;
                             $imagen_adjust = substr_replace($imagen, "_adjust.", -4, 1);
                             $adjust = 0;
                             switch ($extension) {
                                 case 'jpg':
                                     $original = imagecreatefromjpeg($imagen);
                                     break;
                                 case 'gif':
                                     $original = imagecreatefromgif($imagen);
                                     break;
                                 case 'png':
                                     $original = imagecreatefrompng($imagen);
                                     break;
                             }
                             $ancho = imagesx($original);
                             $alto = imagesy($original);
                             if ($this->fields[$f]['ImageWidth']) {
                                 if ($this->fields[$f]['ImageHeight']) {
                                     if ($ancho > $this->fields[$f]['ImageWidth'] or $alto > $this->fields[$f]['ImageHeight']) {
                                         if (!file_exists($imagen_adjust)) {
                                             $medio = imagecreatetruecolor($this->fields[$f]['ImageWidth'], $this->fields[$f]['ImageHeight']);
                                             imagecopyresampled($medio, $original, 0, 0, 0, 0, $this->fields[$f]['ImageWidth'], $this->fields[$f]['ImageHeight'], $ancho, $alto);
                                             $adjust = 1;
                                         }
                                     }
                                 } else {
                                     if ($ancho > $this->fields[$f]['ImageWidth']) {
                                         if (!file_exists($imagen_adjust)) {
                                             $alto_medio = $this->fields[$f]['ImageWidth'] * $alto / $ancho;
                                             $medio = imagecreatetruecolor($this->fields[$f]['ImageWidth'], $alto_medio);
                                             imagecopyresampled($medio, $original, 0, 0, 0, 0, $this->fields[$f]['ImageWidth'], $alto_medio, $ancho, $alto);
                                             $adjust = 1;
                                         }
                                     }
                                 }
                             } elseif ($this->fields[$f]['ImageHeight']) {
                                 if ($alto > $this->fields[$f]['ImageHeight']) {
                                     if (!file_exists($imagen_adjust)) {
                                         $ancho_medio = $this->fields[$f]['ImageHeight'] * $ancho / $alto;
                                         $medio = imagecreatetruecolor($ancho_medio, $this->fields[$f]['ImageHeight']);
                                         imagecopyresampled($medio, $original, 0, 0, 0, 0, $ancho_medio, $this->fields[$f]['ImageHeight'], $ancho, $alto);
                                         $adjust = 1;
                                     }
                                 }
                             }
                             if ($adjust == 1) {
                                 switch ($extension) {
                                     case 'jpg':
                                         imagejpeg($medio, $imagen_adjust, 90);
                                         break;
                                         // 90 es la calidad de compresión
                                     // 90 es la calidad de compresión
                                     case 'gif':
                                         imagegif($medio, $imagen_adjust);
                                         break;
                                     case 'png':
                                         imagepng($medio, $imagen_adjust, 0);
                                         break;
                                 }
                                 unlink($imagen);
                                 rename($imagen_adjust, $imagen);
                             }
                         }
                         break;
                     case 'wysiwyg':
                         $Value = "'" . utf8_decode($_POST[$f]) . "'";
                         $sets[] = "{$f} = {$Value}";
                         break;
                     case 'date':
                         $DateAux = str_replace("'", "", $quoted[$f]);
                         $DateAux = "'" . TEDateToMysql($DateAux) . "'";
                         $sets[] = "{$f} = {$DateAux}";
                         break;
                     case 'int':
                         $DecAux = str_replace("'", "", $quoted[$f]);
                         $DecAux = str_replace("\$", "", $quoted[$f]);
                         $DecAux = str_replace(",", "", $quoted[$f]);
                         $sets[] = "{$f} = {$DecAux}";
                         break;
                     case 'decimal':
                         $DecAux = str_replace("'", "", $quoted[$f]);
                         $DecAux = str_replace("\$", "", $quoted[$f]);
                         $DecAux = str_replace(",", "", $quoted[$f]);
                         $sets[] = "{$f} = {$DecAux}";
                         //if ($value["display"]=="DESCUENTO") {echo ""; exit();}
                         break;
                     case 'binary':
                         $aux_binary = str_replace("'", "", $quoted[$f]);
                         if ($aux_binary == "on") {
                             $aux = 1;
                         } else {
                             $aux = 0;
                         }
                         $sets[] = "{$f} = {$aux}";
                         break;
                     case 'datetime':
                         $DateTimeAux = str_replace("'", "", $quoted[$f]);
                         $DateTimeAux = "'" . TEDatetimeToMysql($DateTimeAux) . "'";
                         $sets[] = "{$f} = {$DateTimeAux}";
                         break;
                         //						default: $sets[]="$f = UPPER({$quoted[$f]})"; break;
                     //						default: $sets[]="$f = UPPER({$quoted[$f]})"; break;
                     default:
                         $sets[] = "{$f} = {$quoted[$f]}";
                         break;
                 }
             }
         }
         $sets_original = $sets;
         $sets = implode(', ', $sets);
         if (isset($_GET['edit'])) {
             if (!empty($this->dataFilters)) {
                 $filters = implode(' AND ', $this->dataFilters);
             } else {
                 $filters = 1;
             }
             list($tables, $joinClause) = $this->getQueryTables();
             $sql = "UPDATE {$tables} SET {$sets} WHERE {$joinClause} AND {$filters} AND {$this->pk} = {$id}";
             //				echo print_r($_POST);
             $res = $this->dbQuery($sql);
             //echo $sql.""; exit();
             define("ID", $id);
             include "" . TEIncludesPath . "AdditionalEdit.php";
             if (!$res) {
                 $error_2 = $this->dbError();
                 $this->errors[] = TEErrorUpdateRecord . ": " . $error_2;
             } else {
                 if (!empty($this->editCallbacks)) {
                     foreach ($this->editCallbacks as $c) {
                         call_user_func($c, $unQuoted);
                     }
                 }
             }
         } elseif (!empty($_GET['add']) or isset($_GET['idcopy'])) {
             $sql = "INSERT INTO {$this->table} SET {$sets}";
             //echo $sql."+++<br><br>"; //exit();
             $res = $this->dbQuery($sql);
             if (!$res) {
                 $this->errors[] = TEErrorAddRecord . ": " . $this->dbError();
             } else {
                 $id = $this->dbGetOne("SELECT LAST_INSERT_ID()");
                 if ($id == "" or $id == 0) {
                     define("ID", $_POST[TEFieldKey($this->table)]);
                 } else {
                     define("ID", $id);
                 }
                 include "" . TEIncludesPath . "AdditionalAdd.php";
                 //exit();
                 if (!empty($_GET['add'])) {
                     if (!empty($this->addCallbacks)) {
                         foreach ($this->addCallbacks as $c) {
                             call_user_func($c, $unQuoted);
                         }
                     }
                 } elseif (isset($_GET['idcopy'])) {
                     if (!empty($this->copyCallbacks)) {
                         foreach ($this->copyCallbacks as $c) {
                             call_user_func($c, $unQuoted);
                         }
                     }
                 }
             }
         }
         if ($id == "" or $id == 0) {
             foreach ($sets_original as $value) {
                 $aux = explode("=", $value);
                 if (trim($aux[0]) == TEFieldKey($table)) {
                     $id = trim($aux[1]);
                 }
             }
         }
         //		echo $id; exit();
         if (empty($this->errors)) {
             include '' . TEIncludesPath . 'TERedirection.php';
             TERedirection($LocationUrl);
             exit;
         }
     }
     $this->displayAddEditCopyPage($id);
 }
                $result = mysql_db_query(DB_NAME, $sql_borrar);
                //echo $sql_borrar;
                if (!empty($NUEVO_editor_hijo[$TABLA_HIJO["TABLA_HIJO"]]->deleteCallbacks)) {
                    foreach ($NUEVO_editor_hijo[$TABLA_HIJO["TABLA_HIJO"]]->deleteCallbacks as $c) {
                        call_user_func($c, $x_borrar);
                    }
                }
            }
        } else {
            $arma_campos = "";
            $invalido = "";
            $no_vacio = 0;
            foreach ($campos as $valor) {
                if ($valor != $llave_tabla_hijo) {
                    if ($NUEVO_editor_hijo[$TABLA_HIJO["TABLA_HIJO"]]->fields[$valor]['input'] == "date") {
                        $arma_campos .= $valor . "='" . TEDateToMysql($_POST[$valor][$i]) . "', ";
                    } else {
                        $arma_campos .= $valor . "='" . $_POST[$valor][$i] . "', ";
                    }
                    //			  if (in_array($valor, $TABLA_HIJO["VALIDAR_CAMPOS"]))
                    if ($_POST[$valor][$i] != "") {
                        $no_vacio = 1;
                    }
                }
            }
            if (1 == 0) {
                if ($invalido != "" and $aviso == "") {
                    ?>
 
			  <script language="javascript">
                alert('Campos indispensables : '+'<?php