<?php
require_once(dirname(__FILE__).'/../../upgrade.php');

class migrate_category_passwords extends UPGRADE
{
	/**
	* IsAlreadyUpgraded
	* Do we need to run this classes upgrade script or not ?
	*
	* We always return false in this instance so each category and password can be checked against groups everytime.
	*
	* @return bool true if the upgrade has already run, otherwise false
	*/
	function IsAlreadyUpgraded() {
		$query = "SELECT * FROM ".$GLOBALS['tablePrefix']."categories WHERE pass != ''";
		$result = $GLOBALS['AKB_DB']->Query($query);
		
		return !(boolean)$GLOBALS['AKB_DB']->CountResult($result);
	}

	/**
	* Upgrade
	* Do the actual upgrade of this component. If this is successful all
	* subsequent calls to IsAlreadyUpgraded should return true
	*
	* @return void
	*/
	function Upgrade() {
		$query = "SELECT * FROM ".$GLOBALS['tablePrefix']."categories WHERE pass != ''";
		$result = $GLOBALS['AKB_DB']->Query($query);
		
		if ($GLOBALS['AKB_DB']->CountResult($result) > 0) {
		
			require_once(dirname(__FILE__).'/../../api/class.user.php');
			
			//Get all groups
			$group_obj = new API_GROUP();
			$group_list = $group_obj->getList();
			
			//We have categories that have passwords. Iterate through the resultset and create groups / users / category auth links.
			while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
				$category_id = $row['categoryid'];
				$category_name = $row['name'];
				$category_pass = $row['pass'];
				
				if (!in_array($category_name,$group_list)) {
					//Set group info
					$_POST['contactable'] = 0;
					$_POST['name'] = $category_name;
					$group_obj->resetPerms();
					$group_obj->applies['feonly'] = "1";
					$group_obj->applies['category'][] = $category_id;
					
					if ($new_group_id = $group_obj->create()) {
						//Now create user for this group
						$user_obj = new API_USER();
						
						$_POST['username'] = $category_name;
						$user_obj->find('username', $_POST['username']);

						$_POST['username'] = $category_name;
						$_POST['password'] = $category_pass;
						
						$_POST['firstname'] = "[Auto Generated]";
						$_POST['lastname'] = "[Auto Generated]";
						$_POST['status'] = 1;
						$_POST['email'] = "";
						
						if ($user_obj->userid == 0) {
							if ($new_user_id = $user_obj->create()) {
								//New user successfully created.
								//Now make sure they belong to the new group.
								$user_obj->userid = $new_user_id;
								$user_group_join = $user_obj->join($new_group_id);
								
								if ($user_group_join) {
									$query = "UPDATE ".$GLOBALS['tablePrefix']."categories SET pass = ''";
									$result = $GLOBALS['AKB_DB']->Query($query);
									return true;
								}
								
							} else {
								return false;
							}
						}

					} else {
						//Group create failed.
						return false;
					}
				}
			}
			
			$query = "UPDATE ".$GLOBALS['tablePrefix']."categories SET pass = ''";
			$result = $GLOBALS['AKB_DB']->Query($query);
			return true;
		}
	}

}
?>
