<?php 

namespace common\modules\support\models\redmine;

use common\models\Settings;

class RedmineHelper 
{
	public static function sortIsuues($issues = null)
	{
        if($issues)
        {
    		$settings = [
                [
                    'statuses' => explode(',', Settings::getValue('support-status-work')),
                    'title' => 'Задачи в работе',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-new')),
                    'title' => 'Задачи на оценке у менеджера',
                    'rating' => true,
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-rework')),
                    'title' => 'Задачи на доработке',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-approve')),
                    'title' => 'Задачи ожидающие проверки',
                ],
                [
                    'statuses' => explode(',', Settings::getValue('support-status-accepted')),
                    'title' => 'Закрытые задачи',
                    'close' => true
                ],
            ];

            foreach ($issues as $issue) 
            {
                foreach ($settings as &$setting) 
                {
                    if(in_array($issue['status']['id'], $setting['statuses']))
                    {
                        $setting['models'][] = $issue;
                    }
                }
            }

            unset($setting);

            return $settings;
        }

        return $issues;
	}

    public static function sortMemberships($memberships = null)
    {
        if($memberships)
        {
            $output = [];
            foreach ($memberships as $membership) 
            {
                if(isset($membership['roles']))
                {
                    foreach ($membership['roles'] as $role) 
                    {
                        if($role['id'] == 5)
                        {
                            $output[$membership['project']['id']] = $membership['project']['name'];
                        }
                    }
                }
            }

            return $output;
        }

        return $memberships;
    }
}