Exemple #1
0
function formEditAgent($pDB, $smarty, $module_name, $local_templates_dir, $id_agent)
{
    // Si se ha indicado cancelar, volver a listado sin hacer nada más
    if (isset($_POST['cancel'])) {
        Header("Location: ?menu={$module_name}");
        return '';
    }
    $smarty->assign('FRAMEWORK_TIENE_TITULO_MODULO', existeSoporteTituloFramework());
    // Leer los datos de la campaña, si es necesario
    $arrAgente = NULL;
    $oAgentes = new Agentes($pDB);
    if (!is_null($id_agent)) {
        $arrAgente = $oAgentes->getAgents($id_agent);
        if (!is_array($arrAgente) || count($arrAgente) == 0) {
            $smarty->assign("mb_title", 'Unable to read agent');
            $smarty->assign("mb_message", 'Cannot read agent - ' . $oAgentes->errMsg);
            return '';
        }
    }
    require_once "libs/paloSantoForm.class.php";
    $arrFormElements = getFormAgent($smarty, !is_null($id_agent));
    // Valores por omisión para primera carga
    if (is_null($id_agent)) {
        // Creación de nuevo agente
        if (!isset($_POST['extension'])) {
            $_POST['extension'] = '';
        }
        if (!isset($_POST['description'])) {
            $_POST['description'] = '';
        }
        if (!isset($_POST['password1'])) {
            $_POST['password1'] = '';
        }
        if (!isset($_POST['password2'])) {
            $_POST['password2'] = '';
        }
        if (!isset($_POST['eccpwd1'])) {
            $_POST['eccpwd1'] = '';
        }
        if (!isset($_POST['eccpwd2'])) {
            $_POST['eccpwd2'] = '';
        }
    } else {
        // Modificación de agente existente
        if (!isset($_POST['extension'])) {
            $_POST['extension'] = $arrAgente['number'];
        }
        if (!isset($_POST['description'])) {
            $_POST['description'] = $arrAgente['name'];
        }
        if (!isset($_POST['password1'])) {
            $_POST['password1'] = $arrAgente['password'];
        }
        if (!isset($_POST['password2'])) {
            $_POST['password2'] = $arrAgente['password'];
        }
        if (!isset($_POST['eccpwd1'])) {
            $_POST['eccpwd1'] = $arrAgente['eccp_password'];
        }
        if (!isset($_POST['eccpwd2'])) {
            $_POST['eccpwd2'] = $arrAgente['eccp_password'];
        }
        // Volver opcional el cambio de clave de acceso
        $arrFormElements['password1']['REQUIRED'] = 'no';
        $arrFormElements['password2']['REQUIRED'] = 'no';
    }
    $oForm = new paloForm($smarty, $arrFormElements);
    if (!is_null($id_agent)) {
        $oForm->setEditMode();
        $smarty->assign("id_agent", $id_agent);
    }
    $bDoCreate = isset($_POST['submit_save_agent']);
    $bDoUpdate = isset($_POST['submit_apply_changes']);
    if ($bDoCreate || $bDoUpdate) {
        if (!$oForm->validateForm($_POST)) {
            // Falla la validación básica del formulario
            $smarty->assign("mb_title", _tr("Validation Error"));
            $arrErrores = $oForm->arrErroresValidacion;
            $strErrorMsg = "<b>" . _tr('The following fields contain errors') . ":</b><br>";
            foreach ($arrErrores as $k => $v) {
                $strErrorMsg .= "{$k}, ";
            }
            $strErrorMsg .= "";
            $smarty->assign("mb_message", $strErrorMsg);
        } else {
            foreach (array('extension', 'password1', 'password2', 'description', 'eccpwd1', 'eccpwd2') as $k) {
                $_POST[$k] = trim($_POST[$k]);
            }
            if ($_POST['password1'] != $_POST['password2'] || $bDoCreate && $_POST['password1'] == '') {
                $smarty->assign("mb_title", _tr("Validation Error"));
                $smarty->assign("mb_message", _tr("The passwords are empty or don't match"));
            } elseif ($_POST['eccpwd1'] != $_POST['eccpwd2']) {
                $smarty->assign("mb_title", _tr("Validation Error"));
                $smarty->assign("mb_message", _tr("ECCP passwords don't match"));
            } elseif (!ereg('^[[:digit:]]+$', $_POST['password1'])) {
                $smarty->assign("mb_title", _tr("Validation Error"));
                $smarty->assign("mb_message", _tr("The passwords aren't numeric values"));
            } elseif (!ereg('^[[:digit:]]+$', $_POST['extension'])) {
                $smarty->assign("mb_title", _tr("Validation Error"));
                $smarty->assign("mb_message", _tr("Error Agent Number"));
            } else {
                $bExito = TRUE;
                if ($bDoUpdate && $_POST['password1'] == '') {
                    $_POST['password1'] = $arrAgente['password'];
                }
                $agente = array(0 => $_POST['extension'], 1 => $_POST['password1'], 2 => $_POST['description'], 3 => $_POST['eccpwd1']);
                if ($bDoCreate) {
                    $bExito = $oAgentes->addAgent($agente);
                    if (!$bExito) {
                        $smarty->assign("mb_message", "" . _tr("Error Insert Agent") . " " . $oAgentes->errMsg);
                    }
                } elseif ($bDoUpdate) {
                    $bExito = $oAgentes->editAgent($agente);
                    if (!$bExito) {
                        $smarty->assign("mb_message", "" . _tr("Error Update Agent") . " " . $oAgentes->errMsg);
                    }
                }
                if ($bExito) {
                    header("Location: ?menu={$module_name}");
                }
            }
        }
    }
    $smarty->assign('icon', 'images/user.png');
    $contenidoModulo = $oForm->fetchForm("{$local_templates_dir}/new.tpl", is_null($id_agent) ? _tr("New agent") : _tr('Edit agent') . ' "' . $_POST['description'] . '"', $_POST);
    return $contenidoModulo;
}