<?php

/**
 * Class file for dashboard management..
 *
 * 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) 2012 - 2015 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfDashboardMgr Class
 *
 * Manage the dashboard groups and icons.
 *
 * @package clearFusionCMS
 */
class cfDashboardMgr {

	/**
	 * The database object.
	 *
	 * @var flDb
	 */
	private $db;

	/**
	 * Construct the object.
	 *
	 * @param flDb $db The database object to use.
	 */
	function __construct($db) {
		$this->db = $db;
	}

	/**
	 * Add a group to the dashboard.
	 *
	 * @param string $name The name of the group.
	 * @param int $displayOrder Value used to sort the order of groups.
	 * @param string $displayName The display name of the group.
	 * @param string $textDomain The text domain for translations.
	 * @return bool true if added; else false.
	 */
	function addGroup($name, $displayOrder, $displayName, $textDomain = '') {
		return $this->db->insert('cf_dashboardgrps', array('grpname' => $name, 'displayname' => $displayName, 'disporder' => $displayOrder, 'textdomain' => $textDomain, 'faicon' => '', 'mdicon' => ''));
	}

	/**
	 * Delete a dashboard group if it contains no icons.
	 *
	 * @param string $name The name of the group to delete.
	 */
	function delGroup($name) {
		$iconCount = $this->db->fetchOne('SELECT COUNT(*) FROM cf_dashboardicons WHERE grpname=?', $name);
		if(!$iconCount)
			$this->db->delete('cf_dashboardgrps', 'grpname=?', $name);
	}

	/**
	 * Get the list of groups.
	 *
	 * @result array Array of group id to display names.
	 */
	function getGroups() {
		$r = array();
		$result = $this->db->query('SELECT grpname,displayname,textdomain FROM cf_dashboardgrps ORDER BY disporder');
		while($row = $result->fetch())
			$r[$row['grpname']] = __($row['displayname'], $row['textdomain']);
		return $r;
	}

	/**
	 * Get the Font Awesome group icons.
	 *
	 * @return array
	 */
	function getGroupFAIcons() {
		$icons = array();
		$result = $this->db->query('SELECT grpname,faicon FROM cf_dashboardgrps');
		while($row = $result->fetch())
			$icons[$row['grpname']] = empty($row['faicon']) ? 'fa-folder-o' : $row['faicon'];
		return $icons;
	}

	/**
	 * Get the Google Material icons for each group.
	 *
	 * @return array
	 */
	function getGroupMDIcons() {
		$icons = array();
		$result = $this->db->query('SELECT grpname,mdicon FROM cf_dashboardgrps');
		while($row = $result->fetch())
			$icons[$row['grpname']] = empty($row['mdicon']) ? 'folder_open' : $row['mdicon'];
		return $icons;
	}

	/**
	 * Get the group display order number.
	 *
	 * @param string $grpName The name of the group to get the order of.
	 * @return int The order number for the group.
	 */
	function getGroupOrder($grpName) {
		return $this->db->fetchOne('SELECT disporder FROM cf_dashboardgrps WHERE grpname=?', $grpName);
	}

	/**
	 * Set the group display order number.
	 *
	 * @param string $grpName The name of the group.
	 * @param int $order The new display order for the group.
	 * @return bool true if the order was updated; else false.
	 */
	function setGroupOrder($grpName, $order) {
		return $this->db->update('cf_dashboardgrps', array('disporder' => $order), 'grpname=?', $grpName);
	}

	/**
	 * Set the display name of a group.
	 *
	 * @param string $name The name of the group to update.
	 * @param string $displayName The new display name of the group.
	 * @param string $textDomain The text domain for translations.
	 * @return bool true if updated; else false.
	 */
	function setGroupName($name, $displayName, $textDomain = '') {
		return $this->db->update('cf_dashboardgrps', array('displayname' => $displayName, 'textdomain' => $textDomain), 'grpname=?', $name);
	}

	/**
	 * Set the Font Awesome icon to use for the group.
	 *
	 * @param string $name The name of the group to update.
	 * @param string $iconName The name of the icon to use.
	 * @return bool true if updated; else false.
	 */
	function setGroupFAIcon($name, $iconName) {
		return $this->db->update('cf_dashboardgrps', array('faicon' => $iconName), 'grpname=?', $name);
	}

	/**
	 * Set the Google Material icon to use for the group.
	 *
	 * @param string $name The name of the group to update.
	 * @param string $iconName The name of the icon to use.
	 * @return bool true if updated; else false.
	 */
	function setGroupMDIcon($name, $iconName) {
		return $this->db->update('cf_dashboardgrps', array('mdicon' => $iconName), 'grpname=?', $name);
	}

