Server IP : 180.180.241.3 / Your IP : 216.73.216.194 Web Server : Microsoft-IIS/7.5 System : Windows NT NETWORK-NHRC 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586 User : IUSR ( 0) PHP Version : 5.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/AppServ/www/app/Controller/ |
Upload File : |
<?php App::uses('AppController', 'Controller'); /** * Organizations Controller * * @property Organization $Organization * @property PaginatorComponent $Paginator */ class OrganizationsController extends AppController { // all actions require log in public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('admin_export'); } // for backend private function __getCommonConditions(&$conditions, &$joins) { $keyword = ''; // keyword filter if ( isset($this->params->query['filter_keyword']) && !empty($this->params->query['filter_keyword']) ) { $keyword = $this->params->query['filter_keyword']; $keywordValue = sprintf('%%%s%%', $keyword); $searchInFields = array( 'name', 'eng_name', 'short_name', 'eng_short_name', 'address1', 'address2', 'postal_code', 'telephone1', 'telephone2', 'telephone3', 'fax', 'email', 'url', 'work_level','mou_name', 'objective', 'publish'); $keywordCondition = array(); foreach ( $searchInFields as $field ) { $keywordCondition['Organization.' . $field . ' LIKE'] = $keywordValue; } $conditions['OR'] = $keywordCondition; } $this->set('keyword', $keyword); // member $isMember = 0; $memberId = 0; if ( isset($this->params->query['is_member']) && (int)$this->params->query['is_member'] == 1) { $isMember = 1; $memberId = (int)$this->params->query['member_id']; if ( $memberId != 0 ) { // $conditions['AND']['Organization.member_id'] = $memberId; // joins $joins[] = array( 'alias' => 'IJ_OrganizationsMembership', 'table' => 'org_memberships', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationsMembership`.`organization_id`', '`IJ_OrganizationsMembership`.`member_id` = ' . $memberId ), ); } } $this->set('is_member', $isMember); $this->set('member_id', $memberId); $typeId = ''; // type filter if ( isset($this->params->query['filter_type']) && !empty($this->params->query['filter_type']) ) { $typeId = $this->params->query['filter_type']; $joins[] = array( 'alias' => 'IJ_OrganizationType', 'table' => 'organizations_types', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationType`.`organization_id`', '`IJ_OrganizationType`.`type_id` = ' . $typeId ), ); } // type filter $this->set('type_id', $typeId); // target $targetId = 0; if ( isset($this->params->query['target_id']) && !empty($this->params->query['target_id']) ) { $targetId = (int)$this->params->query['target_id']; $joins[] = array( 'alias' => 'IJ_OrganizationTarget', 'table' => 'organizations_targets', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationTarget`.`organization_id`', '`IJ_OrganizationTarget`.`target_id` = ' . $targetId ), ); } $this->set('target_id', $targetId); // ประเภทสิทธิหลัก (topic) $topicId = 0; $subtopicId = 0; $subtopics = array(); if ( isset($this->params->query['topic_id']) && !empty($this->params->query['topic_id']) ) { $topicId = (int)$this->params->query['topic_id']; $subtopics = $this->Organization->Subtopic->find('list', array( 'conditions' => array('topic_id' => $topicId) )); if ( isset($this->params->query['subtopic_id']) && !empty($this->params->query['subtopic_id']) ) { // selected a subtopic $subtopicId = (int)$this->params->query['subtopic_id']; $joins[] = array( 'alias' => 'IJ_OrganizationTopic', 'table' => 'organizations_subtopics', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationTopic`.`organization_id`', '`IJ_OrganizationTopic`.`subtopic_id` = ' . $subtopicId ) ); $this->request->data['Organization']['subtopic_id'] = $subtopicId; } else { // all subtopics in selected topic_id $joins[] = array( 'alias' => 'IJ_OrganizationSubtopic', 'table' => 'organizations_subtopics', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationSubtopic`.`organization_id` = `Organization`.`id`' ) ); $joins[] = array( 'alias' => 'IJ_SubtopicTopic', 'table' => 'subtopics', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationSubtopic`.`subtopic_id` = `IJ_SubtopicTopic`.`id`' ) ); $joins[] = array( 'alias' => 'TopicsSubtopics', 'table' => 'topics', 'type' => 'INNER', 'conditions' => array( '`IJ_SubtopicTopic`.`topic_id` = `TopicsSubtopics`.`id`', '`TopicsSubtopics`.`id` = ' . $topicId ) ); } } $this->set('topic_id', $topicId); $this->set('subtopic_id', $subtopicId); $this->set('subtopics', $subtopics); // search from address, defaults to person's actual address $addressType = 'address'; if ( isset($this->params->query['address_type']) && !empty($this->params->query['address_type']) ) { $addressType = $this->params->query['address_type']; } $this->set('address_type', $addressType); // region, province, district $regionId = 0; $provinceId = 0; $districtId = 0; $provinces = array(); $districts = array(); if ( isset($this->params->query['region_id']) && !empty($this->params->query['region_id']) ) { $regionId = (int)$this->params->query['region_id']; // set list values for province_id field in the view $this->loadModel('Province'); $provinces = $this->Province->find('list', array( 'conditions' => array('region_id' => $regionId) )); // select region and province if ( isset($this->params->query['province_id']) && !empty($this->params->query['province_id']) ) { $provinceId = (int)$this->params->query['province_id']; $districts = $this->Organization->District->find('list', array( 'conditions' => array('District.province_id' => $provinceId) )); // specific district if ( isset($this->params->query['district_id']) && !empty($this->params->query['district_id']) ) { $districtId = (int)$this->params->query['district_id']; if ($addressType == 'address') { $conditions['AND']['Organization.district_id'] = $districtId; } else { // all organizations that have $districtId in districts_organizations table $joins[] = array( 'alias' => 'IJ_OrganizationsDistrict', 'table' => 'districts_organizations', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationsDistrict`.`organization_id` = `Organization`.`id`', '`IJ_OrganizationsDistrict`.`district_id` = ' . $districtId ) ); } } else { // all districts in selected province if ( $addressType == 'address' ) { $joins[] = array( 'alias' => 'IJ_OrganizationDistrict', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationDistrict`.`id` = `Organization`.`district_id`', '`IJ_OrganizationDistrict`.`province_id` = ' . $provinceId ) ); } else { $joins[] = array( 'alias' => 'IJ_OrganizationsDistrict', 'table' => 'districts_organizations', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationsDistrict`.`organization_id` = `Organization`.`id`' ) ); $joins[] = array( 'alias' => 'IJ_District', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationsDistrict`.`district_id` = `IJ_District`.`id`', '`IJ_District`.`province_id` = ' . $provinceId ) ); } } } else { // select only region, search in all districts in all provinces in selected region // all districts in selected province if ( $addressType == 'address' ) { $joins[] = array( 'alias' => 'IJ_OrganizationDistrict', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationDistrict`.`id` = `Organization`.`district_id`' ) ); $joins[] = array( 'alias' => 'IJ_DistrictProvince', 'table' => 'provinces', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationDistrict`.`province_id` = `IJ_DistrictProvince`.`id`', '`IJ_DistrictProvince`.`region_id` = ' . $regionId ) ); } else { // search in work area $joins[] = array( 'alias' => 'IJ_OrganizationsDistrict', 'table' => 'districts_organizations', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationsDistrict`.`organization_id` = `Organization`.`id`' ) ); $joins[] = array( 'alias' => 'IJ_District', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationsDistrict`.`district_id` = `IJ_District`.`id`' ) ); $joins[] = array( 'alias' => 'IJ_Province', 'table' => 'provinces', 'type' => 'INNER', 'conditions' => array( '`IJ_District`.`province_id` = `IJ_Province`.`id`', '`IJ_Province`.`region_id` = ' . $regionId ) ); } } } $this->set('region_id', $regionId); $this->set('province_id', $provinceId); $this->set('district_id', $districtId); $this->set('provinces', $provinces); $this->set('districts', $districts); $joined = ''; $joinedTo = ''; if ( isset($this->params->query['joined']) && !empty($this->params->query['joined'])) { $joined = $this->params->query['joined']; $dateConditions = array( 'Organization.joined >= ?' => array($joined) ); if ( isset($this->params->query['joined_to']) && !empty($this->params->query['joined_to']) ) { $joinedTo = $this->params->query['joined_to']; $dateConditions = array( 'Organization.joined BETWEEN ? AND ?' => array( $joined, $joinedTo ) ); } $conditions['OR'] = $dateConditions; } $this->set('joined', $joined); $this->set('joined_to', $joinedTo); } /** * admin_index method * * @return void */ public function admin_index() { $conditions = array(); $joins = array(); $this->__getCommonConditions($conditions, $joins); //Set status for query if ( !isset($this->params->query['status']) ) { $this->params->query['status'] = 1; } if ( !isset($this->params->query['recycle']) ) { $this->params->query['recycle'] = 0; } $conditions['Organization.is_activated'] = array($this->params->query['status']); $conditions['Organization.recycle'] = array($this->params->query['recycle']); $this->Paginator->settings['conditions'] = $conditions; $this->Paginator->settings['joins'] = $joins; $this->Paginator->settings['group'] = 'Organization.id'; $this->Paginator->settings['contain'] = array('Contact', 'Type', 'Activity'); $this->Organization->recursive = 0; $this->set('organizations', $this->Paginator->paginate()); $this->_setListValues(array('Type', 'Topic', 'Member', 'Target', 'Region')); $this->set('topicTitle', __('เครือข่ายองค์กร')); } /** * admin_view method * * @throws NotFoundException * @param string $id * @return void */ public function admin_view($id = null) { if (!$this->Organization->exists($id)) { throw new NotFoundException(__('ไม่พบข้อมูลเครือข่ายองค์กร')); } $options = array('conditions' => array('Organization.' . $this->Organization->primaryKey => $id)); $this->set('organization', $this->Organization->find('first', $options)); } /** * Add `organization` detail * - User can add contact in this form * - If user need to create `mou` we will create new record in `mou` table. * - Update `mou_id` if user created mou record. * * @author Ting <3musketeersteam@gmail.com> * @since 3 October 2013 * @modify 7 October 2013 : Create `mou` and `contact` after save data. * @modify 2014-03-06 - Mike - Added Nation Wide option */ public function admin_add() { if ( $this->request->is('post') ) { $this->Organization->create(); // if nation wide option is selected, we have to add all the districts of the database to the array if( $this->request->data['Organization']['is_nationwide'] ) { unset( $this->request->data['DistrictM2M']['DistrictM2M'] ); $this->loadModel( 'District' ); $districts = $this->District->find( 'all', array( 'recursive' => -1, 'fields' => array( 'id' ) ) ); // convert to array with index starting at 0, 1, 2, ... $this->request->data['DistrictM2M']['DistrictM2M'] = Set::extract( '/District/id', $districts ); } else if( !empty( $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] ) ) { // At least one province has been selected as `province wide` $this->loadModel( 'District' ); $districts = $this->District->find( 'all', array( 'recursive' => -1, 'fields' => array( 'id' ), 'conditions' => array( 'District.province_id' => $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'], ) ) ); $districts = Set::extract( '/District/id', $districts ); // convert to array with index starting at 0, 1, 2, ... $this->request->data['DistrictM2M']['DistrictM2M'] = array_merge( (array) $this->request->data['DistrictM2M']['DistrictM2M'], $districts ); $provinceswide = $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide']; // we don't want cakephp to insert some wrong data in the table, we do it ourself unset( $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] ); } // take care of the people_positions entries if( !empty( $this->request->data['OrgMembership']['OrgMembership'] ) ) { $orgMemberships = $this->request->data['OrgMembership']; // we don't want cakephp to insert some wrong data in the table, we do it ourself unset( $this->request->data['OrgMembership'] ); } $this->request->data['Organization']['createdby']=$this->Session->read('Auth.User.username'); if ( !$this->Organization->save($this->request->data) ) { $this->Session->setFlash(__('ไม่สามารถบันทึกข้อมูลองค์กรได้ กรุณาลองใหม่อีกครั้ง'), 'flash-fail'); return; } //Find last id of `organization` that we just insert. $orgId = $this->Organization->getLastInsertID(); if( isset( $provinceswide ) && !empty( $provinceswide ) ) { // insert all the provinces in the table `organizations_provinces_wide` foreach( ( array )$provinceswide as $province ) { $this->Organization->OrganizationProvinceWide->create(); $this->Organization->OrganizationProvinceWide->save( array( 'organization_id' => $orgId, 'province_id' => $province, ) ); } } if( isset( $orgMemberships ) && !empty( $orgMemberships ) ) { $nOrgMemberships = count( $orgMemberships['OrgMembership'] ); for( $i = 0; $i < $nOrgMemberships; $i++ ) { $this->Organization->OrgMembership->create(); $this->Organization->OrgMembership->save( array( 'organization_id' => $orgId, 'member_id' => $orgMemberships['OrgMembership'][$i], 'start' =>$orgMemberships['OrgMembershipStart'][$i], 'end' =>$orgMemberships['OrgMembershipEnd'][$i], ) ); } } //Save `organization_id` to Contact $this->Organization->Contact->updateAll( array('Contact.organization_id' => $orgId), array('Contact.id' => $this->request->data['Organization']['Contact']) ); try { $this->__handleUploadPhoto( $orgId ); } catch ( Exception $e ) { $this->Session->setFlash( $e->getMessage(), 'flash-fail' ); return; } $this->Session->setFlash(__('บันทึกข้อมูลองค์กรสำเร็จ'), 'flash-success'); $this->redirect(array('action' => 'admin_index', 'admin' => true)); } //Find another relation for add organization. $models = array( 'Type', 'Offer', 'Action', 'Role' ); foreach( $models as $model ) { $this->set( strtolower( Inflector::pluralize($model) ) , $this->Organization->{$model}->find('list', array('conditions' => array( $model . '.is_activated' => 1 )))); } $this->loadModel('Topic'); $this->set('topics', $this->Topic->find('list')); $this->set('targetList', $this->Organization->Target->find('list')); $this->loadModel('Member'); $this->set('members', $this->Member->find('list')); $this->set('districts', array()); $this->set('provinces', $this->Organization->District->Province->find('list')); $this->set('contacts_list', $this->Organization->Contact->find('list', array('conditions' => array('Contact.organization_id' => 0)))); $this->set('topicTitle', __('เพิ่มข้อมูลองค์กร')); } /** * Handle the CSV file upload to create organizations in batch mode * @author Mike <mike@damoiseau.me> * @since 2014-03-09 */ private function __handlePostAdminBatchAdd() { $now = time(); $url = TMP.$now.'.csv'; $file = $this->request->data['csv']; try { if( !is_uploaded_file( $file['tmp_name'] ) ){ throw new Exception( 'The system cannot find the file.' ); } // We copy the file in the TMP folder of CakePHP if ( !move_uploaded_file( $file['tmp_name'], $url ) ) { throw new Exception( 'The system cannot access the file.' ); } $csvData = array(); // open file for reading if( ( $handle = fopen( $url, "r" ) ) === FALSE ) { throw new Exception( 'The system cannot read the file.' ); } while( ( $data = fgetcsv( $handle, 4096, "," ) ) !== FALSE ) { if( empty( $data ) || !is_array( $data ) || ( count( $data ) != 7 ) ) { continue; } $csvData[] = $data; } fclose( $handle ); // Remove the first line (title of column) array_shift( $csvData ); $cpt = 2; // we have removed the first line of the CSV, but we should not forget it when displaying the error message foreach ( $csvData as $data ) { $org = array( 'name' => $data[2], 'role_id' => $data[1], 'district_id' => $data[6] ); $this->Organization->create(); if( !$this->Organization->save( $org ) ) { throw new Exception( sprintf( '%s "%s" on line %d', __( 'ไม่สามารถบันทึกข้อมูลองค์กร ' ), $data[2], $cpt ) ); } $orgID = $this->Organization->getLastInsertID(); $districts = explode( ',', $data[3] ); foreach( $districts as $district ) { $this->Organization->DistrictsOrganization->create(); $this->Organization->DistrictsOrganization->save( array( 'organization_id' => $orgID, 'district_id' => $district, ) ); } $subtopics = explode( ',', $data[4] ); foreach( $subtopics as $subtopic ) { $this->Organization->OrganizationsSubtopic->create(); $this->Organization->OrganizationsSubtopic->save( array( 'organization_id' => $orgID, 'subtopic_id' => $subtopic, ) ); } $this->Organization->OrganizationsType->create(); $this->Organization->OrganizationsType->save( array( 'organization_id' => $orgID, 'type_id' => $data[5], ) ); $cpt++; } $this->Session->setFlash( __( 'นำเข้าข้อมูลองค์กรเรียบร้อยแล้ว.' ), 'flash-success' ); } catch( Exception $e ) { $this->Session->setFlash( sprintf( '%s: %s.', __( 'ไม่สามารถนำเข้าข้อมูลองค์กรค์ได้' ), $e->getMessage() ), 'flash-fail' ); } @unlink( $url ); } /** * Display a file input field to import a CSV file used for the creation of organizations in batch mode * @author Mike <mike@damoiseau.me> * @since 2014-03-09 */ public function admin_batch_add() { if ( $this->request->is('post') ) { $this->__handlePostAdminBatchAdd(); } $this->set( 'topicTitle', __( 'Organizations batch import' ) ); } /** * Edit organization detail * * @throws NotFoundException if we not found any data * @param string $id id of organization * @author Ting <3musketeersteam@gmail.com> * @since 17 October 2013 * @modify 2014-03-06 - Mike - Added Nation Wide option */ public function admin_edit( $id = null ) { //If not found any data. if ( !$this->Organization->exists($id) ) { throw new NotFoundException(__('ไม่พบข้อมูลเครือข่ายองค์กร')); } if ( $this->request->is('post') || $this->request->is('put') ) { $this->Organization->id = $id; // delete previous entries in table `organizations_provinces_wide` $this->Organization->OrganizationProvinceWide->deleteAll( array( 'OrganizationProvinceWide.organization_id' => $id, ) ); // if nation wide option is selected, we have to add all the districts of the database to the array if( $this->request->data['Organization']['is_nationwide'] ) { unset( $this->request->data['DistrictM2M']['DistrictM2M'] ); $this->loadModel( 'District' ); $districts = $this->District->find( 'all', array( 'recursive' => -1, 'fields' => array( 'id' ) ) ); // convert to array with index starting at 0, 1, 2, ... $this->request->data['DistrictM2M']['DistrictM2M'] = Set::extract( '/District/id', $districts ); } else if( !empty( $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] ) ) { // At least one province has been selected as `province wide` // debug( $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] ); return; $this->loadModel( 'District' ); $districts = $this->District->find( 'all', array( 'recursive' => -1, 'fields' => array( 'id' ), 'conditions' => array( 'District.province_id' => $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'], ) ) ); $districts = Set::extract( '/District/id', $districts ); // convert to array with index starting at 0, 1, 2, ... $this->request->data['DistrictM2M']['DistrictM2M'] = array_merge( (array) $this->request->data['DistrictM2M']['DistrictM2M'], $districts ); // insert all the provinces in the table `organizations_provinces_wide` foreach( ( array )$this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] as $province ) { $this->Organization->OrganizationProvinceWide->create(); $this->Organization->OrganizationProvinceWide->save( array( 'organization_id' => $id, 'province_id' => $province, ) ); } // we don't want cakephp to insert some wrong data in the table, we do it ourself unset( $this->request->data['OrganizationProvinceWide']['OrganizationProvinceWide'] ); } // OrgMembership // take care of the people_positions entries $this->Organization->OrgMembership->deleteAll( array( 'OrgMembership.organization_id' => $id ) ); if( !empty( $this->request->data['OrgMembership']['OrgMembership'] ) ) { $nOrgMemberships = count ( $this->request->data['OrgMembership']['OrgMembership'] ); for( $i = 0; $i < $nOrgMemberships; $i++ ) { if( isset( $this->request->data['OrgMembership']['OrgMembership'][$i] ) ) { $this->Organization->OrgMembership->create(); $this->Organization->OrgMembership->save( array( 'organization_id' => $id, 'member_id' => $this->request->data['OrgMembership']['OrgMembership'][$i], 'start' =>$this->request->data['OrgMembership']['OrgMembershipStart'][$i], 'end' =>$this->request->data['OrgMembership']['OrgMembershipEnd'][$i], ) ); } } // we don't want cakephp to insert some wrong data in the table, we do it ourself unset( $this->request->data['OrgMembership'] ); } $this->request->data['Organization']['modifiedby']=$this->Session->read('Auth.User.username'); if ( !$this->Organization->save($this->request->data) ) { $this->Session->setFlash(__('ไม่สามารถบันทึกข้อมูลองค์กรได้ กรุณาลองใหม่อีกครั้ง'), 'flash-fail'); $this->redirect(array('action' => 'index')); } //Save `organization_id` to Contact $this->Organization->Contact->updateAll( array('Contact.organization_id' => $id), array('Contact.id' => $this->request->data['Organization']['Contact']) ); //If user need to add `mou` for organization. if ( isset($this->request->data['Organization']['ismou']) && $this->request->data['Organization']['ismou'] == 1 ) { //Save `mou` with `mou_start` and `mou_end` $this->Organization->OrgMembership->create(); $this->Organization->OrgMembership->save(array( 'organization_id' => $id, 'member_id' => $this->request->data['Organization']['member_id'], 'start' => $this->request->data['Organization']['mou_start'], 'end' => $this->request->data['Organization']['mou_end'] )); } try { $this->__handleUploadPhoto( $id ); } catch ( Exception $e ) { // @todo - update this message to thai $this->Session->setFlash( __( 'The organization has been saved but there was an error with the photo: ' ) . $e->getMessage(), 'flash-fail' ); $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__('บันทึกข้อมูลองค์กรสำเร็จ'), 'flash-success'); $this->redirect(array('action' => 'index')); } //Get organization to edit. $conditions = array( 'conditions' => array( 'Organization.' . $this->Organization->primaryKey => $id )); $this->request->data = $this->Organization->find('first', $conditions); //Find another relation for add organization. $models = array( 'Type', 'Offer', 'Action','Role' ); foreach( $models as $model ) { $this->set( strtolower( Inflector::pluralize($model) ) , $this->Organization->{$model}->find('list', array('conditions' => array( $model . '.is_activated' => 1 )))); } //Condition to select contact for this organization. // $contactConditions = array('conditions' => array( 'Contact.organization_id IN' => array(0, $this->request->data['Organization']['id']))); $this->loadModel('Topic'); $this->set('topics', $this->Topic->find('list')); $this->set('targetList', $this->Organization->Target->find('list')); $this->loadModel('Member'); $this->set('members', $this->Member->find('list')); $provinces = $this->Organization->District->Province->find('list'); $this->set( 'provinces', $provinces ); // $this->set('contacts', $this->Organization->Contact->find('list', $contactConditions)); $this->set('contacts_list', $this->Organization->Contact->find('list', array('conditions' => array('Contact.organization_id' => 0)))); //Find province by district //@TODO : Should we creat new function for this i think we will use many times. $this->Organization->District->recursive = 0; $provinceExists = $this->Organization->District->findById($this->request->data['Organization']['district_id']); $this->set('province_exists', $provinceExists); //Get all district depending on province's id. if ( isset($provinceExists['Province']['name'])) { $districts = $this->Organization->District->find('list', array('conditions' => array('District.province_id' => $provinceExists['Province']['id']))); } else { $districts = array(); } $provinceswide = $this->Organization->OrganizationProvinceWide->find( 'list', array( 'fields' => array( 'OrganizationProvinceWide.province_id', 'OrganizationProvinceWide.organization_id' ), 'conditions' => array( 'OrganizationProvinceWide.organization_id' => $id, ), 'order' => array( 'OrganizationProvinceWide.province_id' ), ) ); foreach( $provinceswide as $kProvWide => $vProvWide ) { $provinceswide[$kProvWide] = $provinces[$kProvWide]; } //Try to make data for mutil checkbox $models = array( 'districtm2ms' => 'DistrictM2M', 'subtopics' => 'Subtopic', 'targets' => 'Target', 'contacts' => 'Contact', ); foreach( $models as $kModel => $vModel ) { $$kModel = array(); foreach( $this->request->data[$vModel] as $vData ) { if( ( $kModel != 'districtm2ms' ) || ( !isset( $provinceswide[$vData['province_id']] ) ) ) { ${$kModel}[$vData['id']] = $vData['name']; } } } $this->set(compact('subtopics', 'districtm2ms', 'districts', 'provinceswide', 'targets', 'contacts')); // build the url for the photos $photosUrl = 'organizations/' . $id; $this->set( 'photos_url', $photosUrl ); $this->Organization->OrgMembership->recursive = 1; $orgMember = $this->Organization->OrgMembership->findAllByOrganizationId($id); $this->set('org_member', $orgMember); $this->set('topicTitle', __('แก้ไขข้อมูล: ') . $this->request->data['Organization']['name']); } /** * admin_delete method * * @throws NotFoundException * @param string $id * @return void */ public function admin_delete($id = null) { $this->Organization->id = $id; if (!$this->Organization->exists()) { throw new NotFoundException(__('Invalid organization')); } $this->request->onlyAllow('get', 'delete', 'post'); if ($this->Organization->delete()) { $this->Session->setFlash(__('ปิดการใช้งานเครืองข่ายองค์กรณ์เรียบร้อยแล้ว'), 'flash-success'); } else { $this->Session->setFlash(__('ไม่สามารปิดการใช้งานเครืองข่ายองค์กร'), 'flash-fail'); } return $this->redirect(array('action' => 'index')); } /** * acvtivate itams from inactivated. * * @param int $id organization's id * @author Ting <3Musketeersteam@gmail.com> * @since 27 January 2014 */ public function admin_active($id = null,$type=null) { if($type=='recycle'){ $this->Organization->id = $id; $data = array( 'id' => $id, 'recycle' => 1 ); $this->Organization->save($data); return $this->redirect(array('action' => 'index')); }else{ $this->Organization->id = $id; if (!$this->Organization->exists()) { throw new NotFoundException(__('Invalid organization')); } $this->request->onlyAllow('get', 'delete', 'post'); if ($this->Organization->activate()) { $this->Session->setFlash(__('เปิดใ้ช้งานองค์กรเรียบร้อยแล้ว'), 'flash-success'); } else { $this->Session->setFlash(__('ไม่สามารถเปิดการใช้งานองค์กร'), 'flash-fail'); } return $this->redirect(array('action' => 'index')); } } /** * Remove MOU from Organization table. * > Remove MOU data from MOU table. * > Take old MOU data to Mou Histoy table. * * @param int $id organization id * @return JSON * @author Ting <3musketeersteam@gmail.com> * @since 17 October 2013 */ public function admin_mouDelete( $id = null, $memberId = null ) { $this->autoRender = false; if ( $id == 0 && $memberId == 0 ) { return json_encode(false); } //Delete old MOU data $orgMemberId = $this->Organization->OrgMembership->findByMemberIdAndOrganizationId($memberId, $id, array('fileds' => 'id')); $this->Organization->OrgMembership->id = $orgMemberId['OrgMembership']['id']; if ( !$this->Organization->OrgMembership->delete() ) { return json_encode(false); } return json_encode(true); } private function __handleUploadPhoto( $id ) { $destinationFolder = WWW_ROOT . 'img' . DS . 'organizations' . DS . $id . DS; // delete photo? if( isset( $this->request->data['Organization']['delete_photo'] ) ) { foreach( $this->request->data['Organization']['delete_photo'] as $delPhoto ) { $photoName = $this->Organization->Photo->find( 'first', array( 'conditions' => array( 'id' => $delPhoto, 'model' => 'organization', ), 'fields' => array( 'id', 'name' ), ) ); // delete file @unlink( $destinationFolder . $photoName['Photo']['name'] ); // delete record from db $this->Organization->Photo->delete( $photoName['Photo']['id'] ); } } // is there a new photo to upload? if( $this->request->data['Organization']['photo']['error'] == UPLOAD_ERR_OK ) { $filePhoto = $this->request->data['Organization']['photo']; $type = @getimagesize( $filePhoto['tmp_name'] ); $newName = $filePhoto['name']; if( ( $type == false ) || !in_array( $type[2], Configure::read( 'image_types_allowed' ) ) ) { throw new Exception( __( 'the type of the image is not allowed.' ) ); } // does the destination folder exist? if( !file_exists( $destinationFolder ) && !is_dir( $destinationFolder ) ) { mkdir( $destinationFolder, 0755, true ); } if( !is_uploaded_file( $filePhoto['tmp_name'] ) || !move_uploaded_file( $filePhoto['tmp_name'], $destinationFolder . $newName ) ) { throw new Exception( __( 'the system cannot access your uploaded file.' ) ); } // Delete existing photo from db and File system $existingPhoto = $this->Organization->Photo->find( 'first', array( 'conditions' => array( 'model' => 'organization', 'fk_id' => $id, ), 'recursive' => -1 ) ); if( !empty( $existingPhoto ) ) { // delete old photo if different name (otherwise it should have been overwritten by `move_uploaded` function) if( $newName != $existingPhoto['Photo']['name'] ) { @unlink( $destinationFolder . $existingPhoto['Photo']['name'] ); } $this->Organization->Photo->delete( $existingPhoto['Photo']['id'] ); } $this->Organization->Photo->create(); $this->Organization->Photo->save( array( 'fk_id' => $id, 'model' => 'organization', 'name' => $newName, ) ); } // if photo uploaded } // __handleUploadPhoto private function __getCommonFilterConditions(&$conditions, &$joins) { // @todo - duplicated code with admin_index ? if ( isset($this->params->query['keyword']) && !empty($this->params->query['keyword']) ) { $keyword = $this->params->query['keyword']; $keywordValue = sprintf('%%%s%%', $keyword); $searchInFields = array( 'name', 'eng_name', 'short_name', 'eng_short_name', 'address1', 'address2', 'postal_code', 'telephone1', 'telephone2', 'telephone3', 'fax', 'email', 'url', 'work_level','mou_name', 'objective', 'publish'); $keywordCondition = array(); foreach ( $searchInFields as $field ) { $keywordCondition['Organization.' . $field . ' LIKE'] = $keywordValue; } $conditions['OR'] = $keywordCondition; $this->request->data['Organization']['keyword'] = $keyword; } // รหัสเครือข่าย if ( isset($this->params->query['id']) && !empty($this->params->query['id']) ) { $id = (int)$this->params->query['id']; $conditions['AND']['Organization.id'] = $id; $this->request->data['Organization']['id'] = $id; } // ประเภทเครือข่าย if ( isset($this->params->query['type_id']) && !empty($this->params->query['type_id']) ) { $typeId = (int)$this->params->query['type_id']; $joins[] = array( 'alias' => 'IJ_OrganizationType', 'table' => 'organizations_types', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationType`.`organization_id`', '`IJ_OrganizationType`.`type_id` = ' . $typeId ), ); $this->request->data['Organization']['type_id'] = $typeId; } // ประเภทสิทธิหลัก $subtopics = array(); if ( isset($this->params->query['topic_id']) && !empty($this->params->query['topic_id']) ) { $topicId = (int)$this->params->query['topic_id']; $subtopics = $this->Organization->Subtopic->find('list', array( 'conditions' => array('topic_id' => $topicId) )); $this->request->data['Organization']['topic_id'] = $topicId; if ( isset($this->params->query['subtopic_id']) && !empty($this->params->query['subtopic_id']) ) { // selected a subtopic $subtopicId = (int)$this->params->query['subtopic_id']; $joins[] = array( 'alias' => 'IJ_OrganizationTopic', 'table' => 'organizations_subtopics', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationTopic`.`organization_id`', '`IJ_OrganizationTopic`.`subtopic_id` = ' . $subtopicId ) ); $this->request->data['Organization']['subtopic_id'] = $subtopicId; } else { // all subtopics in selected topic_id $joins[] = array( 'alias' => 'IJ_OrganizationSubtopic', 'table' => 'organizations_subtopics', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationSubtopic`.`organization_id` = `Organization`.`id`' ) ); $joins[] = array( 'alias' => 'IJ_SubtopicTopic', 'table' => 'subtopics', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationSubtopic`.`subtopic_id` = `IJ_SubtopicTopic`.`id`' ) ); $joins[] = array( 'alias' => 'TopicsSubtopics', 'table' => 'topics', 'type' => 'INNER', 'conditions' => array( '`IJ_SubtopicTopic`.`topic_id` = `TopicsSubtopics`.`id`', '`TopicsSubtopics`.`id` = ' . $topicId ) ); } } $this->set('subtopics', $subtopics); // เป้าหมาย if ( isset($this->params->query['target_id']) && !empty($this->params->query['target_id']) ) { $targetId = (int)$this->params->query['target_id']; $joins[] = array( 'alias' => 'IJ_OrganizationTarget', 'table' => 'organizations_targets', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_OrganizationTarget`.`organization_id`', '`IJ_OrganizationTarget`.`target_id` = ' . $targetId ), ); $this->request->data['Organization']['target_id'] = $targetId; } // จังหวัดที่ตั้ง $locationDistricts = array(); if ( isset($this->params->query['province_id']) && !empty($this->params->query['province_id']) ) { $provinceId = (int)$this->params->query['province_id']; $locationDistricts = $this->Organization->District->find('list', array( 'conditions' => array('District.province_id' => $provinceId) )); $this->request->data['Organization']['province_id'] = $provinceId; // selected a district if ( isset($this->params->query['district_id']) && !empty($this->params->query['district_id']) ) { $districtId = (int)$this->params->query['district_id']; $conditions['AND']['Organization.district_id'] = $districtId; $this->request->data['Organization']['district_id'] = $districtId; } else { // all districts in selected province $joins[] = array( 'alias' => 'IJ_OrganizationDistrict', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationDistrict`.`id` = `Organization`.`district_id`' ) ); $joins[] = array( 'alias' => 'IJ_DistrictProvince', 'table' => 'provinces', 'type' => 'INNER', 'conditions' => array( '`IJ_OrganizationDistrict`.`province_id` = `IJ_DistrictProvince`.`id`', '`IJ_DistrictProvince`.`id` = ' . $provinceId ) ); } } $this->set('location_districts', $locationDistricts); // พื้นที่การทำงาน $workDistricts = array(); if ( isset($this->params->query['work_province_id']) && !empty($this->params->query['work_province_id']) ) { $workProvinceId = (int)$this->params->query['work_province_id']; $workDistricts = $this->Organization->District->find('list', array( 'conditions' => array('District.province_id' => $workProvinceId) )); $this->request->data['Organization']['work_province_id'] = $workProvinceId; // selected a district if ( isset($this->params->query['work_district_id']) && !empty($this->params->query['work_district_id']) ) { $workDistrictId = (int)$this->params->query['work_district_id']; $this->request->data['Organization']['work_district_id'] = $workDistrictId; $joins[] = array( 'alias' => 'IJ_DistrictOrganization', 'table' => 'districts_organizations', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_DistrictOrganization`.`organization_id`', '`IJ_DistrictOrganization`.`district_id` = ' . $workDistrictId ) ); } else { // all districts in selected work_province $joins[] = array( 'alias' => 'IJ_DistrictOrganization', 'table' => 'districts_organizations', 'type' => 'INNER', 'conditions' => array( '`Organization`.`id` = `IJ_DistrictOrganization`.`organization_id`' ) ); $joins[] = array( 'alias' => 'IJ_DistrictWorkProvince', 'table' => 'districts', 'type' => 'INNER', 'conditions' => array( '`IJ_DistrictWorkProvince`.`id` = `IJ_DistrictOrganization`.`district_id`' ) ); $joins[] = array( 'alias' => 'IJ_ProvinceWorkDistrict', 'table' => 'provinces', 'type' => 'INNER', 'conditions' => array( '`IJ_ProvinceWorkDistrict`.`id` = `IJ_DistrictWorkProvince`.`province_id`', '`IJ_ProvinceWorkDistrict`.`id` = ' . $workProvinceId ) ); } } $this->set('work_districts', $workDistricts); } // `เครือข่ายองค์กร` page public function index() { // only org without mou // $conditions['AND']['Organization.member_id'] = 0;; $joins = array(); $conditions = array(); $this->__getCommonConditions($conditions, $joins); $conditions['Organization.is_activated'] = array(1); $this->Paginator->settings['conditions'] = $conditions; $this->Paginator->settings['joins'] = $joins; $this->Paginator->settings['group'] = 'Organization.id'; $this->Paginator->settings['contain'] = array('Contact', 'Type', 'Activity'); $this->Organization->recursive = 0; $this->set('organizations', $this->Paginator->paginate()); $this->_setListValues(array('Type', 'Topic', 'Member', 'Target', 'Region')); $this->set('topicTitle', __('เครือข่ายองค์กร')); } // `เครือข่ายองค์กรที่ทำ MOU` page public function index_mou() { // only org with mou $conditions['AND']['Organization.member_id !='] = 0; $joins = array(); $this->__getCommonFilterConditions($conditions, $joins); // this has the same filter conditions with index action // plus mou dates fields // start date if ( isset($this->params->query['start_date']) && !empty($this->params->query['start_date']) ) { $startDate = $this->params->query['start_date']; $joins[] = array( 'alias' => 'OrgMou', 'table' => 'mous', 'type' => 'INNER', 'conditions' => array( '`Organization`.`mou_id` = `OrgMou`.`id`', '`OrgMou`.`started` = "' . $startDate . '"' ) ); $this->request->data['Organization']['start_date'] = $startDate; } // end date if ( isset($this->params->query['end_date']) && !empty($this->params->query['end_date']) ) { $endDate = $this->params->query['end_date']; $joins[] = array( 'alias' => 'OrgMouEnd', 'table' => 'mous', 'type' => 'INNER', 'conditions' => array( '`Organization`.`mou_id` = `OrgMouEnd`.`id`', '`OrgMouEnd`.`ended` = "' . $endDate . '"' ) ); $this->request->data['Organization']['end_date'] = $endDate; } $this->Paginator->settings['conditions'] = $conditions; $this->Paginator->settings['joins'] = $joins; $this->set('organizations', $this->Paginator->paginate('Organization')); $this->_setListValues(array('Type', 'Province', 'Topic', 'Target')); $this->set('topicTitle', __('เครือข่ายองค์กรที่ทำ MOU')); } /** * View detail of organization * * @param integer $id Organization's id * @return [type] [description] */ public function view( $id = null ) { if ( !$this->Organization->exists($id) ) { throw new NotFoundException(__('ไม่พบข้อมูลเครือข่ายองค์กร')); } // build the url for the photos $photosUrl = 'organizations/' . $id; $this->set( 'photos_url', $photosUrl ); $this->loadModel('Province'); $provinces = $this->Province->find('list'); $this->set('provinces', $provinces); $this->loadModel( 'District' ); $districts = $this->District->find('list'); $this->set('districts', $districts); $this->loadModel('Member'); $members = $this->Member->find('list'); $this->set('members', $members); $organization = $this->Organization->findById($id); // for the case that province-wide work area is selected from a province(s) // we display provinces and also districts in work_area $workAreas = array(); if (!empty($organization['OrganizationProvinceWide'])) { foreach ($organization['OrganizationProvinceWide'] as $w) { // put the province_id as a key for later convinient comparison. // we don't really need array keys in the view $workAreas[$w['province_id']] = __('จังหวัด') . $provinces[$w['province_id']]; } $workProvinces = array_keys($workAreas); foreach ($organization['DistrictM2M'] as $d) { if (!in_array($d['province_id'], $workProvinces)) { $workAreas[] = $d['name']; } } } // put them in $organization so it is available in pdf view also. $organization['work_areas'] = $workAreas; $this->set('organization', $organization); $this->set('topicTitle', __('แสดงข้อมูลเครือข่ายองค์กร')); if ( isset($this->params->ext) && $this->params->ext == 'pdf') { $this->_renderPdf(array_merge($organization, array('Province' => $provinces)), '/Organizations/pdf/view'); } } public function admin_export() { $test = new TestController(); //http://localhost/nhrc/admin/organizations/export?type=csv&status=1&recycle=0 // $_SERVER(); print_r( $_SERVER);exit(); Configure::write('debug',0); // just in case $this->autoRender = false; // thanks, but no. $modelName = $this->modelClass; $conditions = array(); $joins = array(); $this->__getCommonConditions($conditions, $joins); // Find fields needed without recursing through associated models $data = $this->{$modelName}->find('all', array( 'conditions' => $conditions, 'joins' => $joins, 'order' => $modelName . '.id ASC', 'recursive' => 1 )); // Define column headers for CSV file, in same array format as the data itself $headers = array( $modelName => array( 'id' => __('รหัส'), 'type' => __('ประเภทองค์กร'), 'name' => __('ชื่อองค์กร'), 'eng_name' => __('ชื่อองค์กรภาษาอังกฤษ'), 'short_name' => __('ชื่อย่อองค์กร'), 'eng_short_name' => __('ชื่อย่อองค์กรภาษาอังกฤษ'), 'fax' => __('โทรสาร'), 'email' => __('อีเมล์องค์กร'), 'url' => __('เว็บไซต์'), 'telephone1' => __('หมายเลขโทรศัพท์ 1'), 'telephone2' => __('หมายเลขโทรศัพท์ 2'), 'founded' => __('วันที่ก่อตั้ง'), 'joined' => __('วันที่เริ่มเป็นสมาชิก'), 'address' => __('ที่อยู่'), 'province' => __('จังหวัด'), 'district' => __('อำเภอ'), 'postal_code' => __('รหัสไปรษณีย์'), 'work_level' => __('หมายเหตุ'), 'mou_name' => __('ข้อตกลงความร่วมมือ (MOU)'), 'objective' => __('ผลงานที่ผ่านมา'), 'publish' => __('ประวัติโดยย่อ'), // 'offer' => __('ข้อเสนอ'), // 'action' => __('Action'), 'target' => __('กลุ่มเป้าหมาย'), // 'role' => __('บทบาทและหน้าที่'), 'contact' => __('ผู้ประสานงาน'), 'subtopic' => __('ประเภทสิทธิ'), 'is_member' => __('เข้าร่วมโครงการพิเศษ'), 'member_name' => __('ชื่อโครงการ'), 'member_start' => __('วันที่เริ่มทำ'), 'member_end' => __('วันที่สิ้นสุด'), 'work_districts' => __('พื้นที่การทำงาน'), 'created' => __('วันที่เพิ่มข้อมูล'), 'modified' => __('แก้ไขล่าสุด') ) ); foreach ( $data as &$d ) { // many to many - join the names together // $offers = array(); // if ( isset($d['Offer']) ) { // foreach( $d['Offer'] as $offer ) { // $offers[] = $offer['name']; // } // } // $actions = array(); // if ( isset($d['Action']) ) { // foreach( $d['Action'] as $action ) { // $actions[] = $action['name']; // } // } $targets = array(); if ( isset($d['Target']) ) { foreach( $d['Target'] as $target ) { $targets[] = $target['name']; } } $subtopics = array(); if ( isset($d['Subtopic']) ) { foreach( $d['Subtopic'] as $subtopic ) { $subtopics[] = $subtopic['name']; } } $workDistricts = array(); if ($d['Organization']['is_nationwide']) { $workDistricts = __('ทั่วประเทศ'); } else if ( isset($d['DistrictM2M']) ) { foreach( $d['DistrictM2M'] as $work ) { $workDistricts[] = $work['name']; } $workDistricts = implode(',', $workDistricts); } $contacts = array(); if ( isset($d['Contact']) ) { foreach( $d['Contact'] as $contact ) { $contacts[] = $contact['name']; } } $membership = array( 'isMember' => false, 'name' => '', 'start' => '', 'end' => '' ); if (!empty($d['OrgMembership'])) { $membership['isMember'] = true; $this->loadModel('Member'); $this->Member->recursive = -1; $member = $this->Member->findById($d['OrgMembership'][0]['member_id']); $membership['name'] = $member['Member']['name']; $membership['start'] = $d['OrgMembership'][0]['start']; $membership['end'] = $d['OrgMembership'][0]['end']; } // i was trying to use JOIN but for some reason it didn't work // models added with JOIN simply didn't show up in Org object :( $province = $this->Organization->District->Province->find('first', array( 'conditions' => array( 'Province.id' => $d['District']['province_id'] ), 'recursive' => -1, 'fields' => array('name') )); $d = array( $modelName => array( 'id' => $d['Organization']['id'], 'type' => $d['Type'][0]['name'], 'name' => $d['Organization']['name'], 'eng_name' => $d['Organization']['name'], 'short_name' => $d['Organization']['short_name'], 'eng_short_name' => $d['Organization']['eng_short_name'], 'fax' => $d['Organization']['fax'], 'email' => $d['Organization']['email'], 'url' => $d['Organization']['url'], 'telephone1' => $d['Organization']['telephone1'], 'telephone2' => $d['Organization']['telephone2'], 'founded' => $d['Organization']['founded'], 'joined' => $d['Organization']['joined'], 'address' => $d['Organization']['address1'], 'province' => $province['Province']['name'], 'district' => $d['District']['name'], 'postal_code' => $d['Organization']['postal_code'], 'work_level' => $d['Organization']['work_level'], 'mou_name' => __('ข้อตกลงความร่วมมือ (MOU)'), 'objective' => $d['Organization']['objective'], 'publish' => $d['Organization']['publish'], // 'offer' => implode(',', $offers), // 'action' => implode(',', $actions), 'target' => implode(',', $targets), // 'role' => $d['Role']['name'], 'contact' => implode(',', $contacts), 'subtopic' => implode(',', $subtopics), 'is_member' => ($membership['isMember']) ? __('ใช่') : __('ไม่ใช่'), 'member_name' => $membership['name'], 'member_start' => $membership['start'], 'member_end' => $membership['end'], 'work_districts' => $workDistricts, 'created' => $d['Organization']['created'], 'modified' => $d['Organization']['modified'] ) ); } $this->set('model', $modelName); if ( $this->request->params['ext'] == 'csv' ) { echo "1"; // // Add headers to start of data array // array_unshift($data, $headers); // // Make the data available to the view (and the resulting CSV file) // $this->set(compact('data')); // $this->render('/Elements/csv/admin_export', false); } else { echo "2"; // $this->set('topicTitle', __('เครือข่ายองค์กร')); // $this->_renderPdf($data, 'admin_export'); } } } class TestController { public function admin_export() { echo "eee"; } }