/** * Cerca oggetti con le corrispondenze specificate * * @param array $_array La query associativa di ricerca tipo, provincia, dataInizio,dataFine, geo * @param string $_order Ordine espresso come SQL * @param Volontario $me volontario loggato * @return array Array di oggetti */ public static function ricerca($_array, $_order = null, Volontario $me = null) { global $db, $conf, $cache; if (false && $cache && static::$_versione == -1) { static::_caricaVersione(); } if ($_order) { $_order = 'ORDER BY ' . $_order; } $select = " "; $join = " "; $where = "WHERE 1"; if (!empty($_array["inizio"])) { $where .= " AND DATE_FORMAT(FROM_UNIXTIME(inizio), '%Y-%m-%d') > STR_TO_DATE(:inizio, '%Y-%m-%d') "; } /* if (!empty($_array["fine"])) { $where .= " AND DATE_FORMAT(FROM_UNIXTIME(tEsame), '%Y-%m-%d') < STR_TO_DATE(:fine, '%Y-%m-%d')"; } */ if (!empty($_array["type"])) { $typeArray = array_fill(0, count($_array["type"]), ':type'); foreach ($typeArray as $i => &$type_tmp) { $type_tmp = $type_tmp . "_" . $i; } $where .= " AND certificato IN (" . implode(',', $typeArray) . ")"; } if (!empty($_array["provincia"])) { $provArray = array_fill(0, count($_array["provincia"]), ':prov'); foreach ($provArray as $i => &$prov_tmp) { $prov_tmp = $prov_tmp . "_" . $i; } $where .= " AND provincia IN (" . implode(',', $provArray) . ")"; } if (!empty($_array["coords"]->latitude) && !empty($_array["coords"]->longitude)) { $where .= " AND st_distance(point(:long, :lat), geo) < 50"; } if (!empty($me)) { $select = ", g.data AS inizio, g.luogo AS luogoLezione"; $join .= " LEFT JOIN " . static::$_jt_lezioni . " g ON c.id = g.corso "; $select .= ", i.ruolo "; $join .= " RIGHT JOIN " . static::$_jt_iscrizioni . " i ON c.id = i.corso "; $where .= " AND i.volontario = :me"; } $sql = "SELECT c.* {$select} FROM " . static::$_t . " c {$join} {$where} {$_order}"; //print $sql; $hash = null; if (false && $cache && static::$_cacheable) { $hash = md5($sql); $r = static::_ottieniQuery($hash); if ($r !== false) { $cache->incr(chiave('__re')); return $r; } } $query = $db->prepare($sql); if (!empty($_array["inizio"])) { $query->bindParam(":inizio", $_array["inizio"], PDO::PARAM_STR); } /* if (!empty($_array["fine"])) { $query->bindParam(":fine", $_array["fine"], PDO::PARAM_STR); } */ if (!empty($_array["type"])) { foreach ($_array["type"] as $j => $t_tmp) { $query->bindParam(":type_" . $j, $t_tmp); } } if (!empty($_array["provincia"])) { foreach ($_array["provincia"] as $i => $p_tmp) { $query->bindParam(":prov_" . $i, $p_tmp); } } if (!empty($_array["coords"]->latitude) && !empty($_array["coords"]->longitude)) { $query->bindParam(":long", $_array["coords"]->longitude); $query->bindParam(":lat", $_array["coords"]->latitude); } if (!empty($me)) { $query->bindParam(":me", $me->id); } $query->execute(); $t = $c = []; while ($r = $query->fetch(PDO::FETCH_ASSOC)) { $tmp = new Corso($r['id'], $r); $t[] = $tmp; if (false) { $c[] = $r; } } if (false && $cache && static::$_cacheable) { static::_cacheQuery($hash, $c); } return $t; }
/** * Incrementa il numero di versione attuale, invalidando la cache * @return int Numero di versione attuale */ function incrementaVersioneCache() { global $cache, $_versione_cache; $_versione_cache++; $cache->incr(chiave('versione_cache', false), false); }
/** * Ottiene il nome di chiave con o senza numero di versione * @param string $suffisso Il suffisso della chiave * @param bool $conVersione Includere il numero di versione? * @return string La chiave completa */ protected static function _chiave($suffisso, $conVersione = true) { $c = chiave('e:' . static::$_t); if ($conVersione) { $c .= ':' . (int) static::$_versione; } $c .= ':' . $suffisso; return $c; }