<commit_before><?php

namespace bbn\Shop;

use bbn\X;
use bbn\Str;
use bbn\Models\Cls\Db as DbCls;
use bbn\Models\Tts\Dbconfig;
use bbn\Appui\Grid;
use bbn\Appui\Medias;
use bbn\Appui\Note;
use bbn\Appui\Cms;
use bbn\Appui\Option;
use bbn\Db;


class Client extends DbCls
{
  use Dbconfig;

  /**
   * @var Medias
   */
  private $medias;

  /**
   * @var Cms
   */
  private $cms;

  /**
   * @var Note
   */
  private $note;

  /**
   * @var Option
   */
  private $opt;

  /**
   * @var string
   */
  protected $lang;

  /**
   * @var string
   */
  protected $type_note;

  protected static $default_class_cfg = [
    'errors' => [
    ],
    'table' => 'bbn_shop_clients',
    'tables' => [
      'clients' => 'bbn_shop_clients',
      'clients_addresses' => 'bbn_shop_clients_addresses'
    ],
    'arch' => [
      'clients' => [
        'id' => 'id',
        'name' => 'name',
        'email' => 'email',
        'newsletter' => 'newsletter',
        'active' => 'active'
      ],
      'clients_addresses' => [
        'id' => 'id',
        'id_client' => 'id_client',
        'id_address' => 'id_address',
        'def' => 'def',
        'last' => 'last',
        'active' => 'active'
      ]
    ],
  ];

  public function __construct(Db $db, array $cfg = null)
  {
    // The database connection
    $this->db = $db;
    // Setting up the class configuration
    $this->_init_class_cfg($cfg);
    $this->opt   = Option::getInstance();
    $this->note  = new Note($this->db);
    $this->cms   = new Cms($this->db, $this->note);
    $this->media = new Medias($this->db);
    $this->media->setImageRoot('/image/');
  }

  public function add(string $email, bool $newsletter = false): ?string
  {
		if (!($idClient = $this->db->selectOne($this->class_table, $this->fields['id'], [$this->fields['email'] => $email]))
      && $this->db->insert($this->class_table, [
        $this->fields['email'] => $email,
        $this->fields['newsletter'] => empty($newsletter) ? 0 : 1
			])
    ) {
			$idClient = $this->db->lastId();
		}
		return $idClient;
	}
	public function addClientName(string $idClient, string $name, string $lastname){
		if($this->db->rselect($this->class_table, [], [$this->fields['id'] => $idClient])){
			if($this->db->update($this->class_table, [$this->fields['name'] => $name.' '.$lastname],[$this->fields['id'] => $idClient])){
				return $name.' '.$lastname;
			}
		}
	}
	
	public function addAddress(string $idClient, array $address): ?array
  {
    $opt = Option::getInstance();
    $entity = new \bbn\Entities\Address($this->db);
    $cfg = $entity->getClassCfg();
    $address[$cfg['arch']['addresses']['email']] = $this->getEmail($idClient);
		if(($name = $address['name']) && ($lastname = $address['lastname'])){
			$fullName = $this->addClientName($idClient, $name, $lastname);
			unset($address['name'], $address['lastname']);
		}
    $address[$cfg['arch']['addresses']['address']] = $address['address1'] . ' ' . $address['address2'];
    $country = $opt->option($address['country']);
    if ($idAddress = $entity->insert($address)) {
      if ($this->db->insert($this->class_cfg['tables']['clients_addresses'], [
        $this->class_cfg['arch']['clients_addresses']['id_client'] => $idClient,
        $this->class_cfg['arch']['clients_addresses']['id_address'] => $idAddress,
        $this->class_cfg['arch']['clients_addresses']['def'] => 1,
        $this->class_cfg['arch']['clients_addresses']['last'] =>  date('Y-m-d H:i:s')
      ])) {
        $newAddress = $this->getAddress($idAddress);
        $newAddress['continent'] = $country['continent'];
				$newAddress['fullName'] = $fullName;
        return $newAddress;
      }
    }
    return null;
	}

	protected function getEmail(STRING $idClient): ?string
	{
		return $this->db->selectOne($this->class_table, $this->fields['email'], [$this->fields['id'] => $idClient]);
	}

	protected function getAddress(string $idAddress): ?array
	{
    $entity = new \bbn\Entities\Address($this->db);
    $cfg = $entity->getClassCfg();
		return $entity->rselect([$cfg['arch']['addresses']['id'] => $idAddress]);
	}

}<commit_msg>fixes on shop client<commit_after><?php

namespace bbn\Shop;

use bbn\X;
use bbn\Str;
use bbn\Models\Cls\Db as DbCls;
use bbn\Models\Tts\Dbconfig;
use bbn\Appui\Grid;
use bbn\Appui\Medias;
use bbn\Appui\Note;
use bbn\Appui\Cms;
use bbn\Appui\Option;
use bbn\Db;


