function editAction()
	{
		$errors = array();
		$users_table = new Users();
		$users_roles_table = new UsersRoles();
		$request = new RivetyCore_Request($this->getRequest());

		// $countries_table = new Countries();
		// $this->view->countries = $countries_table->getCountriesArray('Choose a country...');

		$roles_table = new Roles();
		$roles = $roles_table->fetchAll(NULL,"shortname ASC");
		$arRoles = array();
		foreach ($roles as $role)
		{
			if (!strpos($role->shortname,"-base"))
			{
				$arRoles[$role->id] = $role->description;
			}
		}
		$this->view->roles = $arRoles;

		$is_new = true;
		$user = array();
		if ($request->has('username'))
		{
			$obUser = $users_table->fetchByUsername($request->username);
			if (!is_null($obUser))
			{
				$is_new = false;
				$user_roles = $users_roles_table->fetchAll($users_roles_table->select()->where("username = ?", $obUser->username));
				if (count($user_roles) > 0)
				{
					$tmp_selected = array();
					foreach ($user_roles as $user_role)
					{
						$tmp_selected[] = $user_role->role_id;
					}
					$this->view->selected_roles = $tmp_selected;
				}
				$user = $obUser->toArray();
			}
		}
		$this->view->is_new = $is_new;

		if ($is_new)
		{
			// defaults for form fields
			$user['username'] = "";
			// $user['full_name'] = "";
			// $user['aboutme'] = "";
		}

		$pre_render = $this->_rivety_plugin->doFilter($this->_mca."_pre_render", array('user' => $user, 'request' => $this->_request)); // FILTER HOOK
		$user = $pre_render['user'];

		foreach ($pre_render as $key => $value)
		{
			if ($key != "user")
			{
				$this->view->$key = $value;
			}
		}

		// $tags = unserialize($user['tags']);

		if ($this->getRequest()->isPost())
		{
			$errors = array();

			$request->stripTags(array('email', 'newpassword', 'confirm'));
			// $request->stripTags(array('full_name', 'email', 'newpassword', 'confirm', 'aboutme'));
			$user['username'] = $request->username;
			$user['email'] = $request->email;
			$user['password'] = $request->newpassword;
			$user['confirm'] = $request->confirm;
			// $user['full_name'] = $request->full_name;
			// $user['birthday'] = $birthday = strtotime($request->Birthday_Day.$request->Birthday_Month.$request->Birthday_Year);
			// $user['gender'] = $request->gender;
			// $user['country_code'] = $request->country_code;
			// $user['aboutme'] = $request->aboutme;

			// validate username
			$username_validator = new Zend_Validate();
			$username_validator->addValidator(new Zend_Validate_StringLength(1, RivetyCore_Registry::get('username_length')));
			$username_validator->addValidator(new Zend_Validate_Alnum());
			if (!$username_validator->isValid($user['username']))
			{
				$show_username = "******".$user['username']."'";
				if (trim($user['username']) == "")
				{
					$show_username = "******".$this->_T("empty")."]";
				}
				$errors[] = $this->_T("%s isn't a valid username. (Between %d and %d characters, only letters and numbers)", array($show_username, 1, RivetyCore_Registry::get('username_length')));
			}
			if ($is_new)
			{
				$user_where = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
				if ($users_table->getCountByWhereClause($user_where) > 0)
				{
					$errors[] = $this->_T("The username '%s' is already in use",$user['username']);
				}
			}

			// validate email
			if (!RivetyCore_Validate::checkEmail($user['email']))
			{
				$errors[] = $this->_T("Email is not valid");
		 	}

			// check to see if email is in use already by someone else
			if ($users_table->isEmailInUse($user['email'], $user['username']))
			{
				$errors[] = $this->_T("Email already in use");
			}

			// if password isn't blank, validate it
			if ($user['password'] != "")
			{
				if (!RivetyCore_Validate::checkLength($user['password'], 6, RivetyCore_Registry::get('password_length')))
				{
					$errors[] = $this->_T("Password must be between 6 and 32 characters");
		 		}
				// if password is set, make sure it matches confirm
				if ($user['password'] != $user['confirm'])
				{
					$errors[] = $this->_T("Passwords don't match");
				}
			}

			// // convert birthday_ts to mysql date
			// $birthday = date("Y-m-d H:i:s", $user['birthday']);

			$params = array(
				'request' => $request,
				'user' => $user,
				'errors' => $errors,
			);

			// // upload new avatar image if present
			// if (array_key_exists('filedata', $_FILES))
			// {
			// 	if ($_FILES['filedata']['tmp_name'] != '')
			// 	{
			// 		$destination_path = RivetyCore_Registry::get('upload_path') . "/" . $user['username'] . "/original";
			// 		if (!is_dir($destination_path))
			// 		{
			// 			mkdir($destination_path, 0777, true);
			// 			RivetyCore_Log::report("Creating user folder at " . $destination_path, null, Zend_Log::DEBUG);
			// 		}
			// 		if (file_exists($destination_path . "/avatar"))
			// 		{
			// 			unlink($destination_path . "/avatar");
			// 			RivetyCore_Log::report("Deleted existing user avatar from " . $destination_path, null, Zend_Log::DEBUG);
			// 		}
			// 		else
			// 		{
			// 			RivetyCore_Log::report("User avatar did not exist in " . $destination_path, null, Zend_Log::DEBUG);
			// 		}
			// 		move_uploaded_file($_FILES['filedata']['tmp_name'], $destination_path . "/avatar");
			// 		Users::clearUserCache($user['username']);
			// 		RivetyCore_Log::report("User avatar uploaded to " . $destination_path, null, Zend_Log::DEBUG);
			// 		$params['user']['hasnewfile'] = true;
			// 	}
			// 	else
			// 	{
			// 		$params['user']['hasnewfile'] = false;
			// 	}
			// }

			$additional = $this->_rivety_plugin->doFilter($this->_mca . "_pre_save", $params); // FILTER HOOK
			$errors = $additional['errors'];
			$user = $additional['user'];

			$users_roles_table->delete($users_roles_table->getAdapter()->quoteInto("username = ?", $user['username']));
			foreach ($request->role_ids as $role_id)
			{
				$role_data = array("username" => $user['username'], "role_id" => $role_id);
				$users_roles_table->insert($role_data);
			}

			if (count($errors) == 0)
			{
				// $user['birthday'] = $birthday;
				// $user['aboutme'] = nl2br($user['aboutme']);
				$user['last_modified_on'] = date(DB_DATETIME_FORMAT);

				// This is a hold-over value from the form.
				unset($user['confirm']);

				if ($user['password'] != "")
				{
					#$data['password'] = $user['password'];
				}
				else
				{
					unset($user['password']);
				}

				$screen_alert_message = 'The user was created succcessfully.';
				if ($is_new)
				{
					$filter_hook_params = array(
						'request' => $request,
						'user' => $user,
						'errors' => $errors,
					);
					$additional1 = $this->_rivety_plugin->doFilter($this->_mca, $filter_hook_params); // FILTER HOOK
					$errors = $additional1['errors'];
					$user = $additional1['user'];
					// $data['username'] = $user['username'];
					// $data['created_on'] = date(DB_DATETIME_FORMAT);
					$user['created_on'] = date(DB_DATETIME_FORMAT);
					$users_table->insert($user);
				}
				else
				{
					$screen_alert_message = 'Changes to the user were saved succcessfully.';
					$where = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
					// $users_table->update($data, $where);
					$users_table->update($user, $where);
				}
				$this->screenAlertQueued('success', $screen_alert_message, date(DB_DATETIME_FORMAT, time() + 30), 'default_useradmin_index');
				$this->_redirect('/default/useradmin/index/');
			}
			else
			{
				$this->view->errors = $errors;
			}
		}
		$this->view->end_year = -(RivetyCore_Registry::get('minimum_registration_age'));
		// $this->view->genders = RivetyCore_Common::getGenderArray();
		// $user['aboutme'] = RivetyCore_Common::br2nl($user['aboutme']);
		$this->view->user = $user;
	}
Example #2
0
	function editAction()
	{
		if ($this->_user->username != $this->_identity->username)
		{
			$this->_forward('default', 'auth', 'missing'); return;
		}
		else
		{
			// $countries_table = new Countries();
			// $this->view->countries = $countries_table->getCountriesArray('Choose a country...');

			$user = $this->_user->toArray();
			$params = array('user' => $user, 'request' => $this->_request, 'session' => $this->session);
			$pre_render = $this->_rivety_plugin->doFilter($this->_mca . "_pre_render", $params); // FILTER HOOK
			$user = $pre_render['user'];

			foreach ($pre_render as $key => $value)
			{
				if ($key != "user") $this->view->$key = $value;
			}

			//$tags = unserialize($user->tags);
			if ($this->getRequest()->isPost())
			{
				$errors = array();
				$request = new RivetyCore_Request($this->getRequest());
				// $request->stripTags(array('email', 'newpassword', 'confirm', 'aboutme'));
				$request->stripTags(array('email', 'newpassword', 'confirm'));
				$user['username'] = $this->_identity->username;
				$user['email'] = $request->email;
				// $user['full_name'] = $request->full_name;
				$user['password'] = $request->newpassword;
				$user['confirm'] = $request->confirm;
				// $user['birthday'] = $birthday = strtotime($request->Birthday_Day . $request->Birthday_Month . $request->Birthday_Year);
				//$user['tags'] = $tag_array = RivetyCore_Common::makeTagArray($request->tags);
				// $user['gender'] = $request->gender;
				// $user['country_code'] = $request->country_code;
				// $user['aboutme'] = $request->aboutme;

				// validate email
				if (!RivetyCore_Validate::checkEmail($user['email']))
				{
					$errors[] = $this->_T("Email is not valid");
	   		 	}

				// check to see if email is in use already by someone else
				if ($this->_users_table->isEmailInUse($user['email'],$user['username']))
				{
					$errors[] = $this->_T("Email already in use");
				}

				// if password isn't blank, validate it
				if ($user['password'] != "")
				{
					if (!RivetyCore_Validate::checkLength($user['password'], 6, RivetyCore_Registry::get('password_length')))
					{
						$errors[] = $this->_T("Password must be between %d and %d characters",array(6, RivetyCore_Registry::get('password_length')));
	   		 		}
					// if password is set, make sure it matches confirm
					if ($user['password'] != $user['confirm'])
					{
						$errors[] = $this->_T("Passwords don't match");
					}
				}

				// if (!RivetyCore_Validate::checkLength($user['aboutme'], 0, RivetyCore_Registry::get('user_about_me_length')))
				// {
				// 	$errors[] = $this->_T("About me must be less than %d characters.",RivetyCore_Registry::get('user_about_me_length'));
				// }

				// convert birthday_ts to mysql date
				//$birthday = date("Y-m-d H:i:s", $user['birthday']);

				$params = array(
					'request' => $this->getRequest(),
					'user' => $user,
					'errors' => $errors,
				);

				// // upload new avatar image if present
				// if (array_key_exists('filedata', $_FILES)) {
				// 	if ($_FILES['filedata']['tmp_name'] != '') {
				// 		$users_table = new Users();
				// 		$destination_path = $users_table->getAvatarPath($user['username']);
				// 		$destination_filename = $users_table->getAvatarPath($user['username'], true);
				// 		if (!is_dir($destination_path)) {
				// 		  	mkdir($destination_path, 0777, true);
				// 			RivetyCore_Log::report("Creating user folder at ".$destination_path, null, Zend_Log::DEBUG);
				// 		}
				// 		if (file_exists($destination_filename)) {
				// 			unlink($destination_filename);
				// 			RivetyCore_Log::report("Deleted existing user avatar from ".$destination_path, null, Zend_Log::DEBUG);
				// 		} else {
				// 			RivetyCore_Log::report("User avatar did not exist in ".$destination_path, null, Zend_Log::DEBUG);
				// 		}
				// 		move_uploaded_file($_FILES['filedata']['tmp_name'], $destination_filename);
				// 		Users::clearUserCache($user['username']);
				// 		RivetyCore_Log::report("User avatar uploaded to ".$destination_path, null, Zend_Log::DEBUG);
				// 		$params['user']['hasnewfile'] = true;
				// 	} else {
				// 		$params['user']['hasnewfile'] = false;
				// 	}
				// }

				$additional = $this->_rivety_plugin->doFilter($this->_mca."_pre_save", $params); // FILTER HOOK
				$errors = $additional['errors'];
				$user = $additional['user'];
				// if (strlen($user['full_name']) < 1) $user['full_name'] = $this->_T("Unidentified User");
				if (count($errors) == 0)
				{
					$data = array(
						'email' => $user['email'],
						// 'full_name' => $user['full_name'],
						// 'birthday' => $birthday,
						// 'aboutme' => nl2br($user['aboutme']),
						// 'gender' => $user['gender'],
						// 'country_code' => $user['country_code'],
						//'tags' => serialize($tag_array),
						'last_modified_on' => date(DB_DATETIME_FORMAT),
					);
					if ($user['password'] != "") {
						$data['password'] = $user['password'];
					}

					$where = $this->_users_table->getAdapter()->quoteInto('username = ?', $this->_username);
					$this->_users_table->update($data, $where);
					$this->_rivety_plugin->doAction('default_user_edit_post_save', array('username' => $this->_username)); // ACTION HOOK
					$this->view->success = $this->_T("Profile Updated.");

				} else {
					$this->view->errors = $errors;
				}

			}
			//$this->view->tags = RivetyCore_Common::makeTagString($tags);

			// $this->view->end_year = -(RivetyCore_Registry::get('minimum_registration_age'));
			// multiply min age by number of seconds in a year
			// $this->view->genders = RivetyCore_Common::getGenderArray();
			// $user['aboutme'] = RivetyCore_Common::br2nl(stripslashes($user['aboutme']));
			$this->view->user = $user;
		}
	}
