<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2015 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfNoticesEventMsg Class
 *
 * Class to hold a single message.
 *
 * @package clearFusionCMS
 */
class cfNoticesEventMsg {
	/**#@+
	 * Type of event.
	 */
	const PLAIN = 0;
	const INFO = 1;
	const WARNING = 2;
	const ERROR  = 3;
	/**#@-*/

	/**
	 * The message text.
	 *
	 * @var string
	 */
	public $text;

	/**
	 * A count to display in front of the message.
	 *
	 * @var int
	 */
	public $count = null;

	/**
	 * Address to link to.
	 *
	 * @var string
	 */
	public $link = null;

	/**
	 * The event type.
	 *
	 * @var string
	 */
	public $type = self::PLAIN;

	/**
	 * Create a new message.
	 *
	 * @param string $text The message.
	 * @param int $count The count.
	 * @param string $link The link.
	 * @param int $type The event type, one of PLAIN, INFO, WARNING, ERROR.
	 */
	function __construct($text, $count = null, $link = null, $type = self::PLAIN) {
		$this->text = $text;
		$this->count = $count;
		$this->link = $link;
		$this->type = $type;
	}
}

/**
 * cfNoticesEvent Class
 *
 * Class to provide a specific event object for handling module notice collections.
 *
 * @package clearFusionCMS
 */
class cfNoticesEvent extends flEvent {

	/**
	 * Array of event messages to show.
	 *
	 * @var array
	 */
	protected $messages = array();

	/**
	 * Create a new event object.
	 */
	function __construct() {
		parent::__construct('system.managerGetNotices');
	}

	/**
	 * Add a message to the event list.
	 *
	 * @param cfNoticesEventMsg $msg The event message.
	 */
	function addMsg($msg) {
		$this->messages[] = $msg;
	}

	/**
	 * Test if messages are present.
	 *
	 * @return bool true if messages are present; else false.
	 */
	function hasMessages() {
		return count($this->messages) != 0;
	}

	/**
	 * Get the messages as a set of li elements.
	 *
	 * @return array List of cfNoticesEventMsg objects.
	 */
	function getList() {

		// Sort messages
		usort($this->messages, function($a, $b) {
			return $b->type - $a->type;
		});

		return $this->messages;
	}

	/**
	 * Get the number of messages.
	 *
	 * @return int The number of messages.
	 */
	function countMessages() {
		return count($this->messages);
	}
}