/** * @param int $cityId * @return array|null */ public function loadDistrict($cityId) { /* @var $dtMapper \Address\Model\DistrictMapper */ $dtMapper = $this->getServiceLocator()->get('Address\\Model\\DistrictMapper'); $dt = new \Address\Model\District(); $dt->setCityId($cityId); return $dtMapper->fetchAll($dt); }
public function loadAction() { $sl = $this->getServiceLocator(); $district = new \Address\Model\District(); $district->setCityId((int) $this->getRequest()->getQuery()->get('cityId')); /*@var $districtMapper \Cart\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); return new JsonModel($district->toSelectBoxArray($districtMapper->fetchAll($district))); }
public function indexAction() { $add = new \Address\Model\Book(); $addMapper = $this->getServiceLocator()->get('Address\\Model\\BookMapper'); /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $this->getServiceLocator()->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $this->getServiceLocator()->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); $districts = array(); if (!!($cityId = $this->getRequest()->getPost()->get('cityId'))) { $district->setCityId($cityId); $districts = $districtMapper->fetchAll($district); } /* @var $form \User\Form\Signup */ $form = $this->getServiceLocator()->get('Address\\Form\\Book'); $form->setInputFilter($this->getServiceLocator()->get('Address\\Form\\BookFilter')); $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); $form->setDistricts($district->toSelectBoxArray($districts)); $form->bind($add); $viewModel = new ViewModel(); if ($this->params()->fromQuery('layout') == 'false') { $viewModel->setTerminal(true); } if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); $address = new Book(); if ($form->isValid()) { $address->exchangeArray((array) $this->getRequest()->getPost()); $address->setCreatedById($this->user()->getUser()->getId()); $addMapper->saveAddress($address); $this->redirect()->toUrl('/profile'); } } $viewModel->setVariable('form', $form); return $viewModel; }
/** * signup */ public function signupAction() { /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $sl = $this->getServiceLocator(); if ($this->user()->hasIdentity()) { return $this->redirect()->toRoute('home'); } $user = new User(); /* @var $userMapper \User\Model\UserMapper */ $userMapper = $sl->get('User\\Model\\UserMapper'); /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $sl->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); $districts = array(); if (!!($cityId = $request->getPost('cityId'))) { $district->setCityId($cityId); $districts = $districtMapper->fetchAll($district); } /* @var $form \User\Form\Signup */ $form = $sl->get('User\\Form\\Signup'); // $form->setInputFilter($sl->get('User\Form\SignupFilter')); // $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); // $form->setDistricts($district->toSelectBoxArray($districts)); $form->bind($user); $viewModel = new ViewModel(); if ($this->params()->fromQuery('layout') == 'false') { $viewModel->setTerminal(true); } if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { // $user->exchangeArray($form->getData()); if ($userMapper->isEmailAvailable($user)) { $user->exchangeArray($form->getData()); /* @var $serviceUser \User\Service\User */ $serviceUser = $sl->get('User\\Service\\User'); $serviceUser->signup($user); $viewModel->setVariable('success', true); $this->redirect()->toUrl('/'); } else { $mes = 'Email đã được sử dụng! Bạn hãy thử lại với Email khác.'; echo "<script type='text/javascript'>alert('{$mes}');</script>"; } /* @var $serviceUser \User\Service\User */ } else { $viewModel->setVariable('success', false); } } $viewModel->setVariable('form', $form); return $viewModel; }
/** * * @param \User\Model\User $item * @param unknown $options */ public function search($item, $options) { $select = $this->getDbSql()->select(array('u' => self::TABLE_NAME)); if ($item->getId()) { $select->where(['u.id' => $item->getId()]); } if ($item->getUsername()) { $select->where(['(u.username LIKE ? OR u.fullName LIKE ?)' => ['%' . $item->getUsername() . '%', '%' . $item->getUsername() . '%']]); } if ($item->getEmail()) { $select->where(['(u.email LIKE ?)' => '%' . $item->getEmail() . '%']); } if ($item->getActive()) { if ($item->getActive() > 0) { $select->where(['u.active' => 1]); } else { $select->where(['(u.active IS NULL OR u.active != ?)' => 1]); } } if ($item->getRole()) { $select->where(['u.role' => $item->getRole()]); } $select->order(['u.id' => 'DESC']); $paginator = $this->preparePaginator($select, $options, new User()); $userIds = []; $districIds = []; $cityIds = []; $ids = []; foreach ($paginator as $user) { /*@var $user \User\Model\User */ if ($user->getCreatedById()) { $userIds[$user->getCreatedById()] = $user->getCreatedById(); } if ($user->getDistrictId()) { $districIds[$user->getDistrictId()] = $user->getDistrictId(); } if ($user->getCityId()) { $cityIds[$user->getCityId()] = $user->getCityId(); } $ids[] = $user->getId(); } $dbAdapter = $this->getDbAdapter(); $users = []; if (count($userIds)) { $select = $this->getDbSql()->select(array('u' => self::TABLE_NAME)); $select->where(['u.id' => $userIds]); $query = $this->getDbSql()->buildSqlString($select); $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE); if ($rows->count()) { foreach ($rows as $row) { $createdBy = new User(); $createdBy->exchangeArray((array) $row); $users[$createdBy->getId()] = $createdBy; } } } $cities = []; if (count($cityIds)) { $select = $this->getDbSql()->select(array('c' => \Address\Model\CityMapper::TABLE_NAME)); $select->where(['id' => $cityIds]); $query = $this->getDbSql()->buildSqlString($select); $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE); if ($rows->count()) { foreach ($rows as $row) { $city = new \Address\Model\City(); $city->exchangeArray((array) $row); $cities[$city->getId()] = $city; } } } $districs = []; if (count($districIds)) { $select = $this->getDbSql()->select(array('c' => \Address\Model\DistrictMapper::TABLE_NAME)); $select->where(['id' => $districIds]); $query = $this->getDbSql()->buildSqlString($select); $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE); if ($rows->count()) { foreach ($rows as $row) { $distric = new \Address\Model\District(); $distric->exchangeArray((array) $row); $districs[$distric->getId()] = $distric; } } } if (count($paginator->getCurrentModels())) { foreach ($paginator->getCurrentModels() as $user) { if ($user->getCreatedById() && isset($users[$user->getCreatedById()])) { $user->addOption('createdBy', $users[$user->getCreatedById()]); } if ($user->getCityId() && isset($cities[$user->getCityId()])) { $user->setCity($cities[$user->getCityId()]); } if ($user->getDistrictId() && isset($districs[$user->getDistrictId()])) { $user->setDistrict($districs[$user->getDistrictId()]); } } } return $paginator; }
/** * load districts for select box * @param $element \Zend\Form\Element|string instance of Zend\Form\Element * or element name * @param $options array */ public function loadDistricts($element, $cityElement, $options = null) { /** @var $request \Zend\Http\Request */ $request = $this->getServiceLocator()->get('Request'); if ($request->isPost()) { $cityId = $request->getPost($cityElement->getName()); } else { $cityId = $request->getQuery($cityElement->getName()); } if (!$cityId && isset($options['cityId'])) { $cityId = $options['cityId']; } if (!$cityId) { return; } $district = new \Address\Model\District(); $district->setCityId($cityId); /** @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $this->getServiceLocator()->get('Address\\Model\\DistrictMapper'); $districts = $districtMapper->fetchAll($district); $arr = ['' => '- Quận huyện -']; if (is_array($districts)) { foreach ($districts as $d) { /** @var $d \Address\Model\District */ $arr[$d->getId()] = $d->getName(); } } if ($element instanceof \Zend\Form\Element) { $element->setValueOptions($arr); } else { $this->get($element)->setValueOptions($arr); } return $this; }
/** * * @param unknown $placeId * @param boolean $getAsId * fasle => text * true => array(country, cityId, districtId) * */ private function mathPlace($placeId, $getAsId = false) { if (!$placeId) { if ($getAsId) { return array('countryId' => null, 'cityId' => null, 'districtId' => null); } else { return null; } } list($officeAdapter, $officeSql) = $this->createOfficeAdapter(); /*@var $officeAdapter \Zend\Db\Adapter\Adapter */ /*@var $officeSql \Zend\Db\Sql\Sql */ $select = $officeSql->select(['l' => 'locations']); $select->where(['ID' => $placeId]); $query = $officeSql->buildSqlString($select); $row = $officeAdapter->query($query, $officeAdapter::QUERY_MODE_EXECUTE); if ($row->count()) { $row = (array) $row->current(); // nếu chỉ lấy text if (!$getAsId) { if ($row['type'] == 3) { // nếu địa điểm là quận huyện, lấy ngược lại parent để lấy thêm thành phố $select = $officeSql->select(['l' => 'locations']); $select->where(['ID' => $row['parent_id']]); $query = $officeSql->buildSqlString($select); $row2 = $officeAdapter->query($query, $officeAdapter::QUERY_MODE_EXECUTE); if ($row2->count()) { $row2 = (array) $row2->current(); return Format::displaySetItems([$row['title'], $row2['title']], ', '); } else { return $row['title']; } } else { return $row['title']; } } else { //nếu phải lấy ra dữ liệu khớp tỉnh thành $districtId = null; $cityId = null; $countryId = 243; switch ($row['type']) { case 3: // quận huyện $districtName = $row['title']; $cityName = null; // lấy ra tên thành phố $select = $officeSql->select(['l' => 'locations']); $select->where(['ID' => $row['parent_id']]); $query = $officeSql->buildSqlString($select); $rowCity = $officeAdapter->query($query, $officeAdapter::QUERY_MODE_EXECUTE); if ($rowCity->count()) { $rowCity = (array) $rowCity->current(); $cityName = $rowCity['title']; } // khớp với tỉnh thành bên mình $city = new \Address\Model\City(); $city->setNativeName($cityName); $cityMapper = $this->getServiceLocator()->get('\\Address\\Model\\CityMapper'); if ($cityMapper->isExisted($city)) { $cityId = $city->getId(); } $district = new \Address\Model\District(); $district->setCityId($cityId); $district->setName($districtName); $districtMapper = $this->getServiceLocator()->get('\\Address\\Model\\DistrictMapper'); if ($districtMapper->isExisted($district)) { $districtId = $district->getId(); } break; case 2: // thành phố $city = new \Address\Model\City(); $city->setNativeName($row['title']); $cityMapper = $this->getServiceLocator()->get('\\Address\\Model\\CityMapper'); if ($cityMapper->isExisted($city)) { $cityId = $city->getId(); } break; case 1: default: break; } return array('countryId' => $countryId, 'cityId' => $cityId, 'districtId' => $districtId); } } if ($getAsId) { return array('countryId' => null, 'cityId' => null, 'districtId' => null); } else { return null; } }
public function editAction() { $userMapper = $this->getServiceLocator()->get('\\User\\Model\\UserMapper'); /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $sl = $this->getServiceLocator(); $id = $this->params()->fromQuery('id'); if (!$id || !($user = $userMapper->get($id))) { return $this->page404(); } /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $sl->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); $districts = array(); if (!!($cityId = $request->getPost('cityId'))) { $district->setCityId($cityId); $districts = $districtMapper->fetchAll($district); } elseif ($user->getCityId()) { $district->setCityId($user->getCityId()); $districts = $districtMapper->fetchAll($district); } $form = new \System\Form\User\Edit($this->getServiceLocator()); $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); $form->setDistricts($district->toSelectBoxArray($districts)); $form->populateValues($user->toFormValues()); if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $data = $form->getData(); $user->exchangeArray($form->getData()); if (isset($data['password']) && $data['password']) { $user->setSalt($user->generateSalt()); $user->setPassword($user->createPassword()); } if (!$user->getRole()) { $user->setRole(User::ROLE_GUEST); } $userMapper = $this->getServiceLocator()->get('\\User\\Model\\UserMapper'); $userMapper->save($user); return $this->redirect()->toUrl('/system/user/index?id=' . $user->getId()); } } $viewModel = new ViewModel(); $viewModel->setVariable('form', $form); return $viewModel; }
public function editbookAction() { /* @var $request \Zend\Http\Request */ $request = $this->getRequest(); $sl = $this->getServiceLocator(); /* @var $addrbkMapper \Address\Model\BookMapper */ $addrbkMapper = $sl->get('Address\\Model\\BookMapper'); $addressBk = new \Address\Model\Book(); $addressBk->setId((int) $this->params('id')); $addressBk = $addrbkMapper->get($addressBk->getId()); /* @var $cityMapper \Address\Model\CityMapper */ $cityMapper = $sl->get('Address\\Model\\CityMapper'); $city = new \Address\Model\City(); /* @var $districtMapper \Address\Model\DistrictMapper */ $districtMapper = $sl->get('Address\\Model\\DistrictMapper'); $district = new \Address\Model\District(); if (!!($cityId = $request->getPost('cityId'))) { $district->setCityId($cityId); } else { $district->setCityId($addressBk->getCityId()); } $districts = $districtMapper->fetchAll($district); /* @var $form \Address\Form\Book */ $form = $sl->get('Address\\Form\\Book'); $form->setCities($city->toSelectBoxArray($cityMapper->fetchAll())); $form->setDistricts($district->toSelectBoxArray($districts)); $form->setInputFilter($sl->get('Address\\Form\\BookFilter')); $form->bind($addressBk); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $addressBk->exchangeArray((array) $request->getPost()); $addressBk->setCreatedById($this->user()->getUser()->getId()); $addrbkMapper->saveAddress($addressBk); } } if ($this->params()->fromQuery('format') == 'json') { return new JsonModel(array('addr' => $addressBk, 'code' => 1)); } $viewModel = new ViewModel(); $viewModel->setVariable('form', $form); $viewModel->setVariable('addressBook', $addressBk); return $viewModel; }