<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class WV42_TreeHelper {
	/**
	 * Unfold the selected category permissions to all articles
	 *
	 * @param  array $params  event params
	 * @return array
	 */
	public static function applyCatPerms(array $params) {
		// different destination, do nothing
		if ($params['destination'] !== 'article') return $params['subject'];

		$values = $params['subject'];

		foreach ($values as $token => $ids) {
			foreach ($ids as $id) {
				if ($id == 0) continue;

				// find children articles
				$cat = sly_Util_Category::findById($id);

				// this was probably just a selected article, so leave it alone
				if (!$cat) continue;

				$articles = $cat->getArticles();

				foreach ($articles as $art) {
					$values[$token][] = $art->getId();
				}
			}

			$values[$token] = array_unique($values[$token]);
		}

		return $values;
	}
}
