Example #1
0
 /**
  * Returns an array of instanced subclass of PCModelObject according to $name
  * @param PCMapper $mapper a subclass of PCMapper
  * @param array $keys a key value array of elements for filtering search in database
  * @param array $optionalAttributes an array of strings containing the names of the optional fields to fetch
  * @param boolean $useCache if use the cache or not
  * @param string $limit the sql query limit
  * @param string $order the order specification
  * @return array an array of instanced subclass of PCModelObject according to $name
  */
 public static function fetchModelObjectInstances($mapper, $keys, $optionalAttributes = null, $useCache = false, $limit = null, $order = NULL)
 {
     /* if(empty($keys)){
        throw new Exception("Illegal argument, \"keys\" is empty");
        } */
     if ($mapper == null) {
         throw new Exception("Mapper must not be null");
     }
     $required_field = $mapper->getRequiredAttributes();
     if ($useCache && isset($keys['identifier'])) {
         $identifier = $keys['identifier'];
         $cache = PCCache::cacheProvider();
         //error_log("GETTING : " . $mapper->getCacheKey($keys));
         $item = $cache->getItem($mapper->getCacheKey($keys));
         if (isset($item) && $item !== FALSE) {
             //error_log("GETTED : " . $mapper->getCacheKey($keys));
             return $item;
         }
     }
     $fields = NULL;
     //unisco parametri opzionali (se presenti) a parametri richiesti
     if ($optionalAttributes == null) {
         $fields = $required_field;
     } else {
         $fields = array_merge($optionalAttributes, $required_field);
     }
     //creo stringa da parametri separati da virgola
     $fields_string = implode(",", $fields);
     $table_name = $mapper->getTableName();
     $select_stm = "SELECT " . $fields_string . " FROM " . $table_name . " WHERE ";
     $prepared_keys_array = array();
     $count = 0;
     $tot = count($keys);
     foreach ($keys as $key => $value) {
         $count++;
         $placeHolder = ":{$key}";
         $select_stm .= $count == $tot ? " {$key} = {$placeHolder} " : " {$key} = {$placeHolder} AND ";
         $prepared_keys_array[$placeHolder] = $value;
     }
     if ($tot == NULL) {
         $select_stm .= " TRUE";
     }
     if (isset($order)) {
         $select_stm .= ' ORDER BY ' . $order;
     }
     if (isset($limit)) {
         $select_stm .= ' LIMIT ' . $limit;
     }
     $pdo = PCDatabase::getSharedDatabaseConnection();
     $prepared = $pdo->prepare($select_stm);
     if ($prepared->execute($prepared_keys_array) == FALSE) {
         $message = "Errore database: (" . $prepared->errorCode() . ") " . $prepared->errorInfo()[1] . " " . $prepared->errorInfo()[2];
         throw new Exception($message);
     }
     $result = null;
     $toReturn = array();
     while (($result = $prepared->fetch(PDO::FETCH_ASSOC)) != NULL) {
         $toReturn[] = $mapper->getMappedInstance($result);
     }
     if ($useCache && count($toReturn) == 1) {
         $identifier = $keys['identifier'];
         //error_log("STORING: " . $mapper->getCacheKey($keys));
         PCCache::cacheProvider()->setItem($toReturn, $mapper->getCacheKey($keys), 300);
     }
     $prepared = NULL;
     return $toReturn;
 }
