View file veppa_wallpaper/sys/app/models/Category.php

File size: 1.92Kb
<?php

/**
 * Free Wallpaper Script
 *
 * Free Wallpaper Script by Vepa Halliyev is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
 *
 * @package		Free Wallpaper Script
 * @author		Vepa Halliyev
 * @copyright	Copyright (c) 2009, Vepa Halliyev, veppa.com.
 * @license		http://www.veppa.com/free-wallpaper-script/
 * @link		http://www.veppa.com/free-wallpaper-script/
 * @since		Version 1.0
 * @filesource
 */

/**
 * class Category
 *
 * @author Vepa Halliyev <veppa.com>
 * @since  0.1
 */
class Category extends Record
{
	const TABLE_NAME = 'category';

	private static $cols = array(
		'id' => 1,
		'name' => 1,
		'added_at' => 1,
		'added_by' => 1
	);

	public function __construct($data = false, $locale_db = false)
	{
		$this->setColumns(self::$cols);
		parent::__construct($data, $locale_db);
	}

	function beforeInsert()
	{
		$this->added_at = REQUEST_TIME;
		$this->added_by = AuthUser::$user->id;
		return true;
	}

	static function getCategories()
	{
		return Category::findAllFrom('Category', '1=1 ORDER BY name');
	}

	static function checkboxes($categories, $wallpaper)
	{
		$arr_tags = explode(',', $wallpaper->tags);
		foreach ($categories as $c)
		{
			if (in_array(strtolower($c->name), $arr_tags))
			{
				$sel = 'checked="checked"';
			}
			else
			{
				$sel = '';
			}
			$r .= '<div class="category_check">
					<input type="checkbox" name="categories[]" id="categories_' . $c->id . '" value="' . $c->id . '" ' . $sel . ' /> 
					<label for="categories_' . $c->id . '">' . View::escape($c->name) . '</label>
					</div>';
		}
		if ($r)
		{
			$r .= '<div class="clear"></div>';
		}

		return $r;
	}

	function changeName($name)
	{
		// change tag name, because same category is used as tag
		Tag::changeName($this->name, $name);

		// change categry name
		$this->name = $name;
		return $this->save('id');
	}
}

// end User class