| 1 |
<?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 |
|
|---|
| 3 |
class User_Edit_Model extends User_Model { |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
protected $class = 'user'; |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
protected $form; |
|---|
| 10 |
|
|---|
| 11 |
public function __construct($action, $title, $id = FALSE) |
|---|
| 12 |
{ |
|---|
| 13 |
|
|---|
| 14 |
parent::__construct($id); |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
$this->form = new Forge($action, $title); |
|---|
| 18 |
|
|---|
| 19 |
$this->form->input('username')->label(TRUE)->rules('required|length[5,32]')->value($this->object->username); |
|---|
| 20 |
$this->form->input('email')->label(TRUE)->rules('required|length[5,127]|valid_email')->value($this->object->email); |
|---|
| 21 |
$this->form->password('password')->label(TRUE)->rules('length[5,64]'); |
|---|
| 22 |
$this->form->password('confirm')->label(TRUE)->matches($this->form->password); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
$this->form->username->callback(array($this, 'is_existing_user')); |
|---|
| 26 |
|
|---|
| 27 |
if ($this->object->id == 0) |
|---|
| 28 |
{ |
|---|
| 29 |
|
|---|
| 30 |
$this->form->password->rules('+required'); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
// $roles = new Role_Model; |
|---|
| 35 |
// $roles = $roles->find(ALL); |
|---|
| 36 |
// |
|---|
| 37 |
// $options = array(); |
|---|
| 38 |
// foreach ($roles as $role) |
|---|
| 39 |
// { |
|---|
| 40 |
// // Add each role to the options |
|---|
| 41 |
// $options[$role->name] = isset($this->roles[$role->id]); |
|---|
| 42 |
// } |
|---|
| 43 |
// |
|---|
| 44 |
// // Create a checklist of roles |
|---|
| 45 |
// $this->form->checklist('roles')->options($options)->label(TRUE); |
|---|
| 46 |
|
|---|
| 47 |
// Add the save button |
|---|
| 48 |
$this->form->submit('Save'); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
public function is_existing_user($input) |
|---|
| 52 |
{ |
|---|
| 53 |
if ($this->object->username == $input->value) |
|---|
| 54 |
return TRUE; |
|---|
| 55 |
|
|---|
| 56 |
if (self::$db->count_records($this->table, array('username' => $input->value)) > 0) |
|---|
| 57 |
{ |
|---|
| 58 |
$input->add_error(__FUNCTION__, 'The username <strong>'.$input->value.'</strong> is already in use.'); |
|---|
| 59 |
return FALSE; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
return TRUE; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
public function save() |
|---|
| 66 |
{ |
|---|
| 67 |
if ($this->form->validate() AND $data = $this->form->as_array()) |
|---|
| 68 |
{ |
|---|
| 69 |
if (empty($data['password'])) |
|---|
| 70 |
{ |
|---|
| 71 |
|
|---|
| 72 |
unset($data['password'], $data['confirm']); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
$new_user = ($this->object->id == 0); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
isset($data['roles']) and $roles = arr::remove('roles', $data); |
|---|
| 80 |
|
|---|
| 81 |
foreach ($data as $field => $val) |
|---|
| 82 |
{ |
|---|
| 83 |
|
|---|
| 84 |
$this->$field = $val; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
if ($status = parent::save()) |
|---|
| 88 |
{ |
|---|
| 89 |
|
|---|
| 90 |
// { |
|---|
| 91 |
// foreach ($roles as $role) |
|---|
| 92 |
// { |
|---|
| 93 |
// // Add the user roles |
|---|
| 94 |
// $this->add_role($role); |
|---|
| 95 |
// } |
|---|
| 96 |
// } |
|---|
| 97 |
// else |
|---|
| 98 |
// { |
|---|
| 99 |
// foreach (array_diff($this->roles, $roles) as $role) |
|---|
| 100 |
// { |
|---|
| 101 |
// // Remove roles that were deactivated |
|---|
| 102 |
// $this->remove_role($role); |
|---|
| 103 |
// } |
|---|
| 104 |
// |
|---|
| 105 |
// foreach (array_diff($roles, $this->roles) as $role) |
|---|
| 106 |
// { |
|---|
| 107 |
// // Add new roles |
|---|
| 108 |
// $this->add_role($role); |
|---|
| 109 |
// } |
|---|
| 110 |
// } |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
return $status; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
return FALSE; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
public function render() |
|---|
| 121 |
{ |
|---|
| 122 |
|
|---|
| 123 |
return $this->form->render(); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
public function __toString() |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
return $this->form->render(); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
} |
|---|