<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 Product 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_products',
    'tables' => [
      'products' => 'bbn_shop_products'
    ],
    'arch' => [
      'products' => [
        'id' => 'id',
        'id_provider' => 'id_provider',
        'id_note' => 'id_note',
        'id_main' => 'id_main',
        'price' => 'price',
        'price_purchase' => 'price_purchase',
        'dimensions' => 'dimensions',
        'weight' => 'weight',
        'product_type' => 'product_type',
        'id_edition' => 'id_edition',
        'id_main' => 'id_main',
        'stock' => 'stock',
        'front_img' => 'front_img',
        'active' => 'active',
        'cfg' => 'cfg'
      ],
    ],
  ];

  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 getTypeNote()
  {
    if (!$this->type_note) {
      $o = Option::getInstance();
      $this->type_note = $o->fromCode('products', 'types', 'note', 'appui');
    }

    return $this->type_note;
  }


  public function getTypes()
  {
    return $this->opt->options('product_types');
  }


  public function getEditions()
  {
    return $this->opt->options('editions');
  }


  public function exists(string $id)
  {
    $cfg = $this->getClassCfg();
    return (bool)$this->db->count($cfg['table'], [$cfg['arch']['products']['id'] => $id]);
  }


  public function getByUrl(string $url, $public = true)
  {
    $cfg = $this->getClassCfg();
    if (
      ($id_note = $this->note->urlToId($url))
      && ($id = $this->db->selectOne($cfg['table'], $cfg['arch']['products']['id'], [$cfg['arch']['products']['id_note'] => $id_note]))
    ) {
      $all = $this->get($id);
      if ($public) {
        unset($all[$cfg['arch']['products']['price_purchase']]);
        $all['stock'] = (bool)$all['stock'];
      }

      return $all;
    }
  }


  public function get(string $id): ?array
  {
    if ($this->exists($id)) {
      $cfg  = $this->getClassCfg();
      $res  = $this->db->rselect($cfg['table'], [], [$cfg['arch']['products']['id'] => $id]);
      $note = $this->cms->get($res['id_note'], true);
      $final = array_merge($note, $res);
      $final['variants'] = $this->getVariantsList($final);
      return $final;
    }

    return null;
  }


  public function add($data)
  {
    $cfg  = $this->getClassCfg();
    $a = $cfg['arch']['products'];
    $noteCfg = $this->note->getClassCfg();
    if (!X::hasProps($data, [$a['product_type'], $a['id_provider'], 'url', $noteCfg['arch']['versions']['title']])) {
      throw new \Exception(_("Some mandatory fields are missing"));
    }

    if (!($type = $this->opt->fromCode('products', 'types', 'note', 'appui'))) {
      throw new \Exception(_("Impossible to retrieve the product type"));
    }
<commit_msg>Updated option path after move to plugins<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 Product 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_products',
    'tables' => [
      'products' => 'bbn_shop_products'
    ],
    'arch' => [
      'products' => [
        'id' => 'id',
        'id_provider' => 'id_provider',
        'id_note' => 'id_note',
        'id_main' => 'id_main',
        'price' => 'price',
        'price_purchase' => 'price_purchase',
        'dimensions' => 'dimensions',
        'weight' => 'weight',
        'product_type' => 'product_type',
        'id_edition' => 'id_edition',
        'id_main' => 'id_main',
        'stock' => 'stock',
        'front_img' => 'front_img',
        'active' => 'active',
        'cfg' => 'cfg'
      ],
    ],
  ];

  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 getTypeNote()
  {
    if (!$this->type_note) {
      $o = Option::getInstance();
      $this->type_note = $o->fromCode('products', 'types', 'note', 'plugins', 'shop', 'appui');
    }

    return $this->type_note;
  }


  public function getTypes()
  {
    return $this->opt->options('product_types');
  }


  public function getEditions()
  {
    return $this->opt->options('editions');
  }


  public function exists(string $id)
  {
    $cfg = $this->getClassCfg();
    return (bool)$this->db->count($cfg['table'], [$cfg['arch']['products']['id'] => $id]);
  }


  public function getByUrl(string $url, $public = true)
  {
    $cfg = $this->getClassCfg();
    if (
      ($id_note = $this->note->urlToId($url))
      && ($id = $this->db->selectOne($cfg['table'], $cfg['arch']['products']['id'], [$cfg['arch']['products']['id_note'] => $id_note]))
    ) {
      $all = $this->get($id);
      if ($public) {
        unset($all[$cfg['arch']['products']['price_purchase']]);
        $all['stock'] = (bool)$all['stock'];
      }

      return $all;
    }
  }


  public function get(string $id): ?array
  {
    if ($this->exists($id)) {
      $cfg  = $this->getClassCfg();
      $res  = $this->db->rselect($cfg['table'], [], [$cfg['arch']['products']['id'] => $id]);
      $note = $this->cms->get($res['id_note'], true);
      $final = array_merge($note, $res);
      $final['variants'] = $this->getVariantsList($final);
      return $final;
    }

    return null;
  }


  public function add($data)
  {
    $cfg  = $this->getClassCfg();
    $a = $cfg['arch']['products'];
    $noteCfg = $this->note->getClassCfg();
    if (!X::hasProps($data, [$a['product_type'], $a['id_provider'], 'url', $noteCfg['arch']['versions']['title']])) {
      throw new \Exception(_("Some mandatory fields are missing"));
    }

    if (!($type = $this->getTypeNote())) {
      throw new \Exception(_("Impossible to retrieve the product type"));
    }

{'fraction_non_alphanumeric': 0.21340721719151237, 'fraction_numerical': 0.0, 'mean_word_length': 2.7392622536634663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 132, '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': '5252949', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2596, 'n_words': 499}