Example #3
0
	function editAction()
	{
		$request = new RivetyCore_Request($this->getRequest());
		$roles_table = new Roles();
		$role = null;

		if ($request->has('id'))
		{
			if (!is_null($request->id))
			{
				$role = $roles_table->fetchRow($roles_table->select()->where("id = ?",$request->id));

				if (!is_null($role))
				{
					// we do not edit the guest role
					if ($role->shortname == "guest")
					{
						$this->_redirect("/default/role");
					}
					$this->view->role 			= $role->toArray();
					$this->view->role_tree 		= $roles_table->getRoleTree(null,$role->id);
					$this->view->inherited_ids 	= $roles_table->getInheritedRoles($role->id);
				}
			}
		}

		if (is_null($role))
		{
			$this->view->role_tree = $roles_table->getRoleTree();
		}

		if ($this->getRequest()->isPost())
		{
			$errors = array();
			if ($request->has('inherit_role'))
			{
				$parents = array();
				foreach ($request->inherit_role as $inherit_role)
				{
					$parents = array_merge($parents, $roles_table->getAllAncestors($inherit_role));
				}
				$inherit_ids = array();
				foreach ($request->inherit_role as $inherit_role)
				{
					if (!in_array($inherit_role, $parents))
					{
						$inherit_ids[] = $inherit_role;
					}
				}
			}
			if ($request->has('shortname'))
			{
				$shortname = $request->shortname;
				if (!RivetyCore_Validate::checkLength($request->shortname, 1, 255))
				{
					$errors[] = $this->_T("Shortname must be between 1 and 255 chars.");
				}
			}
			else
			{
			  $errors[] = $this->_T("Shortname is a requried field.");
			}

			$description = $request->description;
			$isadmin = (int)$request->checkbox('isadmin');
			if (count($errors) == 0)
			{
				$data = array(
					'shortname' => $shortname,
					'description' => $description,
					'isadmin' => $isadmin,
				);
				$id = (int)$this->_request->getPost('id');
				if ($id != 0)
				{
					$where = 'id = ' . $id;
					$roles_table->update($data, $where);
				}
				else
				{
					$id = $roles_table->insert($data);
				}
				$roles_table->removeInheritedRole($id);
				foreach ($inherit_ids as $in_id)
				{
					$roles_table->setInheritedRole($id,$in_id);
				}
				$this->_redirect("/default/role/index");
			}
			else
			{
			  $this->view->errors = $errors;
			}
		}
		if ($request->has('id'))
		{
			$id = $request->id;
			if ($id > 0)
			{
				$this->view->role = $roles_table->fetchRow('id = ' . $id)->toArray();
			}
			$this->view->inherited_ids 	= $roles_table->getInheritedRoles($id);
		}
		else
		{
			foreach ($roles_table->fetchAll()->toArray() as $role)
			{
				$role_choices[$role['id']] = $role['shortname'];
			}
			$this->view->role_choices = $role_choices;
		}
		$this->view->breadcrumbs = array(
			'Roles' => '/default/role/index',
			($request->has('id') && !empty($request->id)) ? 'Edit Role: ' . $role['shortname'] : 'New Role' => null,
		);
	}