<?php
/**
 * Class represents records from table email_sent
 * {autogenerated}
 * @property int $email_sent_id PK
 * @property string|null $subject
 * @property string|null $format
 * @property string|null $body
 * @property string|null $files
 * @property string|null $newsletter_ids
 * @property int|null $count_users
 * @property int|null $sent_users
 * @property string|null $last_email
 * @property string|null $desc_users
 * @property string|null $is_cancelled
 * @property string|null $serialized_vars
 * @property datetime $tm_added
 * @property datetime|null $tm_finished
 * @property int|null $admin_id FK admin
 * {/autogenerated}
 */
class EmailSent extends Am_Record
{
    protected $_serializeFields = ['subject', 'body', 'format', 'files',];

    public function unserialize()
    {
        $arr = unserialize($this->serialized_vars);
        foreach ($this->_serializeFields as $k)
        {
            $arr[$k] = $this->$k;
            if ($k == 'files')
                $arr[$k] = array_filter(explode(',', $arr[$k]));
        }
        return $arr;
    }

    public function serialize(array $arr)
    {
        foreach ($this->_serializeFields as $k)
        {
            $this->$k = @$arr[$k];
            unset($arr[$k]);
            if ($k == 'files' && is_array($this->$k))
                $this->$k = implode(",", $this->$k);
        }
        $this->serialized_vars = serialize($arr);
    }
}

/** @extends Am_Table<EmailSent> */
class EmailSentTable extends Am_Table
{
    protected $_key = 'email_sent_id';
    protected $_table = '?_email_sent';
    protected $_recordClass = 'EmailSent';

    public function insert(array $values, $returnInserted = false)
    {
        if (empty($values['tm_added']))
            $values['tm_added'] = $this->getDi()->sqlDateTime;
        return parent::insert($values, $returnInserted);
    }
}