Example #2
0
    /**
     * XXX evitare utilizzo diretto del database
     * @param array $attributes
     * @param string $error
     * @return boolean
     */
    public static function createUserWithAttributes($attributes, &$error) {
        $username = $attributes['username'];
        $name = $attributes['name'];
        $surname = $attributes['surname'];
        $email = $attributes['email'];
        $password = $attributes['password'];

        if (static::validateName($name) == false) {
            $error = "Invalid name";
            return false;
        }
        if (static::validateSurname($surname) == false) {
            $error = "Invalid surname";
            return false;
        }
        if (static::validateUsername($username) == FALSE) {
            $error = "username is not valid (min 5, max 20 chars)";
            return false;
        }
        if (static::validateMail($email) == FALSE) {
            $error = "email already registered";
            return FALSE;
        }

        $mapper = PCModelUser::getMapper();

        $pdo = PCDatabase::getSharedDatabaseConnection();

        $select = "SELECT username ,email FROM " . $mapper->getTableForInsertUpdate() . " WHERE (username = :uname OR email = :mail) AND account_type = :type;";
        $prepared = $pdo->prepare($select);

        if ($prepared === FALSE) {
            c_dump($prepared->errorInfo());
            return FALSE;
        }

        $result = $prepared->execute(array(':uname' => $username, ':mail' => $email, ':type' => PCModelUser::$TYPE_DEFAULT));

        if ($result === FALSE) {
            ob_start();
            print_r($prepared->errorInfo());
            $prepared->debugDumpParams();
            $contents = ob_get_contents();
            ob_end_clean();
            error_log($contents);
            return FALSE;
        }

        while ($item = $prepared->fetch(PDO::FETCH_ASSOC)) {

            if (strcmp($item['email'], $email) == 0) {
                $error = "email already registered";
                return FALSE;
            } else if (strcmp($item['username'], $username) == 0) {
                $error = "username already registered";
                return FALSE;
            }
        }

        $date = new DateTime('now', new DateTimeZone('UTC'));
        

        $keys = array(
            'creation_date' => $date->format('Y-m-d H:i:s'),
            'username' => $username,
            'penalities' => '0',
            'surname' => $surname,
            'name' => $name,
            'email' => $email,
            'password' => $password
        );



        return PCModelManager::insertObject($mapper, $keys);
    }
Example #3
0
 /**
  * XXX rimuovere utilizzo diretto db
  * @param string $user_identifier
  * @return array
  */
 public static function getUserAverageAndCount($user_identifier){
     
     $mapper = PCModelReview::getMapper();
     
     $pdo = PCDatabase::getSharedDatabaseConnection();
     $select = "SELECT avg(usability) as usability, avg(reliability) as reliability,";
     $select .= " avg(contents) as contents, count(contents) as tot FROM ".$mapper->getTableName();
     $select .= " WHERE user_identifier = :id ;";
     
     $prepared = $pdo->prepare($select);
     
     
     $result = $prepared->execute(array(':id'=> $user_identifier));
     
     if($result === FALSE){
         
         return NULL;
     }
     
     $item = $prepared->fetch(PDO::FETCH_ASSOC);
     
     if(!isset($item)){
        
         return NULL;
     }
     
     $usability =(double) $item['usability'];
     $reliability = (double)$item['reliability'];
     $contents = (double)$item['contents'];
     $tot = (int)$item['tot'];
     $avg = ($usability + $reliability + $contents )/3.0;
     
     return array(
         'avg'=>$avg, 'tot'=>$tot,
         'usability'=>$usability,
         'reliability'=>$reliability,
         'contents'=>$contents
             );
     
 }
Example #4
0
 /**
  * @XXX remove direct database interaction
  * @param PCModelWebsite $site
  */
 public static function recacheSiteReview($site)
 {
     //error_log('RECACHING SITE INFO: '.$site->getIdentifier());
     $select = "SELECT avg(usability) as usability, avg(reliability) as reliability,";
     $select .= " avg(contents) as contents, count(identifier) as count ";
     $mapper = PCModelReview::getMapper();
     $select .= " FROM " . $mapper->getTableName() . " WHERE site_identifier = :id";
     $pdo = PCDatabase::getSharedDatabaseConnection();
     $prepared = $pdo->prepare($select);
     $result = $prepared->execute(array(":id" => $site->getIdentifier()));
     if ($result === FALSE) {
         return NULL;
     }
     $item = $prepared->fetch(PDO::FETCH_ASSOC);
     if (!isset($item)) {
         return NULL;
     }
     $usability = (double) $item['usability'];
     $reliability = (double) $item['reliability'];
     $contents = (double) $item['contents'];
     $count = (double) $item['count'];
     $cache_time = new DateTime('now', new DateTimeZone('UTC'));
     $keys = array('usability' => $usability, 'reliability' => $reliability, 'contents' => $contents, 'number_of_votes' => $count, 'cached' => $cache_time->format('Y-m-d H:i:s'));
     $condition = "identifier = :id";
     $bindings = array(':id' => $site->getIdentifier());
     $websiteMapper = PCModelWebsite::getMapper();
     if (PCModelManager::updateObject($websiteMapper, $keys, $condition, $bindings)) {
         $site->cached_date = $cache_time;
         $site->contents = $contents;
         $site->number_of_votes = $count;
         $site->reliability = $reliability;
         $site->usability = $usability;
         PCCache::cacheProvider()->setItem($site, $websiteMapper->getTableName() . $site->getIdentifier());
         return $site;
     }
     return NULL;
 }