public function render() { $columns = array(); $user_id = \Work\Models\User::where('session', '=', $_SESSION['frame_key'])->pluck('user_id'); if (is_null($user_id)) { $user_id = 0; } foreach ($this->inputs as $input) { $id = \Frame\Key::get(); if ($input['type'] == 'hidden') { if ($user_id) { $store = \Work\Models\Store::where('user_id', '=', $user_id)->where('key', '=', $input['value'])->pluck('store_id'); } else { $store = \Work\Models\Store::where('key', '=', $input['value'])->pluck('store_id'); } if (is_null($store)) { $hidden = \Frame\Key::get(); \Work\Models\Store::create(array('user_id' => $user_id, 'key' => $hidden, 'value' => $input['value'])); $input['value'] = $hidden; } } $columns[] = array($input['label'], $this->build('input', '', array('id' => $id, 'name' => $id, 'type' => $input['type'], 'value' => $input['value']))); if (is_null($input['input'])) { continue; } $input_id = \Work\Models\Input::where('input', '=', $input['input'])->pluck('input_id'); \Work\Models\Navigation::create(array('user_id' => $user_id, 'key' => $id, 'type' => 'input', 'navigation' => $input_id)); } $layout = new \Frame\Views\Layout($columns); $key = \Frame\Key::get(); $target_id = \Work\Models\Target::where('target', '=', $this->target)->pluck('target_id'); if ($target_id) { $target = \Work\Models\Navigation::where('user_id', '=', $user_id)->where('type', '=', 'target')->where('navigation', '=', $target_id)->orderBy('navigation_id', 'desc')->pluck('key'); } else { $navigation_id = \Work\Models\Navigation::where('type', '=', 'target')->where('key', '=', $this->target)->pluck('navigation_id'); if ($navigation_id) { $target = $this->target; } else { $target = null; } } if (is_null($target)) { $id = $target = 'body'; } else { $id = "#{$target}"; } $form = $this->build('form', $layout->render(), array('id' => $key)); $js = <<<JS \$('#{$key}').submit(function(event) { event.preventDefault(); var hidden = []; \$('{$id} [id]').each(function() { hidden.push(\$(this).attr('id')); }); console.log(hidden); var ids = []; \$('body [id]').each(function() { if (\$.inArray(\$(this).attr('id'), hidden) >= 0) return true; ids.push(\$(this).attr('id')); }); console.log(ids); \$.ajax({url: '', type: 'post', dataType: 'json', data: {key: '{$key}', form: \$(this).serializeArray(), target: '{$target}', clean: ids}}).success(function(json) { if (!json.status || json.status != 'COMPLETE' || !json.html) { var message = json.message || 'System error, check logs'; alert(message); return false; } \$('{$id}').html(json.html); }); }); JS; $controller_id = \Work\Models\Controller::where('controller', '=', $this->controller)->pluck('controller_id'); if (is_null($controller_id)) { throw new \Exception("{$this->controller} not found in controller table}"); } \Work\Models\Navigation::create(array('user_id' => $user_id, 'key' => $key, 'type' => 'controller', 'navigation' => $controller_id)); $script = $this->build('script', $js); return $form . $script; }
private static function cleanup($user_id = null) { if ($user_id && arrayGet($_REQUEST, 'clean')) { \Work\Models\Navigation::where('user_id', '=', $user_id)->whereNotIn('key', $_REQUEST['clean'])->delete(); \Work\Models\Store::where('user_id', '=', $user_id)->whereNotIn('key', $_REQUEST['clean'])->delete(); } \Work\Models\Navigation::where('user_id', '=', 0)->where('created_at', '<', date('Y-m-d H:i:s', strtotime('2 hours ago')))->delete(); \Work\Models\Store::where('user_id', '=', 0)->where('created_at', '<', date('Y-m-d H:i:s', strtotime('2 hours ago')))->delete(); }