db->where('id', $id); $this->db->where('published', 1); $this->db->where('deleted', 0); $query = $this->db->get($this->table_name); if ($query->num_rows() == 1) return $query->row(); return NULL; } public function get_org_address(){ $this->db->select('id, full_title, address, longitude, latitude'); $this->db->where('published', 1); $this->db->where('deleted', 0); $query = $this->db->get($this->table_name); return $query->result(); } public function get_sector($sector_id){ $this->db->where('id', $sector_id); $query = $this->db->get('sectors'); if ($query->num_rows() == 1) return $query->row(); return NULL; } public function get_sectors($order=''){ $sql = 'SELECT t1.* , (select count(distinct t2.org_id) from `org_okved` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1 and t2.code in (select t3.id from `okved` t3 where t3.sector_id = t1.id)) cc from `sectors` t1 where (select count(t2.org_id) from `org_okved` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1 and t2.code in (select t3.id from `okved` t3 where t3.sector_id = t1.id)) > 0 '; if($order!=''){ $sql .= ' order by ' . $order; } $query = $this->db->query($sql, FALSE); //echo $this->db->last_query(); return $query->result(); } public function get_sectors_all($order=''){ //$this->db->where('published', 1); //$this->db->where('deleted', 0); if($order!=''){ $this->db->order_by($order, 'ASC'); } $query = $this->db->get('sectors'); return $query->result(); } public function get_areas($sector_id='all'){ $sql='select distinct t1.area_id, t1.name from `areas` t1 inner join `_ved_raw` t3 on t1.area_id = t3.area_id inner join `org_okved` t4 on t4.org_id = t3.id inner join `okved` t5 on t5.id = t4.code where t3.Published=1 '; if($sector_id!='all'){ $sql .= " and t5.sector_id = '" . $sector_id . "' "; } $sql .= 'order by t1.name'; $query = $this->db->query($sql, FALSE); //echo $this->db->last_query(); return $query->result(); } public function get_okved($sector_id='all'){ if($sector_id!='all'){ $query = $this->db->query("select t1.* from `okved` t1 where t1.sector_id=" . $this->db->escape($sector_id) . " and t1.id in (select t2.code from `org_okved` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1) order by t1.title_ru", FALSE); }else{ $query = $this->db->query("select t1.* from `okved` t1 where t1.id in (select t2.code from `org_okved` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1) order by t1.title_ru", FALSE); } return $query->result(); } public function get_okved_all($sector_id='all'){ $sql = "select t1.* from `okved` t1 where t1.sector_id=" . $this->db->escape($sector_id) . " order by t1.title_ru"; $query = $this->db->query($sql, FALSE); return $query->result(); } public function get_okved_by_org($id){ $query = $this->db->query("select t1.* from `okved` t1 where t1.id in (select t2.code from `org_okved` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1 and t4.id={$id})", FALSE); return $query->result(); } public function get_country_by_org($id, $current=0){ $query = $this->db->query("select t1.*, (select t5.LON from `world_boundaries` as t5 where t5.ISO2 = t1.id) as lon, (select t7.LAT from `world_boundaries` as t7 where t7.ISO2 = t1.id) as lat from `countrys` t1 where t1.id in (select t2.country_id from `org_countrys` t2 inner join `_ved_raw` t4 on t2.org_id = t4.id where t4.Published=1 and t4.id={$id} and t2.current= $current)", FALSE); return $query->result(); } public function get_image($content_id){ $this->db->where('action_id', $content_id); $this->db->where('in_body', 0); $this->db->where('object_id', $this->object_id); $query = $this->db->get('images'); if ($query->num_rows() == 1) return $query->row(); return NULL; } public function get_props($id) { $this->db->from('org_prop'); $this->db->where('org_id', $id); $query = $this->db->get(); if ($query->num_rows() > 0) return $query->result(); return NULL; } public function get_orgs_list($num, $offset, $options){ $data = array(); $like=array(); $where=array(); //make search if(array_key_exists('s', $options)){ if(!is_numeric($options['s'])){ $like['news_title'] = $options['s']; $like['news_description'] = $options['s']; $like['news_body'] = $options['s']; $like['news_source'] = $options['s']; $like['news_source_url'] = $options['s']; }else{ $like['news_id'] = $options['s']; } } $this->db->distinct(); $this->db->select($this->table_name . '.*, ' . $this->table_name . '.id as org_id'); $this->db->from($this->table_name); /* if(array_key_exists('kind', $options)){ $this->db->join('news_ntypes', 'news_ntypes.news_id = news.news_id'); $this->db->where('news_ntypes.ntype_id', $options['kind']); } */ $this->db->join('org_okved', 'org_okved.org_id = id'); $this->db->join('okved', 'okved.id = org_okved.code'); if($options['sector_id']!='all'){ $this->db->where('okved.sector_id', $options['sector_id']); } if(isset($options['okveds'])){ $okved_where='('; foreach ($options['okveds'] as $okved) { if($okved_where!='('){ $okved_where .= ' OR okved.id = ' . $okved; } else{ $okved_where .= 'okved.id = ' . $okved; } } $okved_where .= ')'; $this->db->where($okved_where, NULL, FALSE); } if(isset($options['area_id'])){ $this->db->where('area_id', $options['area_id']); } $this->db->where('published', 1); if(count($like)>=1){ $this->db->where(get_str_by_arr($like, 'OR')); } if(count($where)>=1){ $this->db->where(get_str_by_arr($where, 'AND')); } $tempdb = clone $this->db; $data['count'] = $tempdb->count_all_results(); unset($tempdb); //$this->db->order_by('news_date', 'DESC'); $sql = $this->db->get('',$num, $offset); $data['rows'] = $sql->result(); //echo $this->db->last_query(); return $data; } public function convert_wkt_to_poly_arr($wkt_str) { $ret_arr = array(); $matches = array(); preg_match('/\)\s*,\s*\(/', $wkt_str, $matches); if(empty($matches)) { $polys = array(trim($wkt_str)); } else { $polys = explode($matches[0], trim($wkt_str)); } foreach($polys as $poly) { $ret_arr = str_replace(' ', ',', str_replace(',','],[', str_replace('(','',str_replace(')','',substr($poly, stripos($poly,'(')+2, stripos($poly,')')-2))))); } return '[' . $ret_arr . ']'; } public function get_regions2($id='', $current=1){ if($id==''){ $this->db->select('t1.region_id, t1.region_title, 0 as cc', false); }else{ $this->db->select('t1.region_id, t1.region_title, (select count(t2.org_id) from org_regions as t2 WHERE t2.region_id=t1.region_id AND t2.org_id=' . $id . ' AND t2.current=' . $current . ') as cc', false); } $this->db->from('regions as t1'); $this->db->where('t1.hide_from_org',0); $ignore = array(701, 702, 703, 704, 705, 706, 707, 708, 94, 99, 80, 84, 85, 88); $this->db->where_not_in('region_id', $ignore); $this->db->order_by('t1.region_title'); $sql = $this->db->get(); $data = $sql->result(); //echo $this->db->last_query(); return $data; } public function get_countrys2($id='', $current=1){ if($id==''){ $this->db->select('t1.id, t1.title, 0 as cc', false); }else{ $this->db->select('t1.id, t1.title, (select count(t2.org_id) from org_countrys as t2 WHERE t2.country_id=t1.id AND t2.org_id=' . $id . ' AND t2.current=' . $current . ') as cc', false); } $this->db->from('countrys as t1'); $this->db->where('t1.hide',0); $this->db->order_by('t1.title'); $sql = $this->db->get(); $data = $sql->result(); //echo $this->db->last_query(); return $data; } public function get_okved2($id=''){ if($id==''){ $this->db->select('t1.id, t1.title_ru, t1.code, 0 as cc', false); }else{ $this->db->select('t1.id, t1.title_ru, t1.code, (select count(t2.org_id) from org_okved as t2 WHERE t2.code=t1.id AND t2.org_id=' . $id . ') as cc', false); } $this->db->from('okved as t1'); $this->db->order_by('t1.title_ru'); $sql = $this->db->get(); $data = $sql->result(); //echo $this->db->last_query(); return $data; } public function save($input){ $data=array(); $data['full_title'] = $input['full_title']; $data['B'] = $input['full_title']; $data['phone'] = $input['phone']; $data['email'] = $input['email']; $data['address'] = $input['address']; $data['A'] = $input['address']; $data['ur_address'] = $input['ur_address']; $data['inn'] = $input['inn']; $data['ogrn'] = $input['ogrn']; $data['N'] = $input['org_peoples']; $data['j'] = $input['j']; $data['AB'] = $input['AB']; $data['surname'] = $input['org_surname']; $data['name'] = $input['org_name']; $data['patronymic'] = $input['org_patronymic']; $data['U'] = $input['U']; $data['H'] = $input['site']; if(strpos($input['site'], 'http')===FALSE){ $data['H'] = 'http://' . $input['site']; } if(isset($input['registration_date'])){ $tmp_date = date_create_from_format('m/Y', $input['registration_date']); if($tmp_date){ $ss = (float)$tmp_date->getTimestamp(); $ss = $ss*1000; $data['registration_date'] = $ss; } } if ($this->db->insert($this->table_name, $data)) { $id = $this->db->insert_id(); } //regions $this->db->where('org_id', $id); $this->db->delete('org_regions'); if(isset($input['regions_current'])){ foreach ($input['regions_current'] as $key => $value){ $this->db->insert('org_regions', array('org_id'=>$id,'region_id'=>$value, 'current'=>'1')); } } if(isset($input['regions_wants'])){ foreach ($input['regions_wants'] as $key => $value){ $this->db->insert('org_regions', array('org_id'=>$id,'region_id'=>$value, 'current'=>'0')); } } //countrys $this->db->where('org_id', $id); $this->db->delete('org_countrys'); if(isset($input['countrys_current'])){ foreach ($input['countrys_current'] as $key => $value){ $this->db->insert('org_countrys', array('org_id'=>$id,'country_id'=>$value, 'current'=>'1')); } } if(isset($input['countrys_wants'])){ foreach ($input['countrys_wants'] as $key => $value){ $this->db->insert('org_countrys', array('org_id'=>$id,'country_id'=>$value, 'current'=>'0')); } } //okved $this->db->where('org_id', $id); $this->db->delete('org_okved'); if(isset($input['okved_list'])){ foreach ($input['okved_list'] as $key => $value){ $this->db->insert('org_okved', array('org_id'=>$id,'code'=>$value)); } } //props $prop_data['products'] = trim($input['ved_prod']); $prop_data['advantage'] = trim($input['ved_adv']); $prop_data['teh_advantage'] = trim($input['ved_tecadv']); $prop_data['org_id'] = $id; if($prop_data['products']!='' || $prop_data['advantage']!='' || $prop_data['teh_advantage']!=''){ $this->db->insert('org_prop', $prop_data); } //send email to ivm $mdata['full_title'] = $data['full_title']; $mdata['email'] = $data['email']; $this->send_org_mail($mdata); return array('id' => $id); } private function send_org_mail($data){ $user_quest_body = 'Заполнена новая форма добавления организации ВЭД
'; $user_quest_body .= 'Организация: ' . $data['full_title'] . '
'; $user_quest_body .= 'Почта: ' . $data['email'] . '
'; $user_quest_body .= '

Почтовый робот mbmosreg.ru'; //send email to ask@mbmosreg.ru $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.yandex.ru', 'smtp_port' => '465', 'smtp_user' => 'ask@mbmosreg.ru', 'smtp_pass' => 'loser666', 'mailtype' => 'html', 'starttls' => true, 'newline' => "\r\n" ); $this->load->library('email', $config); $this->email->from('ask@mbmosreg.ru', 'Форма добавления организации ВЭД'); //$this->email->to('ivm1@rcsme.ru'); $this->email->subject('Форма добавления организации ВЭД'); $this->email->message($user_quest_body); if ( ! $this->email->send()) { echo 'Что-то пошло не так :('; die(); // die($this->email->print_debugger()); } $this->email->clear(); } } ?>