public function save($sys_object_id, $row_id = '') { $sys_object = new Crud_Models_Object(); $sys_object->load(intval($sys_object_id)); $sys_object->loadFieldsForObject(); if ($sys_object->is_system == 1 && $this->user->role->safe_title != 'administrator') { $response = array(); $response['errors'] = array('Can\'t edit this object'); echo json_encode($response); return; } if (!$this->user->role->canExecuteAction('can_edit_' . $sys_object->table_name)) { $response = array(); $response['errors'] = array('Can\'t edit this object'); echo json_encode($response); return; } if ($sys_object->custom_crud_class != '' && new $sys_object->custom_crud_class() instanceof Crud_Interfaces_Crud) { echo call_user_func(array(new $sys_object->custom_crud_class(), 'save'), $sys_object_id, $row_id); } else { $object = new Libraries_Db_Mysql_Model($sys_object->table_name); if (!empty($row_id)) { $object->load(intval($row_id)); if (isset($_POST['last_modify']) && $_POST['last_modify'] != $object->last_modify) { $response = array(); $response['errors']['general'] = array('Object is already saved by another user!'); echo json_encode($response); return; } } $response = array(); $response['errors'] = array(); $response['message'] = ''; foreach ($sys_object->sys_field_list as $field) { if ($field->is_hidden == 1) { continue; } $field->loadValidators(); $field_value = isset($_POST[$field->field_name]) ? $_POST[$field->field_name] : null; if (!empty($field->validators)) { foreach ($field->validators as $validator) { $validator_response = call_user_func(array($validator->class, 'validate'), $field_value); if (!$validator_response) { $response['errors'][] = array('field' => $field->field_name, 'message' => call_user_func(array($validator->class, 'getFalseMessage'))); continue 2; // goto next field } } } $object->{$field->field_name} = $field_value; } unset($object->data['last_modify']); /* echo '<pre>'; print_r($_POST); echo '</pre>'; echo '<pre>'; print_r($object); echo '</pre>'; exit; */ if (empty($response['errors'])) { $response['message'] = 'Object is saved'; Libraries_Event::trigger('beforeCrudSave', $sys_object, $object); $object->save(); $response['id'] = $object->id; Libraries_Event::trigger('afterCrudSave', $sys_object, $object); } echo json_encode($response); } }
<?php Libraries_Event::registerEventHandler('beforeCrudSave', 'Crud_Events_Events', 'orderIndexBeforeSaveEvent'); Libraries_Event::registerEventHandler('afterCrudSave', 'Crud_Events_Events', 'logActionAfterSave');
<?php header('Content-Type: text/html; charset=utf-8'); session_start(); !defined('PROJECT_ROOT_DIR') ? define('PROJECT_ROOT_DIR', dirname(__FILE__)) : ''; // define project root if not defined !defined('APPLICATION_ROOT_DIR') ? define('APPLICATION_ROOT_DIR', PROJECT_ROOT_DIR . DIRECTORY_SEPARATOR . 'application') : ''; // define application root paths !defined('ADMIN_ROOT_DIR') ? define('ADMIN_ROOT_DIR', PROJECT_ROOT_DIR . DIRECTORY_SEPARATOR . 'administration') : ''; // define admin root paths !defined('SITE_ROOT_URI') ? define('SITE_ROOT_URI', 'http://' . $_SERVER['HTTP_HOST'] . '/adm') : ''; // !defined('APPLICATUIN_ROOT_URI') ? define('APPLICATUIN_ROOT_URI', SITE_ROOT_URI . '/application') : ''; // require_once PROJECT_ROOT_DIR . '/libraries/loader.php'; Libraries_Loader::getInstance()->register(); Libraries_Event::registerEvents(ADMIN_ROOT_DIR); Libraries_Event::registerEvents(APPLICATION_ROOT_DIR); $enviorement = Libraries_Environment_Factory::getEnvironment(); $request = Libraries_Request::getInstance(); // only one request per call $router = Libraries_Router::getInstance(); $router->route($request);