	/**
	 * Add an icon to a dashboard group.
	 *
	 * @param string $grpName The name of the grouop to add the icon to.
	 * @param string $name The unique name of the icon.
	 * @param string $permission The permission required to access that icon.
	 * @param string $displayName The name to display with the icon.
	 * @param string $desc The description of the icon function.
	 * @param string $icon The location of the icon to show.
	 * @param string $module The module to invoke the action on.
	 * @param string $action The module action to invoke.
	 * @param string $textDomain The text domain for translations.
	 * @return bool true if the icon was added else false.
	 */
	function addIcon($grpName, $name, $permission, $displayName, $desc, $icon, $module, $action, $textDomain = '') {
		if($this->db->insert('cf_dashboardicons', array('grpname' => $grpName,
			'iconname' => $name,
			'permission' => $permission,
			'displayname' => $displayName,
			'description' => $desc,
			'iconfile' => $icon,
			'module' => $module,
			'action' => $action,
			'textdomain' => $textDomain))) {
			// Test if the group is valid
			if(!$this->db->fetchOne('SELECT COUNT(*) FROM cf_dashboardgrps WHERE grpname=?', $grpName)) {
				$this->db->delete('cf_dashboardicons', 'name=?', $name);
				return false;
			}

			return true;
		}
		else
			return false;
	}

	/**
	 * Delete an icon, if there are no other icons in the group then the group is deleted as well.
	 *
	 * @param string $name The name of the icon to delete.
	 */
	function delIcon($name) {
		$grp = $this->db->fetchOne('SELECT grpname FROM cf_dashboardicons WHERE iconname=?', $name);
		$this->db->delete('cf_dashboardicons', 'iconname=?', $name);
		$iconCount = $this->db->fetchOne('SELECT COUNT(*) FROM cf_dashboardicons WHERE grpname=?', $grp);
		if(!$iconCount)
			$this->db->delete('cf_dashboardgrps', 'grpname=?', $grp);
	}

	/**
	 * Set the name of the icon file.
	 *
	 * @param string $name The name of the icon to set.
	 * @param string $file The new icon file name.
	 * @return bool true if updated; else false.
	 */
	function setIconFile($name, $file) {
		return $this->db->update('cf_dashboardicons', array('iconfile' => $file), 'iconname=?', $name);
	}

	/**
	 * Set the display name of the icon.
	 *
	 * @param string $name The name of the icon to set.
	 * @param string $displayName The new display name of the icon.
	 * @param string $textDomain The text domain for translations.
	 * @return bool true if updated; else false.
	 */
	function setIconName($name, $displayName, $textDomain = '') {
		return $this->db->update('cf_dashboardicons', array('displayname' => $displayName, 'textdomain' => $textDomain), 'iconname=?', $name);
	}

	/**
	 * Set the description of the icon.
	 *
	 * @param string $name The name of the icon to set.
	 * @param string $desc The new description of the icon.
	 * @param string $textDomain The text domain for translations.
	 * @return bool true if updated; else false.
	 */
	function setIconDesc($name, $desc, $textDomain = '') {
		return $this->db->update('cf_dashboardicons', array('description' => $desc, 'textdomain' => $textDomain), 'iconname=?', $name);
	}

	/**
	 * Set the group for the icon.
	 *
	 * @param string $name The name of the icon to set the group for.
	 * @param string $newGrp The name of the new group for the icon.
	 * @return bool true if updated; else false.
	 */
	function setIconGroup($name, $newGrp) {
		return $this->db->update('cf_dashboardicons', array('grpname' => $newGrp), 'iconname=?', $name);
	}

	/**
	 * Get all the icons in a given group.
	 *
	 * @param string $grpName The name of the group to get.
	 * @param flUser $user The user object for access control or null. If given the permission of each icon will be tested and
	 * only those that the user has permission for will be returned.
	 * @return array The array of icons.
	 */
	function getIcons($grpName, $user = null) {
		$icons = $this->db->fetchAll('SELECT iconname,permission,displayname,description,iconfile,module,action,textdomain FROM cf_dashboardicons WHERE grpname=? ORDER BY displayname', $grpName);
		$allowed = array();
		foreach($icons as $icon) {
			if($user === null || empty($icon['permission']) || $user->hasPermission($icon['permission'])) {
				$icon['displayname'] = __($icon['displayname'], $icon['textdomain']);
				$icon['description'] = __($icon['description'], $icon['textdomain']);
				$allowed[] = $icon;
			}
		}
		return $allowed;
	}
}