class Client extends DbCls
{
  use Dbconfig;

  /**
   * @var Medias
   */
  private $medias;

  /**
   * @var Cms
   */
  private $cms;

  /**
   * @var Note
   */
  private $note;

  /**
   * @var Option
   */
  private $opt;

  /**
   * @var string
   */
  protected $lang;

  /**
   * @var string
   */
  protected $type_note;

  protected $id_client;

  protected static $default_class_cfg = [
    'errors' => [
    ],
    'table' => 'bbn_shop_clients',
    'tables' => [
      'clients' => 'bbn_shop_clients',
      'clients_addresses' => 'bbn_shop_clients_addresses'
    ],
    'arch' => [
      'clients' => [
        'id' => 'id',
        'name' => 'name',
        'email' => 'email',
        'newsletter' => 'newsletter',
        'active' => 'active'
      ],
      'clients_addresses' => [
        'id' => 'id',
        'id_client' => 'id_client',
        'id_address' => 'id_address',
        'def' => 'def',
        'last' => 'last',
        'active' => 'active'
      ]
    ],
  ];

  public function __construct(Db $db, array $cfg = null)
  {
    // The database connection
    $this->db = $db;
    // Setting up the class configuration
    $this->_init_class_cfg($cfg);
    $this->opt   = Option::getInstance();
    $this->note  = new Note($this->db);
    $this->cms   = new Cms($this->db, $this->note);
    $this->media = new Medias($this->db);
    $this->media->setImageRoot('/image/');
  }

  public function getId(){
    return $this->id_client;
  }
  public function add(string $email, bool $newsletter = false): ?string
  {
		if (!($this->db->selectOne($this->class_table, $this->fields['id'], [$this->fields['email'] => $email]))
      && $this->db->insert($this->class_table, [
        $this->fields['email'] => $email,
        $this->fields['newsletter'] => empty($newsletter) ? 0 : 1
			])
    ) {
			$this->id_client = $this->db->lastId();
    }
		return $this->id_client;
	}
	public function addClientName(string $idClient, string $name, string $lastname){
		if($this->db->rselect($this->class_table, [], [$this->fields['id'] => $idClient])){
			if($this->db->update($this->class_table, [$this->fields['name'] => $name.' '.$lastname],[$this->fields['id'] => $idClient])){
				return $name.' '.$lastname;
			}
		}
	}
	
	public function addAddress(string $idClient, array $address): ?array
  {
    $opt = Option::getInstance();
    $entity = new \bbn\Entities\Address($this->db);
    $cfg = $entity->getClassCfg();
    $address[$cfg['arch']['addresses']['email']] = $this->getEmail($idClient);
		if(($name = $address['name']) && ($lastname = $address['lastname'])){
			$fullName = $this->addClientName($idClient, $name, $lastname);
			unset($address['name'], $address['lastname']);
		}
    $address[$cfg['arch']['addresses']['address']] = $address['address1'] . ' ' . $address['address2'];
    $country = $opt->option($address['country']);
    if ($idAddress = $entity->insert($address)) {
      if ($this->db->insert($this->class_cfg['tables']['clients_addresses'], [
        $this->class_cfg['arch']['clients_addresses']['id_client'] => $idClient,
        $this->class_cfg['arch']['clients_addresses']['id_address'] => $idAddress,
        $this->class_cfg['arch']['clients_addresses']['def'] => 1,
        $this->class_cfg['arch']['clients_addresses']['last'] =>  date('Y-m-d H:i:s')
      ])) {
        $newAddress = $this->getAddress($idAddress, $idClient);
        $newAddress['continent'] = $country['continent'];
				$newAddress['fullName'] = $fullName;
        return $newAddress;
      }
    }
    return null;
	}

	protected function getEmail(string $idClient): ?string
	{
		return $this->db->selectOne($this->class_table, $this->fields['email'], [$this->fields['id'] => $idClient]);
	}

	protected function getAddress(string $idAddress, string $idClient): ?array
	{
    $entity = new \bbn\Entities\Address($this->db);
    $cfg = $entity->getClassCfg();
    $cfgShop = $this->getClassCfg();

    $id = $this->db->selectOne($cfgShop['tables']['clients_addresses'], 'id', [
      $cfgShop['arch']['clients_addresses']['id_client'] => $idClient,
      $cfgShop['arch']['clients_addresses']['def'] => 1
    ]);
    $address = $entity->rselect([$cfg['arch']['addresses']['id'] => $idAddress]);
    $address['id_shop_address'] = $id;
   
		return $address;
	}

}
{'fraction_non_alphanumeric': 0.21705515808908377, 'fraction_numerical': 0.001269328409877683, 'mean_word_length': 3.299107142857143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5684858', 'n_tokens_mistral': 3117, 'n_tokens_neox': 3016, 'n_words': 513}