summaryrefslogtreecommitdiff
path: root/modules/forge/models/user_edit.php
blob: c8cf9594b18c7424e06d17f2e2b2e133a134150e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

class User_Edit_Model extends User_Model {

	// Overload the class
	protected $class = 'user';

	// Forge instance
	protected $form;

	public function __construct($action, $title, $id = FALSE)
	{
		// Load the user
		parent::__construct($id);

		// Create the form
		$this->form = new Forge($action, $title);

		$this->form->input('username')->label(TRUE)->rules('required|length[5,32]')->value($this->object->username);
		$this->form->input('email')->label(TRUE)->rules('required|length[5,127]|valid_email')->value($this->object->email);
		$this->form->password('password')->label(TRUE)->rules('length[5,64]');
		$this->form->password('confirm')->label(TRUE)->matches($this->form->password);

		// Make sure that the username does not already exist
		$this->form->username->callback(array($this, 'is_existing_user'));

		if ($this->object->id == 0)
		{
			// Password fields are required for new users
			$this->form->password->rules('+required');
		}

		// // Find all roles
		// $roles = new Role_Model;
		// $roles = $roles->find(ALL);
		// 
		// $options = array();
		// foreach ($roles as $role)
		// {
		// 	// Add each role to the options
		// 	$options[$role->name] = isset($this->roles[$role->id]);
		// }
		// 
		// // Create a checklist of roles
		// $this->form->checklist('roles')->options($options)->label(TRUE);

		// Add the save button
		$this->form->submit('Save');
	}

	public function is_existing_user($input)
	{
		if ($this->object->username == $input->value)
			return TRUE;

		if (self::$db->count_records($this->table, array('username' => $input->value)) > 0)
		{
			$input->add_error(__FUNCTION__, 'The username <strong>'.$input->value.'</strong> is already in use.');
			return FALSE;
		}

		return TRUE;
	}

	public function save()
	{
		if ($this->form->validate() AND $data = $this->form->as_array())
		{
			if (empty($data['password']))
			{
				// Remove the empty password so it's not reset
				unset($data['password'], $data['confirm']);
			}

			// Need to set this before saving
			$new_user = ($this->object->id == 0);

			// Remove the roles from data
			isset($data['roles']) and $roles = arr::remove('roles', $data);

			foreach ($data as $field => $val)
			{
				// Set object data from the form
				$this->$field = $val;
			}

			if ($status = parent::save())
			{
				// if ($new_user)
				// {
				// 	foreach ($roles as $role)
				// 	{
				// 		// Add the user roles
				// 		$this->add_role($role);
				// 	}
				// }
				// else
				// {
				// 	foreach (array_diff($this->roles, $roles) as $role)
				// 	{
				// 		// Remove roles that were deactivated
				// 		$this->remove_role($role);
				// 	}
				// 
				// 	foreach (array_diff($roles, $this->roles) as $role)
				// 	{
				// 		// Add new roles
				// 		$this->add_role($role);
				// 	}
				// }
			}

			// Return the save status
			return $status;
		}

		return FALSE;
	}

	public function render()
	{
		// Proxy to form html
		return $this->form->render();
	}

	public function __toString()
	{
		// Proxy to form html
		return $this->form->render();
	}

} // End User Edit Model