/** * Fonction appelée lorsque l'on clique sur le bouton 'import' * Appel la méthode privée _validFromFormImportParams * vérifie les infos saisies dans le formulaire * stock l'objet en session * @author Audrey Vassal <*****@*****.**> */ public function doExport() { $serviceAuth = new AgendaAuth(); $serviceExport = new ExportService(); $agendaService = new AgendaService(); $dateService = new DateService(); //demande de mettre l'objet à jour en fonction des valeurs saisies dans le formulaire if (!($exportParams = $this->_getSessionExport())) { return CopixActionGroup::process('genericTools|Messages::getError', array('message' => CopixI18N::get('agenda.error.cannotFindSession'), 'back' => CopixUrl::get('agenda|agenda|vueSemaine'))); } //on vérifie les droits des utilisateurs sur la liste des agendas sélectionnés foreach ((array) $this->getRequest('agenda') as $id_agenda) { //on vérifie si l'utilisateur a les droits d'écriture sur un des agendas affiché if ($serviceAuth->getCapability($id_agenda) < $serviceAuth->getRead()) { return CopixActionGroup::process('genericTools|Messages::getError', array('message' => CopixI18N::get('agenda.error.enableToWrite'), 'back' => CopixUrl::get('agenda|agenda|vueSemaine'))); } } $this->_validFromFormExportParams($exportParams); $errors = $this->_checkExport($exportParams); if (count($errors) > 0) { $this->_setSessionExport($exportParams); return CopixActionGroup::process('agenda|ImportExport::getPrepareExport', array('e' => 1, 'errors' => $errors)); } else { //var_dump($exportParams); //die(); //on récupère tous les évènements des agendas cochés dans la période demandée foreach ((array) _request('agenda') as $idAgenda) { $arEventsPeriode[$idAgenda] = $agendaService->checkEventOfAgendaInBdd($idAgenda, CopixDateTime::dateToYYYYMMDD($exportParams->datedeb_export), CopixDateTime::dateToYYYYMMDD($exportParams->datefin_export)); } //on classe ces évènements par jour $arEventByDay = $agendaService->getEventsByDay($arEventsPeriode, CopixDateTime::dateToYYYYMMDD($exportParams->datedeb_export), CopixDateTime::dateToYYYYMMDD($exportParams->datefin_export)); //on ordonne les évènements par ordre croissant d'heure de début d'évènement dans la journée //var_dump($arEventByDay); $arEventByDay = $agendaService->getEventsInOrderByDay($arEventByDay); $content = $serviceExport->getFileICal($arEventByDay, CopixDateTime::dateToTimestamp($exportParams->datedeb_export), CopixDateTime::dateToTimestamp($exportParams->datefin_export)); } //on vide la session $this->_setSessionExport(null); return _arContent($content, array('filename' => 'agenda.ics', 'content-disposition' => 'attachement', 'content-type' => CopixMIMETypes::getFromExtension('.ics'))); }
public function actionExport() { $infoType = $this->getInfoType(); $productId = $this->getProductId($infoType); $fieldCookieKey = $productId . '_' . $infoType . '_showField'; $cookieShowFieldStr = $this->getShowFieldCookie($fieldCookieKey); if (!empty($cookieShowFieldStr) && is_string($cookieShowFieldStr)) { $showFieldArr = CommonService::splitStringToArray(',', $cookieShowFieldStr); } else { $showFieldArr = SearchService::getDefaultShowFieldArr($infoType); } $searchRowArr = Yii::app()->user->getState($productId . '_' . $infoType . '_search'); $filterSql = Yii::app()->user->getState($productId . '_' . $infoType . '_filterSql'); $orderArr = Yii::app()->user->getState($productId . '_' . $infoType . '_sortArr'); if (Info::TYPE_BUG == $infoType) { $showFieldArr[] = 'repeat_step'; } else { if (Info::TYPE_CASE == $infoType) { $showFieldArr[] = 'case_step'; } else { if (Info::TYPE_RESULT == $infoType) { $showFieldArr[] = 'result_step'; } } } if (!in_array('id', $showFieldArr)) { array_unshift($showFieldArr, 'id'); } $exportDataResult = ExportService::getExportData($infoType, $searchRowArr, $productId, $showFieldArr, $orderArr, $filterSql); if (CommonService::$ApiResult['SUCCESS'] == $exportDataResult['status']) { $rawData = $exportDataResult['detail']; } else { throw new CHttpException(400, $exportDataResult['detail']); } $fileStoreUrl = Yii::app()->params->uploadPath . '/' . $infoType . 'list.xml'; $file = fopen($fileStoreUrl, "w"); $searchFieldConfig = SearchService::getSearchableFields($infoType, $productId); $searchFieldConfig['case_step'] = array('label' => Yii::t('CaseInfo', 'case_step'), 'isBasic' => true); $searchFieldConfig['result_step'] = array('label' => Yii::t('ResultInfo', 'result_step'), 'isBasic' => true); $searchFieldConfig['repeat_step'] = array('label' => Yii::t('BugInfo', 'repeat_step'), 'isBasic' => true); $searchFieldConfig['action_note'] = array('label' => Yii::t('Common', 'action_note'), 'isBasic' => false); $showFieldArr[] = 'action_note'; $content = ExportService::exportXML($rawData, $showFieldArr, $searchFieldConfig); file_put_contents($fileStoreUrl, $content); header('Content-type: text/xml; charset=utf-8'); header('Content-Disposition: attachment; filename=' . $infoType . 'list.xml'); readfile($fileStoreUrl); // exit; //enable this will not output log }
public function index() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //$profiler = new Profiler; //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); $query_struct = array('where' => array('status' => ProductService::PRODUCT_STATUS_PUBLISH), 'orderby' => array(), 'limit' => array()); switch (trim($request_data['type'])) { case 'category': $query_struct['orderby']['id'] = 'ASC'; if (!isset($request_data['id']) or !preg_match('/^\\d+$/', $request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $category = CategoryService::get_instance()->get($request_data['id']); $category['sub_ids'] = trim($category['sub_ids']); if (empty($category['sub_ids'])) { $query_struct['where']['category_id'] = $category['id']; } else { $sub_ids = explode(',', $category['sub_ids']); array_push($sub_ids, $category['id']); $query_struct['where']['category_id'] = $sub_ids; } break; case 'classify': $query_struct['orderby']['id'] = 'ASC'; if (!isset($request_data['id']) or !preg_match('/^\\d+$/', $request_data['id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $categorys = CategoryService::get_instance()->query_assoc(array('where' => array('classify_id' => $request_data['id']))); if (empty($categorys)) { throw new MyRuntimeException(Kohana::lang('o_product.export_pdt_not_found')); } $query_struct['where']['category_id'] = array(); foreach ($categorys as $category) { $query_struct['where']['category_id'][] = $category['id']; } break; default: if (!empty($request_data['product_id'])) { $request_data['product_id'] = explode('-', $request_data['product_id']); foreach ($request_data['product_id'] as $item) { if (!preg_match('/^\\d+$/', $item)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } } $query_struct['where']['id'] = $request_data['product_id']; } if (isset($request_data['category_id'])) { if (!preg_match('/^\\d+$/', $request_data['category_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['where']['category_id'] = $request_data['category_id']; } if (isset($request_data['brand_id'])) { if (!preg_match('/^\\d+$/', $request_data['brand_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['where']['brand_id'] = $request_data['brand_id']; } if (!empty($request_data['title'])) { $query_struct['where']['title'] = trim($request_data['title']); } if (!empty($request_data['name_manage'])) { $query_struct['where']['name_manage'] = trim($request_data['name_manage']); } if (!empty($request_data['sku'])) { $query_struct['where']['sku'] = trim($request_data['sku']); } if (isset($request_data['orderby'])) { if (!is_array($request_data['orderby'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } foreach ($request_data['orderby'] as $item) { $item = explode('-', $item); if (count($item) != 2) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['orderby'][$item[0]] = $item[1] === '0' ? 'ASC' : 'DESC'; } } if (isset($request_data['page'])) { if (!preg_match('/^\\d+$/', $request_data['page'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['limit']['page'] = $request_data['page']; if (!isset($request_data['per_page']) or !preg_match('/^\\d+$/', $request_data['per_page'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $query_struct['limit']['per_page'] = $request_data['per_page']; } } $products = ProductService::get_instance()->query_assoc($query_struct); if (empty($products)) { throw new MyRuntimeException(Kohana::lang('o_product.export_pdt_not_found')); } $csv = ExportService::get_instance()->run($products); $csv = csv::encode($csv); $csv = iconv('UTF-8', 'GBK//IGNORE', $csv); if ($this->is_ajax_request()) { $fid = uniqid(); $dir = Kohana::config('product.export_tmp_dir'); $dir = rtrim(trim($dir), '/'); if (!is_dir($dir) && !@mkdir($dir, 0777, TRUE)) { throw new MyRuntimeException(Kohana::lang('o_product.export_cte_tmpdir_failed')); } $filename = $dir . '/' . $fid . '.csv'; if (!@file_put_contents($filename, $csv)) { throw new MyRuntimeException(Kohana::lang('o_product.export_wte_tmp_failed')); } } else { @header('Cache-control: private'); @header('Content-Disposition: attachment; filename=' . 'export-' . date('Ymd', time()) . '.csv'); @header('Content-type: text/csv; charset=GBK'); echo $csv; exit; } //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = ''; $return_struct['content'] = url::base() . 'product/export/download?fid=' . $fid; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 //exit('Not Implemented'); $this->template->content = $return_struct; } else { // html 输出 //* 模板输出 */ $content = new View($this->package . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ $this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->site_id = $site_id; $this->template->content->sites = $sites; $this->template->content->classifies_html = $html; } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $this->template->return_struct = $return_struct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }
/** * query * * @param string $xml * @param string $schema * @return array */ public function query($xml, $schema, $productId, $compatible = false) { $code = API::ERROR_NONE; $info = ''; if (empty($xml)) { $code = API::ERROR_QUERY_EMPTY; $info = Yii::t('API', 'query empty error info'); } else { Yii::app()->request->stripSlashes($xml); $xml = preg_replace("/>\\s+</", "><", $xml); libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadXML($xml); if ($dom->schemaValidateSource($schema)) { $query = $dom->getElementsByTagName('query'); $query = $query->item(0); $infoType = strtolower($query->getAttribute('table')); $showFieldArr = $query->getAttribute('select') ? explode(',', $query->getAttribute('select')) : null; $order = $query->getAttribute('order') ? $query->getAttribute('order') : null; $isAsc = $query->getAttribute('asc') ? $query->getAttribute('asc') : false; $orderArr = $order ? array($order => $isAsc) : null; $currentPage = $query->getAttribute('page') ? $query->getAttribute('page') : 1; $pageSize = $query->getAttribute('size') ? $query->getAttribute('size') : 100; $arr = array(); $searchRowArr = $this->getSearchRowArr($query->firstChild, $arr, $infoType, $compatible); $result = ExportService::getExportData($infoType, $searchRowArr, $productId, $showFieldArr, $orderArr, null, $pageSize, $currentPage); $detail = $result['detail']; if (CommonService::$ApiResult['FAIL'] == $result['status']) { $code = API::ERROR_QUERY; $info = $detail; } else { $list = array(); if ('count(*)' == $showFieldArr[0]) { $list = $detail; } else { foreach ($detail as $val) { $id = $val['id']; if ($compatible) { foreach ($val as $key => $field) { unset($val[$key]); $key = $this->fieldNew2Old($key, $infoType); $val[$key] = $field; } } $list[$id] = $val; } } $info['QueryList'] = $list; $info['size'] = $pageSize; $info['page'] = $currentPage; } } else { $code = API::ERROR_QUERY_EMPTY; $info = Yii::t('API', 'query xml invalid info'); } } return array($code, $info); }
public function export() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->get(); if (empty($request_data['classify_id'])) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } $classify_id = $request_data['classify_id']; if ($classify_id > 0) { $classify = ClassifyService::get_instance()->get($request_data['classify_id']); } else { $classify_id = 0; } $export = ExportService::get_instance()->get_titlebar($classify_id); $csv = csv::encode(array($export)); header('Cache-control: private'); header('Content-Disposition: attachment; filename=import.csv'); header('Content-type: text/csv; charset=GBK'); echo iconv('UTF-8', 'GBK//IGNORE', $csv); exit; //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = 'Success'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template->return_struct = $return_struct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { $this->template->return_struct = $return_struct; $content = new View('info'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }