File "user.class.php"

Full Path: /srv/www/www.cadoro.it/src/models/user.class.php
File size: 31.28 KB
MIME-type: text/x-php
Charset: utf-8

<?php

  namespace models;

  class User extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO users (".
                        "uniqid, ".
                        "card, ".
                        "lastname, ".
                        "firstname, ".
                        "email, ".
                        "password, ".
                        "address,".
                        "address_no,".
                        "city,".
                        "province,".
                        "zipcode,".
                        "country,".
                        "mobile,".
                        "phone,".
                        "birthday,".
                        "gender,".
                        "active,".
                        "privacy,".
                        "privacy1,".
                        "privacy2,".
                        "privacy3,".
                        "newsletter,".
                        "store,".
                        "created_at".
                      ") VALUES (".
                        _text(_a($params, 'uniqid', uniqid())) . ", ".
                        _text(_a($params, 'card')) . ", ".
                        _text(_a($params, 'lastname')) . ", ".
                        _text(_a($params, 'firstname')) . ", ".
                        _text(_a($params, 'email')) . ", ".
                        _password_sha256(_a($params, 'password')) . ", ".
                        _text(_a($params, 'address')) . ", ".
                        _text(_a($params, 'address_no')) . ", ".
                        _text(_a($params, 'city')) . ", ".
                        _text(_a($params, 'province')) . ", ".
                        _text(_a($params, 'zipcode')) . ", ".
                        _text(_a($params, 'country')) . ", ".
                        _text(_a($params, 'mobile')) . ", ".
                        _text(_a($params, 'phone')) . ", ".
                        _date(_a($params, 'birthday')) . ", ".
                        _text(_a($params, 'gender')) . ", ".
                        _integer(_a($params, 'active')) . ", ".
                        _integer(_a($params, 'privacy')) . ", ".
                        _integer(_a($params, 'privacy1')) . ", ".
                        _integer(_a($params, 'privacy2')) . ", ".
                        _integer(_a($params, 'privacy3')) . ", ".
                        _integer(_a($params, 'newsletter')) . ", ".
                        _integer(_a($params, 'store')) . ", ".
                        "NOW()".
                      ");") 
                      or die("query error in User::add: " . mysqli_error($db));
      $id =  mysqli_insert_id($db);
      $user = User::get_by_id($id);
      // $user->newsletter();
      if ($user['card']) {
        \classes\Fidelity::SetAnagrafica($user['card'], '01', $user);
      }
      return $id;
    }

    static public function get_by_id($id) {
      global $db;
      $rs = mysqli_query($db, "SELECT * FROM users WHERE id = " . _integer($id) . ";") or
            die("query error in User::get_by_id: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new User($r);
      }
      return null;
    }

    static public function get_by_card($card) {
      global $db;
      $rs = mysqli_query($db, "SELECT * FROM users WHERE card = " . _text($card) . ";") or
            die("query error in User::get_by_card: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new User($r);
      }
      return null;
    }

    static public function get_by_email($email) {
      global $db;
      $rs = mysqli_query($db, "SELECT * FROM users WHERE email = " . _text($email) . ";") or
            die("query error in User::get_by_email: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new User($r);
      }
      return null;
    }

    static public function get($params=array(), $offset=0, $limit=25) {
      global $db;
      $where = User::get_where($params);
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM users ".
                        "WHERE 1 = 1 $where ".
                        "ORDER BY lastname, firstname ".
                        "LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or
            die("query error in User::get: " . mysqli_error($db));
      $results = array();
      while ($r = mysqli_fetch_assoc($rs)) {
        array_push($results, new User($r));
      }
      return $results;
    }

    static public function get_count($params) {
      global $db;
      $where = User::get_where($params);
      $rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
                        "FROM users ".
                        "WHERE 1 = 1 $where;") or
            die("query error in User::get_count: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      return $r['count'];
    }

    static public function get_where($params) {
      $where = "";
      if (isset($params['q'])) {
        $where .= "AND (firstname like " . _text('%' . $params['q'] . '%') . " OR ".
                       "lastname like " . _text('%' . $params['q'] . '%') . " OR ".
                       "card like " . _text('%' . $params['q'] . '%') . " OR ".
                       "email like " . _text('%' . $params['q'] . '%') . ") ";
      }
      if (isset($params['store']) && $params['store']) {
        $where .= "AND (store = " . _integer($params['store']) . ") ";
      }
      if (isset($params['filter']) && $params['filter'] == 'app') {
        $where .= "AND (app_details IS NOT NULL AND app_details <> '[]') ";
      }
      return $where;
    }

    static public function get_vocabulary($params=array()) {
      $result = array();
      foreach (User::get($params) as $user) {
        $result[$user['id']] = $user['lastname'] . " " . $user['firstname'];
      }
      return $result;
    }

    static public function identify() {
      global $_COOKIE;
      if (isset($_COOKIE[COOKIE_NAME])) {
        $parts = explode("-", $_COOKIE[COOKIE_NAME]);
        if (count($parts) == 2 && ctype_digit($parts[0]) && md5($parts[0] . COOKIE_SECRET)) {
          $user = User::get_by_id($parts[0]);
          return $user && $user['active'] && !$user['deleted_at'] ? $user : null;
        }
      }
    }

    public function get_points() {
      $result = $this->get_anagrafica();
      return _a($result, 'points', 0);
    }

    public function get_points_pending() {
      $result = $this->get_anagrafica();
      return _a($result, 'points_pending', 0);
    }

    public function get_points_migrations() {
      $result = \classes\Fidelity::GetMigrazione($this['card']);
      return _a($result, 'punti_cancellati', 0);
    }

    public function get_store() {
      return Store::get_by_id($this['store']);
    }

    static public function get_anagrafica_from_card($card) {
      $result = \classes\Fidelity::GetTessera($card);
      return $result && !_a($result, 'Errore') ? array(
        "card" => trim(_a($result, "codice")),
        "firstname" => trim(_a($result, "nome")),
        "lastname" => trim(_a($result, "cognome")),
        "email" => trim(_a($result, "mail")),
        "address" => trim(_a($result, "indirizzo")),
        "address_no" => trim(_a($result, "num")),
        "city" => trim(_a($result, "citta")),
        "zipcode" => trim(_a($result, "cap")),
        "province" => trim(_a($result, "provincia")),
        "phone" => trim(_a($result, "telefono")),
        "mobile" => trim(_a($result, "cellulare")),
        "gender" => trim(_a($result, "sesso")),
        "birthday" => substr(_a(_a($result, "data_nascita"), "date"), 0, 10),
        "points" => trim(_a($result, "punti_correnti")),
        "points_pending" => trim(_a($result, "punti_sospesi")),
        "store" => trim(_a($result, "negozio"))*1,
        "stato" => trim(_a($result, "stato")),
        "privacy1" => _a($result, 'accetta_posta') == 'S' ? 1 : 0,
        "privacy2" => _a($result, 'accetta_profilazione') == 'S' ? 1 : 0,
        "privacy3" => _a($result, 'accetta_ricerche') == 'S' ? 1 : 0,
        "dipendente" => _a($result, 'dipendente') == 'D' ? 1 : 0,
      ) : array();
    }

    public function get_anagrafica() {
      return User::get_anagrafica_from_card($this['card']);
    }

    public function forget() {
      setcookie(COOKIE_NAME, 'content', 1, "/", COOKIE_DOMAIN);
    }

    public function remember() {
      setcookie(COOKIE_NAME, $this['id'] . "-" . md5($this['id'] . COOKIE_SECRET), time()+365*24*3600, "/", COOKIE_DOMAIN);
    }

    public function delete() {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "deleted_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::delete: " . mysqli_error($db));
    }

    public function undelete() {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "deleted_at = NULL ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::undelete: " . mysqli_error($db));
    }

    public function email_password() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $smarty->assign("user", $this);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      $mail->Subject = "Recupero password";
      $mail->MsgHTML($smarty->fetch("email-password.tmpl"));
      $mail->Send();
    }

    public function email_cardoro_optin() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $store = Store::get_by_id($this['store']);
      $smarty->assign("user", $this);
      $smarty->assign("store", $store);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      $mail->Subject = "Conferma di richiesta CARD'ORO";
      $mail->MsgHTML($smarty->fetch("email-cardoro-optin.tmpl"));
      $mail->Send();
    }

    public function email_cardoro() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $store = Store::get_by_id($this['store']);
      $smarty->assign("user", $this);
      $smarty->assign("store", $store);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      //if (!DEBUG && $store['email']) {
      //  $mail->AddCc($store['email']);
      //}
      $mail->Subject = "Ricevuta di richiesta CARD'ORO";
      $mail->MsgHTML($smarty->fetch("email-cardoro.tmpl"));
      $mail->Send();
    }

    public function email_cardoro_conferma() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $store = Store::get_by_id($this['store']);
      $smarty->assign("user", $this);
      $smarty->assign("store", $store);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      $mail->Subject = "Conferma di attivazione CARD'ORO";
      $mail->MsgHTML($smarty->fetch("email-cardoro-conferma.tmpl"));
      $mail->Send();
    }

    public function email_registrazione() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $smarty->assign("user", $this);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      $mail->Subject = "Conferma di registrazione";
      $mail->MsgHTML($smarty->fetch("email-registrazione.tmpl"));
      $mail->Send();
    }

    public function email_optin() {
      $smarty = new \Smarty();
      $smarty->setTemplateDir('templates/');
      $smarty->setCompileDir('templates_c/');
      $smarty->setConfigDir('config/');
      $smarty->setCacheDir('cache/');

      $smarty->assign("user", $this);
      $smarty->assign("base_url", BASE_URL);

      $mail = new \PHPMailer();
      $mail->IsSMTP(); 
      $mail->Host = SMTP_HOST;
      $mail->CharSet = 'utf-8';
      $mail->Encoding = '8bit';
      $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
      $mail->AddAddress($this['email'], $this['firstname'] . " " . $this['lastname']);
      $mail->Subject = "Conferma di modifica indirizzo email";
      $mail->MsgHTML($smarty->fetch("email-optin.tmpl"));
      $mail->Send();
    }

    public function get_shopping_list() {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM users_shopping_list ".
                        "WHERE deleted_at IS NULL AND user_id = " . _integer($this['id']) . " ".
                        "ORDER BY created_at DESC;") or
            die("query error in User::get_favourites: " . mysqli_error($db));
      $result = array();
      while ($r = mysqli_fetch_assoc($rs)) {
        array_push($result, new UserShoppingList($r));
      }
      return $result;
    }

    public function get_favourites() {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM users_favourites ".
                        "WHERE deleted_at IS NULL AND user_id = " . _integer($this['id']) . " ".
                        "ORDER BY created_at DESC;") or
            die("query error in User::get_favourites: " . mysqli_error($db));
      $result = array();
      while ($r = mysqli_fetch_assoc($rs)) {
        array_push($result, new UserFavourite($r));
      }
      return $result;
    }

    public function get_favourites_count() {
      global $db;
      $rs = mysqli_query($db, "SELECT COUNT(*) as count ".
                        "FROM users_favourites ".
                        "WHERE deleted_at IS NULL AND user_id = " . _integer($this['id']) . " ".
                        "ORDER BY created_at DESC;") or
            die("query error in User::get_favourites: " . mysqli_error($db));
      if ($r = mysqli_fetch_assoc($rs)) {
        return $r['count'];
      }
      return 0;
    }

    public function get_favourite_by_type_and_id($type, $id) {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM users_favourites ".
                        "WHERE deleted_at IS NULL ".
                          "AND user_id = " . $this['id'] . " ".
                          "AND content_type = " . _text($type) . " ".
                          "AND content_id = " . _integer($id) . ";") or
            die("query error in User::get_favourite_by_type_and_id: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new UserFavourite($r);
      }
    }

    public function add_favourite($type, $id) {
      if ($this->get_favourite_by_type_and_id($type, $id)) {
        return;
      }
      global $db;
      mysqli_query($db, "INSERT INTO users_favourites (".
                    "user_id,".
                    "content_type,".
                    "content_id,".
                    "created_at) ".
                  "VALUES (".
                    _integer($this['id']) . ", " .
                    _text($type) . ", " .
                    _integer($id) . ", " .
                    "NOW());") or
                  die("query error in User::add_product: " . mysqli_error($db));
    }

    public function remove_favourite($type, $id) {
      global $db;
      mysqli_query($db, "UPDATE users_favourites SET deleted_at = NOW() ".
                  "WHERE user_id = " . $this['id'] . " ".
                    "AND content_type = " . _text($type) . " ".
                    "AND content_id = " . _integer($id) . ";") or
                  die("query error in User::remove_favourite: " . mysqli_error($db));
    }

    static public function password_policy($user, $password) {
      return strlen($password) >= 6 &&
             preg_match('/[a-zA-Z]/', $password) &&
             preg_match('/[0-9]/', $password)/* &&
             (!$user['lastname'] || strstr(strtolower($password), strtolower($user['lastname'])) == FALSE) &&
             (!$user['firstname'] || strstr(strtolower($password), strtolower($user['firstname'])) == FALSE) &&
             (!$user['email'] || strstr(strtolower($password), strtolower($user['email'])) == FALSE) */;
    }

    public function pdf() {
      $pdf = new \FPDI();
      $pdf->SetAutoPageBreak(false, 0);
      $pdf->setSourceFile("pdf/richiesta-cardoro.pdf");

      $pdf->AddPage();
      $tplIdx = $pdf->importPage(1, '/MediaBox');
      $pdf->useTemplate($tplIdx, 0, 0, 210);

      $azienda = null;
      $negozi = \classes\Fidelity::Negozi();
      if($negozi !== NULL){
        foreach ($negozi as $n) {
          if ($n['negozio'] == $this['store']) {
            $azienda = $n['azienda'];
            break;
          }
        }
      }

      if ($azienda) {
        foreach (\classes\Fidelity::Aziende() as $a) {
          if ($a['azienda'] == $azienda) {
            $azienda = $a;
          }
        }
      }

      if (!is_array($azienda)) {
        $azienda = null;
      }

      $pdf->SetFont('Courier', '', 14);

      //Cognome
      $pdf->SetXY(26, 47.5);
      $pdf->MultiCell(70, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['lastname'])));

      //Nome
      $pdf->SetXY(107, 47.5);
      $pdf->MultiCell(70, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['firstname'])));

      //Sesso
      $pdf->SetXY($this['gender'] == 'M' ? 184 : 190, 49.5);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));

      //Data di nascita
      $pdf->SetXY(32, 55);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper(str_replace("/", "  ", _dd($this['birthday'])))));

      //Indirizzo
      $pdf->SetXY(85, 55);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['address'])));

      //Numero
      $pdf->SetXY(180, 55);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['address_no'])));

      //Comune
      $pdf->SetXY(26, 61);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['city'])));
    
      //Provincia
      $pdf->SetXY(158.5, 61);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['province'])));

      //CAP
      $pdf->SetXY(180, 61);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['zipcode'])));

      //Telefono
      $pdf->SetXY(26, 67);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['mobile'] ? $this['mobile'] : $this['phone'])));

      //Email
      $pdf->SetXY(85, 67);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper($this['email'])));

      /*
      $pdf->SetXY(32, 91.5);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", mb_strtoupper(str_replace("/", "  ", _dd($this['birthday'])))));
      */

      //Nuova tessera
      $pdf->SetXY(14, 79);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));
    
      $pdf->SetFont('Courier', '', 9);
    
      //Informativa privacy letta
      $pdf->SetXY(14, 255.5);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));
      
      //Privacy 1
      $pdf->SetXY($this['privacy1'] == '1' ? 14 : 21.5, 260);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));
      
      //Privacy 2
      $pdf->SetXY($this['privacy2'] == '1' ? 14 : 21.5, 265);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));
      
      //Privacy 3
      $pdf->SetXY($this['privacy3'] == '1' ? 14 : 21.5, 269.5);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", "X"));
      
      //Data prima pagina
      $pdf->SetXY(25, 276);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", date('d/m/Y')));

      $pdf->AddPage();
      $tplIdx = $pdf->importPage(2, '/MediaBox');
      $pdf->useTemplate($tplIdx, 0, 0, 210);

      //Date seconda pagina
      $pdf->SetXY(25, 256);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", date('d/m/Y')));

      $pdf->SetXY(25, 275);
      $pdf->MultiCell(180, 6.0, iconv("UTF-8", "ISO-8859-15//TRANSLIT", date('d/m/Y')));

      return $pdf;
    }

    public function newsletter() {
      return _newsletter(array(
        "external_id" => $this['card'],
        "firstname" => $this['firstname'],
        "lastname" => $this['lastname'],
        "email" => $this['email'],
        "address" => $this['address'] . " " . $this['address_no'],
        "zipcode" => $this['zipcode'],
        "city" => $this['city'],
        "province" => $this['province'],
        "store" => $this['store'],
        "privacy1" => $this['privacy1']*1,
        "privacy2" => $this['privacy2']*1,
        "privacy3" => $this['privacy3']*1,
        "status" => 1,
      ));
    }

    public function update($params, $ws=false) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "card = " . _text(_a($params, 'card')) . ", ".
                        "lastname = " . _text(_a($params, 'lastname')) . ", ".
                        "firstname = " . _text(_a($params, 'firstname')) . ", ".
                        "email = " . _text(_a($params, 'email')) . ", ".
                        "password = " . _password_sha256(_a($params, 'password')) . ", ".
                        "address = " . _text(_a($params, 'address')) . ", ".
                        "address_no = " . _text(_a($params, 'address_no')) . ", ".
                        "city = " . _text(_a($params, 'city')) . ", ".
                        "province = " . _text(_a($params, 'province')) . ", ".
                        "zipcode = " . _text(_a($params, 'zipcode')) . ", ".
                        "country = " . _text(_a($params, 'country')) . ", ".
                        "mobile = " . _text(_a($params, 'mobile')) . ", ".
                        "phone = " . _text(_a($params, 'phone')) . ", ".
                        "birthday = " . _date(_a($params, 'birthday')) . ", ".
                        "gender = " . _text(_a($params, 'gender')) . ", ".
                        "privacy = " . _integer(_a($params, 'privacy')) . ", ".
                        "privacy1 = " . _integer(_a($params, 'privacy1')) . ", ".
                        "privacy2 = " . _integer(_a($params, 'privacy2')) . ", ".
                        "privacy3 = " . _integer(_a($params, 'privacy3')) . ", ".
                        "newsletter = " . _integer(_a($params, 'newsletter')) . ", ".
                        "store = " . _integer(_a($params, 'store')) . ", ".
                        "dipendente = " . _integer(_a($params, 'dipendente')) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update: " . mysqli_error($db));
      $user = User::get_by_id($this['id']);
      $user->newsletter();
      //
      if ($ws) {
        $u = User::get_by_id($this['id']);
        \classes\Fidelity::SetAnagrafica($u['card'], '12', $u);
      }
    }

    public function update_admin($params) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "card = " . _text(_a($params, 'card')) . ", ".
                        "lastname = " . _text(_a($params, 'lastname')) . ", ".
                        "firstname = " . _text(_a($params, 'firstname')) . ", ".
                        "email = " . _text(_a($params, 'email')) . ", ".
                        "password = " . _password_sha256(_a($params, 'password')) . ", ".
                        "address = " . _text(_a($params, 'address')) . ", ".
                        "address_no = " . _text(_a($params, 'address_no')) . ", ".
                        "city = " . _text(_a($params, 'city')) . ", ".
                        "province = " . _text(_a($params, 'province')) . ", ".
                        "zipcode = " . _text(_a($params, 'zipcode')) . ", ".
                        "country = " . _text(_a($params, 'country')) . ", ".
                        "mobile = " . _text(_a($params, 'mobile')) . ", ".
                        "phone = " . _text(_a($params, 'phone')) . ", ".
                        "birthday = " . _date(_a($params, 'birthday')) . ", ".
                        "gender = " . _text(_a($params, 'gender')) . ", ".
                        "privacy = " . _integer(_a($params, 'privacy')) . ", ".
                        "privacy1 = " . _integer(_a($params, 'privacy1')) . ", ".
                        "privacy2 = " . _integer(_a($params, 'privacy2')) . ", ".
                        "privacy3 = " . _integer(_a($params, 'privacy3')) . ", ".
                        "newsletter = " . _integer(_a($params, 'newsletter')) . ", ".
                        "active = " . _integer(_a($params, 'active')) . ", ".
                        "store = " . _integer(_a($params, 'store')) . ", ".
                        "app_token = " . _text($params['app_token']) . ", ".
                        "app_notifications = " . _integer($params['app_notifications']) . ", ".
                        "app_details = " . _text_json($params['app_details']) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update: " . mysqli_error($db));
      //
      $u = User::get_by_id($this['id']);
      \classes\Fidelity::SetAnagrafica($u['card'], $this['card'] ? '12' : '02', $u);
      if (!_a($u['data'], 'first-login')) {
        $res = \classes\Fidelity::SetPunti($u['card'], '02');
        if (!_a($res, 'Errore') || $res['Errore'] == 3) {
          $u['data']['first-login'] = date('Y-m-d H:i:s');
          $u->update_data($u['data']);
        }
      }
    }

    public function update_card($params) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "card = " . _text($params['card']) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update_card: " . mysqli_error($db));
    }

    public function update_active($active) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "active = " . _integer($active) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update_active: " . mysqli_error($db));
      // Aggiorno dati fmail
      $user = User::get_by_id($this['id']);
      $user->newsletter();
    }

    public function update_store($store) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "store = " . _integer($store) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update_store: " . mysqli_error($db));
    }

    public function update_data($data) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "data = " . _text_json($data) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update_data: " . mysqli_error($db));
    }

    public function update_password($password) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                        "password = " . _password_sha256($password) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in User::update_password: " . mysqli_error($db));
    }

    public function update_app($params) {
      global $db;
      mysqli_query($db, "UPDATE users SET ".
                    "app_token = " . _text($params['app_token']) . ", ".
                    "app_notifications = " . _integer($params['app_notifications']) . ", ".
                    "app_details = " . _text_json($params['app_details']) . ", ".
                    "updated_at = " . "NOW() ".
                  "WHERE id = " . _integer($this['id']) . ";") or
                  die("query error in User::update_app: " . mysqli_error($db));
    }

    public function json() {
      return array(
        'id' => (int)$this['id'],
        'card' => $this['card'],
        'firstname' => $this['firstname'],
        'lastname' => $this['lastname'],
        'email' => $this['email'],
        'address' => $this['address'],
        'address_no' => $this['address_no'],
        'city' => $this['city'],
        'province' => $this['province'],
        'zipcode' => $this['zipcode'],
        'country' => $this['country'],
        'mobile' => $this['mobile'],
        'phone' => $this['phone'],
        'birthday' => $this['birthday'],
        'gender' => $this['gender'],
        'active' => (int)$this['active'],
        'privacy' => (int)$this['privacy'],
        'privacy1' => (int)$this['privacy1'],
        'privacy2' => (int)$this['privacy2'],
        'privacy3' => (int)$this['privacy3'],
        'newsletter' => (int)$this['newsletter'],
        'store' => (int)$this['store'],
        'app_notifications' => (int)$this['app_notifications'],
        'points' => (int)$this->get_points(),
        'points_pending' => (int)$this->get_points_migrations(),
        //'points_pending' => 0,
        'first_app' => !_a($this['data'], 'first-app') ? true : false,
        'app_version' => $this['app_version'],
        'dipendente' => $this['dipendente'],
      );
    }

    function __construct($value) {
      parent::__construct($value);
      $this['data'] = $this['data'] ? json_decode($this['data'], true) : array();
      $this['first_app'] = !_a($this['data'], 'first-app') ? true : false;
      $this['app_details'] = $this['app_details'] ? json_decode($this['app_details'], true) : array();
      $this['app_version'] = $this['app_details'] ? $this['app_details']['app'] : "";
    }

  }

?>