ApplicationFormService.php 2.2 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * gredu_labs.
 *
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 *
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\ApplicationForm\Service;

13
use RedBeanPHP\OODBBean;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
14 15 16 17 18 19 20 21 22
use RedBeanPHP\R;

class ApplicationFormService implements ApplicationFormServiceInterface
{
    public function submit(array $data)
    {
        $appForm                      = R::dispense('applicationform');
        $appForm->school_id           = $data['school_id'];
        $appForm->comments            = $data['comments'];
23 24 25 26
        $appForm->submitted           = time();
        $appForm->submitted_by        = $data['submitted_by'];
        $items                        = [];
        foreach ($data['items'] as $itemData) {
27 28
            $item                  = R::dispense('applicationformitem');
            $item->lab_id          = $itemData['lab_id'];
29 30
            $item->itemcategory_id = $itemData['itemcategory_id'];
            $item->qty             = $itemData['qty'];
31
            $item->qtyacquired     = $itemData['qtyacquired'];
32 33 34 35 36 37 38 39 40 41 42 43
            $item->reasons         = $itemData['reasons'];
            $items[]               = $item;
        }
        if (!empty($items)) {
            $appForm->ownApplicationformitemList = $items;
        }

        R::store($appForm);

        return $this->exportApplicationForm($appForm);
    }

44 45
    public function findSchoolApplicationForm($schoolId)
    {
46
        $appForm = R::findOne('applicationform', ' school_id = ? ORDER BY id DESC', [$schoolId]);
47 48 49 50 51 52 53 54
        if (null === $appForm) {
            return;
        }

        return $this->exportApplicationForm($appForm);
    }

    private function exportApplicationForm(OODBBean $bean)
55 56 57 58 59 60 61 62 63 64
    {
        $appForm          = $bean->export();
        $appForm['items'] = array_map(function ($itemBean) {
            return array_merge($itemBean->export(), [
                'lab'          => $itemBean->lab->name,
                'itemcategory' => $itemBean->itemcategory->name,
            ]);
        }, $bean->ownApplicationformitemList);

        return $appForm;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
65 66
    }
}