public function getAjaxArticles() { try { $page = \Arr::get($_GET, 'page', null); $itemId = (int) \Arr::get($_GET, 'item', null); $keyword = \Arr::get($_GET, 'keyword', null); if (empty($page)) { throw new \Exception("Error request [10]"); } $page = (int) $page; $limit = 10; $offset = ($page - 1) * $limit; $cmd = \DB::table('wintness')->select('wintness.id', 'wintness.title', 'wintness.cover', 'wintness.background_color', 'wintness.description')->where('wintness.status', '=', '1')->orderBy('wintness.sort', 'desc')->orderBy('wintness.updated_at', 'desc')->skip($offset)->take($limit); if ($itemId >= 1) { $cmd = $cmd->join('wintness_labels', 'wintness.id', '=', 'wintness_labels.wid')->where('wintness_labels.label_id', '=', $itemId); } if (!empty($keyword)) { $format = '(`tabs`.`title` like "%s" or `tabs`.`content` like "%s" or `wintness`.`title` like "%s" or `wintness`.`description` like "%s")'; $keyword = '%' . $keyword . '%'; $cmd = $cmd->join('tabs', 'wintness.id', '=', 'tabs.item_id')->where('tabs.type', '=', 'wintness')->whereRaw(sprintf($format, $keyword, $keyword, $keyword, $keyword))->groupBy('wintness.id'); } $rows = $cmd->get(); $data = array(); if (!empty($rows)) { foreach ($rows as $row) { $cover = json_decode($row->cover, true); $row->cover = !empty($cover) ? $cover[0]['image'] : 'null'; $labels_service = \WintnessLabels::where('wid', '=', $row->id)->lists('label_id'); $services = null; if (!empty($labels_service)) { $services = \ServiceFaq::where('status', '=', 'Y')->where('type', '=', 'service')->whereIn('id', $labels_service)->get(array('id', 'title')); } $labelList = array(); if (!empty($services)) { foreach ($services as $idx => $label) { $label->link = \URL::route('frontend.service_faq.article', array('type' => 'service', 'id' => $label->id)); $labelList[] = $label->toArray(); } } $row->label_faq = $labelList; $row->description = \Text::preEllipsize($row->description, 46, 0.6); $data[] = (array) $row; } } return \Response::json(array('status' => 'ok', 'data' => $data, 'item' => $itemId)); } catch (Excpetion $e) { return \Response::json(array('status' => 'error', 'message' => $e->getMessage())); } }
public function postArticleAction() { try { if (!isset($_POST['id'])) { throw new Exception("Error Processing Request [10]"); } $id = (int) Arr::get($_POST, 'id', null); if (empty($id)) { $model = new Wintness(); } else { $model = Wintness::find($id); if ($model == null) { throw new Exception("Error Processing Request [11]"); } } // collect image uploader /* * (array) currentList * (string) competeKey * (array) newList * (string) deleteFieldName */ $delImages = Arr::get($_POST, 'deleteImages', array()); $delLength = sizeof($delImages); for ($i = 0; $i < $delLength; $i++) { $delLength[$i] = basename($delLength); } $imgUploaderList = array('cover' => array('fieldName' => 'cover', 'items' => null), 'before' => array('fieldName' => 'img_before', 'items' => null), 'after' => array('fieldName' => 'img_after', 'items' => null), 'gallery' => array('fieldName' => 'gallery', 'items' => null)); foreach ($imgUploaderList as $key => $val) { $imgs = json_decode($model->{$val}['fieldName']); if (!empty($imgs) && sizeof($imgs) > 0) { foreach ($imgs as $img) { if ($delLength > 0 && in_array($img->id, $delImages)) { fps::getInstance()->delete($img->image); } } } $list = array(); $descFieldName = $val['fieldName'] . '_desc'; $imagesDesc = Input::get($descFieldName, array()); $images = Input::get($val['fieldName'], array()); foreach ($images as $idx => $image) { $list[] = array('id' => basename($image), 'image' => $image, 'text' => $imagesDesc[$idx]); } $imgUploaderList[$key]['items'] = $list; } $status = (int) Arr::get($_POST, 'status', 0); $isInSiderbar = (int) Arr::get($_POST, 'isInSiderbar', 0); $model->title = Input::get('title'); $model->background_color = Input::get('background_color', '#ccc'); $model->cover = json_encode($imgUploaderList['cover']['items']); $model->img_before = json_encode($imgUploaderList['before']['items']); $model->img_after = json_encode($imgUploaderList['after']['items']); $model->description = Input::get('description', ''); $model->gallery = json_encode($imgUploaderList['gallery']['items']); $model->status = $status % 2; $model->isInSiderbar = $isInSiderbar % 2; $model->created_at = time(); $model->updated_at = time(); $model->save(); WintnessLabels::where('wid', '=', $model->id)->delete(); $types = array('service', 'faq'); foreach ($types as $type) { $fieldName = 'label_' . $type; $labels = Input::get($fieldName, array()); foreach ($labels as $label) { WintnessLabels::create(array('wid' => (int) $model->id, 'label_id' => (int) $label)); } } Tabs::where('type', '=', 'wintness')->where('item_id', '=', $model->id)->delete(); // collect tabs $tabContents = Input::get('tabContents', array()); $tabs = array(); $tabName = Input::get('tabName', array()); $order = 1; foreach ($tabName as $key => $tab) { if (!isset($tabContents[$key])) { continue; } else { Tabs::create(array('type' => 'wintness', 'item_id' => $model->id, 'title' => $tab, 'content' => $tabContents[$key], 'sort' => $order++)); } } return Redirect::route('admin.wintness.article.list', array('page' => 1, 'message' => 'success')); } catch (Exception $e) { return Redirect::back()->withInput()->withErrors($e->getMessage()); } }