/** * Set avatar from social account * * @param type $id */ public function avatar_action($id) { $loginza = new Db_ORM('users_loginza'); $loginza->id = $id; if ($loginza->find() && ($this->user->id == $id or access('loginza delete_all'))) { if ($loginza->photo) { $user = new User_Object(); $user->id = $this->user->id; $path = UPLOADS . DS . 'avatars' . DS . $this->user->id . DS . basename($loginza->photo); copy($loginza->photo, $path); $user->avatar = Url::toUri($path, UPLOADS); $user->save(); Ajax::json(array('action' => 'reload')); } } else { Ajax::denied(); } }
/** * Set value from request * * @return mixed */ public function result() { $image = new Upload_Image($this->name, $this->getAttributes(), $this->validators->findByValue('Required')); if ($result = $image->upload()) { $this->is_fetched = TRUE; $this->image = $image->getInfo(); $this->value = $result; } else { $this->errors = $image->errors; } if ($this->form->is_ajaxed) { $result = array('value' => $this->value, 'file' => Url::toUri(UPLOADS . $this->value), 'width' => $this->image->width, 'height' => $this->image->height, 'errors' => $this->errors ? strip_tags(implode("\n", $this->errors)) : ''); Ajax::json($result); } // if there is hidden field with the same name — take value from there if (isset($this->form->request[$this->name])) { $this->value = $this->form->request[$this->name]; } return $this->value; }
/** * Delete post * * @param type $cid */ public function delete_action($post_id) { $post = new Post(); $post->id = $post_id; if ($post->find() && access('Post.delete.all')) { if ($post->delete()) { $message = t('Пост удалён'); if (Ajax::is()) { $data['success'] = TRUE; $data['messages'] = array(array('type' => 'success', 'body' => $message)); $data['redirect'] = server('referer'); $ajax = new Ajax(); $ajax->json($data); } $post = new Post(); $post->id = $post->post_id; flash_success($message); back(-2); } } }
/** * Диспатчер ajax-запросов * * @param string $action * @return JSON */ public function ajax_action($action = NULL) { $ajax = new Ajax(); switch ($action) { case 'saveItemsTree': if (!($menu = menu($param))) { return event('empty'); } if ($this->rebuildItemsTree($this->input->post('items'))) { $ajax->success = TRUE; $ajax->message(t('Структура сохранена!')); } else { $ajax->success = FALSE; $ajax->message(t('Не удалось сохранить структуру'), 'error', t('Ошибка')); } break; case 'order': default: if ($menus = $this->input->post('menus')) { foreach ($menus as $id => $position) { if ($menu = menu($id)) { $menu->update(array('position' => $position)); } } } } $ajax->json(); }
/** * Result * * @return NULL|Core_ArrayObject — filtered and validated data */ public function result() { // Define if form is requested via ajaxed $this->ajaxed = $this->options->ajax && Ajax::is() && cogear()->input->post('ajaxed') === $this->getId(); $method = strtolower($this->options->method); $result = array(); $is_valid = TRUE; if (cogear()->input->{$method}() or $_FILES) { foreach ($this->elements as $name => $element) { $value = $element->result(); if ($value !== NULL) { $result[$name] = $value; } if ($value === FALSE) { $is_valid = FALSE; } } } if ($this->ajaxed) { $data = array(); $data['success'] = $is_valid && $result; if ($data['success']) { $data['result'] = $result; } else { foreach ($this->elements as $key => $element) { $element->errors->count() > 0 && ($data['errors'][$key] = $element->errors); } $ajax = new Ajax(); $ajax->json($data); } } if ($is_valid && $result) { $result = Core_ArrayObject::transform($result); } if (!event('form.result.' . $this->options->name, $this, $is_valid, $result)->check() or !event('form.result', $this, $is_valid, $result)->check()) { return FALSE; } return $is_valid ? $result : FALSE; }
public function upload_action($type) { $ajax = new Ajax(); $ajax->success = FALSE; switch ($type) { case 'editor/image': $form = new Form(array('#name' => 'file.upload.editor.image', 'file' => array('type' => 'image', 'allowed_types' => array('jpg', 'png', 'gif'), 'maxsize' => '300Kb', 'path' => Image::uploadPath(), 'validate' => array('Required'), 'rewrite' => TRUE, 'preset' => 'image.large'))); if ($result = $form->result()) { if ($result->file) { $ajax->code = Image::getThumbCode($result->file, 'image.medium'); $ajax->success = TRUE; } } break; } $ajax->json(); }
/** * Gears dispatcher * * @param string $action * @param string $gear */ public function index($action = NULL, $gear = NULL) { if (!access('admin gears')) { Ajax::denied(); } switch ($action) { case 'activate': cogear()->activate($gear); $tpl = new Template('Gears.item'); $tpl->assign($this->{$gear}->info()); Ajax::json(array('items' => array(array('id' => 'gear-' . $gear, 'action' => 'replace', 'code' => $tpl->render())))); break; case 'deactivate': cogear()->deactivate($gear); $tpl = new Template('Gears.item'); $name = strtolower($gear); $tpl->assign($this->{$name}->info()); Ajax::json(array('items' => array(array('id' => 'gear-' . $gear, 'action' => 'replace', 'code' => $tpl->render())))); break; } }
/** * Access */ public function upload_action() { if (Ajax::is() && $_FILES['images']) { $image = new Image_Upload(array('name' => 'images', 'allowed_types' => array('png', 'jpg', 'gif'), 'maxsize' => '100Kb', 'overwrite' => TRUE, 'path' => Image::uploadPath(), 'preset' => 'post.large')); $files = $image->upload(); $data = array(); $ajax = new Ajax(); if ($image->uploaded) { $data['success'] = TRUE; $data['code'] = ''; foreach ($files as $file) { if ($file->uri_full) { $data['code'] .= template('Image/templates/insert', array('image' => $file))->render(); } } } else { $data['success'] = FALSE; if ($image->errors) { $data['messages'][] = array('type' => 'error', 'body' => implode('<br/>', $image->errors)); } } $ajax->json($data); } }
/** * Управление виджетами * * @param mixed $action */ public function widgets_action($action = 'list', $subaction = NULL) { $this->hookAdminMenu(); new Menu_Tabs(array('name' => 'admin.theme.widgets', 'elements' => array(array('label' => icon('list') . ' ' . t('Список'), 'link' => l('/admin/theme/widgets')), array('label' => icon('plus') . ' ' . t('Добавить'), 'link' => l('/admin/theme/widgets/add') . e('uri', $this->input->get('uri')), 'class' => 'fl_r'), array('label' => icon('pencil') . ' ' . t('Редактировать'), 'link' => l('/admin/theme/widgets/' . $this->router->getSegments(3)), 'access' => check_route('widgets/(\\d+)'), 'active' => check_route('widgets/(\\d+)$'), 'class' => 'fl_r'), array('label' => icon('wrench') . ' ' . t('Настройки'), 'link' => l('/admin/theme/widgets/' . $this->router->getSegments(3) . '/options'), 'access' => check_route('widgets/(\\d+)'), 'active' => check_route('widgets/(\\d+)/options'), 'class' => 'fl_r')))); if ($action == 'ajax' && ($widgets = $this->input->post('widgets'))) { $ajax = new Ajax(); $position = 0; foreach ($widgets as $config) { if ($widget = widget($config['id'])) { $widget->region = $config['region']; $widget->position = ++$position; $widget->save(); } } $this->cache->remove('widgets'); $ajax->success = TRUE; $ajax->json(); } else { if ($action == 'list') { jqueryui(); template('Theme/templates/widgets/search')->show(); $widget = widget(); $widget->order('position'); $widgets = $widget->findAll(); // Фильтруем, если задан параметр if ($uri = $this->input->get('uri')) { $remove_ids = array(); foreach ($widgets as $key => $widget) { if (!check_route($widget->route, $uri)) { $remove_ids[] = $key; } } foreach ($remove_ids as $id) { $widgets->offsetUnset($id); } } template('Theme/templates/widgets/list', array('regions' => $this->regions, 'widgets' => $widgets))->show(); } else { $form = new Form('Theme/forms/widget'); $form->callback->setValues($this->getWidgets()); $form->region->setValues(array_combine($this->regions, $this->regions)); if (is_numeric($action) && ($widget = widget($action))) { if ($subaction === 'options') { template('Theme/templates/widgets/options.header', array('widget' => $widget))->show(); $class = $widget->callback; $current_widget = new $class($widget->object()->options); $current_widget->object($widget); if ($current_widget->settings()) { $this->cache->remove('widgets'); flash_success(t('Настройки виджета <b>%s</b> сохранены!', $widget->name), '', 'growl'); redirect(l(TRUE)); } return; } $form->object($widget); $form->callback->options->disabled = TRUE; } elseif ($action == 'add') { if ($uri = $this->input->get('uri')) { $form->route->setValue($uri); } if ($region = $this->input->get('region')) { $form->region->setValue($region); } $form->remove('delete'); $widget = widget(); } else { return event('empty'); } if ($result = $form->result()) { $this->cache->remove('widgets'); if ($result->delete && $widget->delete()) { flash_success(t('Виджет «<b>%s</b>»успешно удалён!', $widget->name), '', 'growl'); redirect(l('/admin/theme/widgets')); } if ($action == 'add') { $result->position = 1 + widget()->where('region', $result->region)->countAll(); } $widget->object()->extend($result); if ($widget->save()) { if ($action == 'add') { flash_success(t('Виджет «<b>%s</b>» успешно добавлен!', $widget->name), '', 'growl'); redirect(l('/admin/theme/widgets/' . $widget->id . '/options')); } else { flash_success(t('Виджет «<b>%s</b>» успешно отредактирован!', $widget->name)); redirect(l(TRUE)); } } } $form->show(); } } }
/** * 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; }
/** * Ajax интерцептор * * @param string $action * @param mixed $param */ public function admin_ajax($action = 'getLink', $param = NULL) { $ajax = new Ajax(); switch ($action) { case 'getLink': if ($page = page($param)) { $ajax->success = TRUE; $ajax->link = trim($page->getLink(), '/'); } else { $ajax->success = FALSE; } break; case 'saveDBtree': if ($pages = $this->rebuildPagesTree($this->input->post('items'))) { $ajax->success = TRUE; $ajax->message(t('Структура сохранена!')); } else { $ajax->success = FALSE; $ajax->message(t('Не удалось сохранить структуру'), 'error', t('Ошибка')); } break; } $ajax->json(); }