Source of file NoteAjax.php

Size: 2,582 Bytes - Last Modified: 2020-10-24T02:46:31+00:00

/home/travis/build/NextDom/nextdom-core/src/Ajax/NoteAjax.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
<?php
/* This file is part of Jeedom.
 *
 * Jeedom is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jeedom is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
 */

namespace NextDom\Ajax;

use NextDom\Enums\AjaxParams;
use NextDom\Enums\UserRight;
use NextDom\Exceptions\CoreException;
use NextDom\Helpers\Utils;
use NextDom\Managers\NoteManager;
use NextDom\Model\Entity\Note;

/**
 * Class NoteAjax
 * @package NextDom\Ajax
 */
class NoteAjax extends BaseAjax
{
    protected $NEEDED_RIGHTS = UserRight::ADMIN;
    protected $MUST_BE_CONNECTED = true;
    protected $CHECK_AJAX_TOKEN = true;

    /**
     * Get all notes
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function all()
    {
        $this->ajax->success(Utils::o2a(NoteManager::all()));
    }

    /**
     * Get note by Id
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function byId()
    {
        $this->ajax->success(Utils::o2a(NoteManager::byId(Utils::init(AjaxParams::ID))));
    }

    /**
     * Save note
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function save()
    {
        $noteData = json_decode(Utils::init('note'), true);
        if (empty($noteData['name'])) {
            $this->ajax->error(__('entity.note.name-cannot-be-empty'));
        } else {
            if (isset($noteData['id'])) {
                $note = NoteManager::byId($noteData['id']);
            } else {
                $note = new Note();
            }
            Utils::a2o($note, $noteData);
            $note->save();
            $this->ajax->success(Utils::o2a($note));
        }
    }

    /**
     * Remove note
     *
     * @throws CoreException
     * @throws \ReflectionException
     */
    public function remove()
    {
        $note = NoteManager::byId(Utils::init(AjaxParams::ID));
        if (!is_object($note)) {
            throw new CoreException(__('Note inconnue. Vérifiez l\'ID'));
        }
        $note->remove();
        $this->ajax->success();
    }

}