/** * Process ajax request * * @return array */ public function ajax() { if (!$this->is_ajaxed) { return NULL; } $result = array(); $action = Ajax::get('action', 'replace'); $result['action'] = $action; $result['id'] = $this->getId(); switch ($action) { case 'replace': $this->setValue(NULL); $result['code'] = $this->render(); break; } event('form.element.ajax', $this, $result); return $result; }
/** * Edit action * * @param string $login */ public function edit_action($id = NULL) { $id or $id = $this->user->id; $user = new User_Object(); $this->db->where('id', $id); if (!$user->find()) { return _404(); } if (!access('user edit_all') && $this->id != $user->id) { return _403(); } $this->renderUserInfo($user); $user = new User_Object(); $user->where('id', $id); $user->find(); $form = new Form('User.profile'); $user->password = ''; $form->object($user->object()); if ($form->elements->avatar->is_ajaxed && Ajax::get('action') == 'replace') { $user->avatar = ''; $user->update(); } if ($result = $form->result()) { if ($user->login != $result['login']) { $redirect = Url::gear('user') . $result['login']; } if ($result->delete && access('users delete_all')) { $user->delete(); flash_success(t('User <b>%s</b> was deleted!')); redirect(Url::link('/users')); } $user->merge($result); if ($result->password) { $user->hashPassword(); } else { unset($user->password); } if ($user->update()) { d('User edit'); flash_success(t('User data saved!'), t('Success')); d(); if ($user->id == $this->id) { $this->store($user->object()->toArray()); } redirect(Url::gear('user') . $user->login); } } append('content', $form->render()); }
/** * Result * * @return NULL|Core_ArrayObject — filtered and validated data */ public function result() { $this->init(); event('form.result.before', $this); event('form.' . $this->name . '.result.before', $this); $this->request = $this->method == 'POST' ? $_POST : $_GET; $result = array(); $is_valid = TRUE; if (sizeof($this->request) > 0) { foreach ($this->elements as $name => $element) { $value = $element->result(); if ($value !== FALSE) { $result[$name] = $value; } else { $is_valid = FALSE; } } } if ($this->is_ajaxed) { $response = array(); foreach ($this->elements as $name => $element) { if ($name == Ajax::get('element')) { if ($result = $element->ajax()) { $response[$name] = $result; } } } event('form.ajax.before', $this, $response); event('form.' . $this->name . '.ajax.before', $this, $response); $response && Ajax::json($response); } event('form.result.after', $this, $is_valid, $result); event('form.' . $this->name . '.result.after', $this, $is_valid, $result); return $is_valid && $result ? Core_ArrayObject::transform($result) : FALSE; }