vendor/pimcore/pimcore/models/Tool/Email/Log/Dao.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Tool\Email\Log;
  15. use Pimcore\Logger;
  16. use Pimcore\Model;
  17. /**
  18.  * @internal
  19.  *
  20.  * @property \Pimcore\Model\Tool\Email\Log $model
  21.  */
  22. class Dao extends Model\Dao\AbstractDao
  23. {
  24.     /**
  25.      * Name of the db table
  26.      *
  27.      * @var string
  28.      */
  29.     protected static $dbTable 'email_log';
  30.     /**
  31.      * Get the data for the object from database for the given id, or from the ID which is set in the object
  32.      *
  33.      * @param int|null $id
  34.      */
  35.     public function getById($id null)
  36.     {
  37.         if ($id != null) {
  38.             $this->model->setId($id);
  39.         }
  40.         $data $this->db->fetchAssociative('SELECT * FROM email_log WHERE id = ?', [$this->model->getId()]);
  41.         $this->assignVariablesToModel($data);
  42.     }
  43.     /**
  44.      * Save document to database
  45.      */
  46.     public function save()
  47.     {
  48.         if (!$this->model->getId()) {
  49.             $this->create();
  50.         }
  51.         $data = [];
  52.         $emailLog $this->model->getObjectVars();
  53.         foreach ($emailLog as $key => $value) {
  54.             if (in_array($key$this->getValidTableColumns(self::$dbTable))) {
  55.                 // check if the getter exists
  56.                 $getter 'get' ucfirst($key);
  57.                 if (!method_exists($this->model$getter)) {
  58.                     continue;
  59.                 }
  60.                 // get the value from the getter
  61.                 $value $this->model->$getter();
  62.                 if (is_bool($value)) {
  63.                     $value = (int) $value;
  64.                 } elseif (is_array($value)) {
  65.                     //converts the dynamic params to a basic json string
  66.                     $preparedData self::createJsonLoggingObject($value);
  67.                     $value json_encode($preparedData);
  68.                 }
  69.                 $data[$key] = $value;
  70.             }
  71.         }
  72.         try {
  73.             $this->db->update(self::$dbTable$data, ['id' => $this->model->getId()]);
  74.         } catch (\Exception $e) {
  75.             Logger::emerg('Could not Save emailLog with the id "'.$this->model->getId().'" ');
  76.         }
  77.     }
  78.     /**
  79.      * Deletes object from database
  80.      */
  81.     public function delete()
  82.     {
  83.         $this->db->delete(self::$dbTable, ['id' => $this->model->getId()]);
  84.     }
  85.     public function create()
  86.     {
  87.         $this->db->insert(self::$dbTable, []);
  88.         $date time();
  89.         $this->model->setId((int) $this->db->lastInsertId());
  90.         $this->model->setModificationDate($date);
  91.     }
  92.     /**
  93.      * @param array|string $data
  94.      *
  95.      * @return array|string
  96.      */
  97.     protected function createJsonLoggingObject($data)
  98.     {
  99.         if (!is_array($data)) {
  100.             return json_encode(new \stdClass());
  101.         } else {
  102.             $loggingData = [];
  103.             foreach ($data as $key => $value) {
  104.                 $loggingData[] = self::prepareLoggingData($key$value);
  105.             }
  106.             return $loggingData;
  107.         }
  108.     }
  109.     /**
  110.      * Creates the basic logging for the treeGrid in the backend
  111.      * Data will be enhanced with live-data in the backend
  112.      *
  113.      * @param string $key
  114.      * @param mixed $value
  115.      *
  116.      * @return \stdClass
  117.      */
  118.     protected function prepareLoggingData($key$value)
  119.     {
  120.         $class = new \stdClass();
  121.         $class->key = (string)$key// key has to be a string otherwise the treeGrid won't work
  122.         if (is_string($value) || is_int($value) || is_null($value)) {
  123.             $class->data = ['type' => 'simple',
  124.                 'value' => $value, ];
  125.         } elseif ($value instanceof \DateTimeInterface) {
  126.             $class->data = ['type' => 'simple',
  127.                 'value' => $value->format('Y-m-d H:i'), ];
  128.         } elseif (is_object($value) && method_exists($value'getId')) {
  129.             $class->data = ['type' => 'object',
  130.                 'objectId' => $value->getId(),
  131.                 'objectClass' => get_class($value), ];
  132.         } elseif (is_array($value)) {
  133.             foreach ($value as $entryKey => $entryValue) {
  134.                 $class->children[] = self::prepareLoggingData($entryKey$entryValue);
  135.             }
  136.         }
  137.         return $class;
  138.     }
  139. }