Last commit for models/PurchaseModel.php: a5419847e16cb67dd58342fe218685c1efa999e8

Fix some issues with CreditConfigElement.php, a=chris

Chris Pollett [2015-08-31 17:Aug:st]
Fix some issues with CreditConfigElement.php, a=chris
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2015  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.orgs
 * @license http://www.gnu.org/licenses/ GPL3
 * @link http://www.seekquarry.com/
 * @copyright 2009 - 2015
 * @filesource
 */
namespace seekquarry\yioop\models;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;

/**
 *
 * @author Chris Pollett
 */
class PurchaseModel extends Model
{
    public function __construct($db_name = C\DB_NAME, $connect = true)
    {
        $db_class = C\NS_DATASOURCES . "Sqlite3Manager";
        $db_name = "ad_purchase";
        $create_flag = false;
        if(!file_exists(C\CRAWL_DIR . "/data/$db_name.db")) {
            $create_flag = true;
        }
        $this->db = new $db_class();
        $this->db->connect("", "", "", $db_name);
        if($create_flag) {
            $this->db->execute("CREATE TABLE PURCHASE (EMAIL VARCHAR(" .
                C\LONG_NAME_LEN . "), NAME_SERVER VARCHAR(" .
                C\MAX_URL_LEN . "), TIMESTAMP NUMERIC(" . C\TIMESTAMP_LEN . ")
                )");
        }
    }

    public function checkPurchase($email, $name_server)
    {
        $db = $this->db;
        $sql = "SELECT * FROM PURCHASE WHERE EMAIL = ? AND NAME_SERVER = ?";
        $params = [$email, $name_server];
        $result = $db->execute($sql, $params);
        if (!$row = $db->fetchArray($result)) {
            return false;
        }
        return true;
    }
    public function addPurchase($email, $name_server)
    {
        $db = $this->db;
        $sql = "INSERT INTO PURCHASE VALUES (?, ?, ?)";
        $time = time();
        $db->execute($sql, [$email, $name_server, $time]);
        return $time;
    }
}
ViewGit