git-svn-id: https://192.168.0.254/svn/Proyectos.Incam_Intranet/trunk/src@69 e2b1556b-49f8-d141-9351-52d6861a72d9
156 lines
4.6 KiB
PHP
156 lines
4.6 KiB
PHP
<?php
|
||
|
||
/*
|
||
* Clase ListaOfertas
|
||
*/
|
||
|
||
include_once("Objects/Oferta.php");
|
||
|
||
class ListaOfertas {
|
||
|
||
// Atributos:
|
||
/* Una lista de pedidos. */
|
||
private $ofertas = array();
|
||
|
||
/* Gestor dueño de la lista. */
|
||
private $gestor;
|
||
|
||
/* Campos por los que ordenar la lista. */
|
||
private $orden;
|
||
|
||
/* Condiciones para búsquedas. */
|
||
private $condiciones;
|
||
|
||
// Constructor:
|
||
/**
|
||
* Crea una lista de ofertas.
|
||
*
|
||
* @param usuario $ - gestor dueño de la lista.
|
||
* @param orden $ - parámetros por los que ordenar la lista.
|
||
* @param sql $ - consulta de búsqueda.
|
||
*/
|
||
function ListaOfertas($usuario, $condiciones=array(), $orden=array()) {
|
||
$this->gestor = $usuario;
|
||
$this->orden = $orden;
|
||
$this->condiciones = $condiciones;
|
||
}
|
||
|
||
/**
|
||
* Busca y devuelve todos los pedidos del usuario.
|
||
*/
|
||
function getOfertas() {
|
||
// sacado de http://patrickallaert.blogspot.com/2007/09/building-dynamic-sql-queries-elegant.html
|
||
|
||
$consulta = "SELECT * FROM candidato_pedido";
|
||
|
||
if (count($this->condiciones)) {
|
||
$consulta .= ' WHERE ' . implode(' AND ', $this->condiciones);
|
||
}
|
||
|
||
if (count($this->orden)) {
|
||
$consulta .= ' ORDER BY ' . implode(' , ', $this->orden);
|
||
}
|
||
|
||
$bd = new BD();
|
||
$resultado = $bd->execQuery($consulta);
|
||
// Procesamos las ofertas.
|
||
if (mysql_num_rows($resultado) == 0) {
|
||
$this->ofertas = array();
|
||
} else {
|
||
while ($rows = mysql_fetch_array($resultado)) {
|
||
$p = new Oferta($rows["oid"], $this->gestor);
|
||
$this->ofertas[] = $p;
|
||
}
|
||
}
|
||
|
||
return $this->ofertas;
|
||
}
|
||
|
||
/**
|
||
* Devuelve una lista de los posibles estados en los que se puede encontrar un pedido
|
||
* como Key => value, donde key es el cod del estado y value es el nombre del estado.
|
||
*/
|
||
function getEstadosOfertas() {
|
||
$consulta = "SELECT cod, nombre FROM candidaturas_estados";
|
||
$bd = new BD();
|
||
return $bd->keyValueQuery($consulta, "cod", "nombre");
|
||
}
|
||
|
||
/**
|
||
* Busca una oferta en función de su identificador.
|
||
*
|
||
* @return el pedido, en caso de encontrarlo y null en
|
||
* caso contrario.
|
||
*/
|
||
function buscarOferta($id) {
|
||
$lista = $this->getOfertas();
|
||
if ($lista) {
|
||
foreach ($lista as $elem) {
|
||
if ($elem->getValor("oid") == $id) {
|
||
return $elem;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function addOferta($campos) {
|
||
if (!$this->gestor->tieneRol(1) && !$this->gestor->tieneRol(3)) {
|
||
$error = "El usuario no tiene permisos para crear ofertas.";
|
||
throw new Exception($error);
|
||
}
|
||
// Calculamos el id
|
||
$referencia = $this->calculaReferencia($campos["pedido"]);
|
||
$inserto = "fecha, referencia";
|
||
$fecha = "'" . date("Y-m-d") . "'";
|
||
$valores = "$fecha, $referencia";
|
||
// Procesamos los datos
|
||
foreach ($campos as $key => $value) {
|
||
// Metemos todos los valores excepto el candidato, que se hace despu<70>s
|
||
if ($key != "candidato") {
|
||
$inserto .= ", $key";
|
||
$valores .= ", '$value'";
|
||
}
|
||
}
|
||
// Insertamos en la BD
|
||
$consulta = "INSERT INTO candidato_pedido ($inserto) VALUES ($valores)";
|
||
|
||
$bd = new BD();
|
||
if (!$bd->execQuery($consulta)) {
|
||
return "-1";
|
||
} else {
|
||
$id = mysql_insert_id();
|
||
$ofertaNueva = new Oferta($id, $this->gestor);
|
||
$mensaje = "Nueva oferta";
|
||
$ofertaNueva->actualizarHistorial($mensaje);
|
||
|
||
// Si viene ya el candidato indicado, hay que cambiarlo de estado
|
||
if (array_key_exists("candidato", $campos)) {
|
||
$ofertaNueva->colocarCandidato($campos["candidato"]);
|
||
}
|
||
}
|
||
return $id;
|
||
}
|
||
|
||
function calculaReferencia($solicitud) {
|
||
$bd = new BD();
|
||
$consulta = "select referencia from candidato_pedido where pedido = '$solicitud' order by oid desc limit 1";
|
||
|
||
if ($resultado = $bd->execQuery($consulta)) {
|
||
$rows = mysql_fetch_array($resultado);
|
||
|
||
$num = 1;
|
||
if (!empty($rows[0])) {
|
||
$num = substr($rows[0], strpos($rows[0], '/') + 1, strlen($rows[0]));
|
||
$num += 1;
|
||
}
|
||
return sprintf('\'%s/%s\'', $solicitud, $num);
|
||
} else {
|
||
return sprintf('\'%s/%s\'', $solicitud, 1);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
?>
|