Created
September 29, 2016 22:30
-
-
Save cyberlex404/845c393dfa17d6cc2236afb0cbf38949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\thomas_service\Form; | |
use Drupal\Component\Serialization\Json; | |
use Drupal\Core\Entity\EntityStorageInterface; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\taxonomy\Entity\Term; | |
use Drupal\thomas_service\Entity\ServiceCenter; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Entity\Query\QueryFactory; | |
/** | |
* Class ServiceFilter. | |
* | |
* @package Drupal\thomas_service\Form | |
*/ | |
class ServiceFilter extends FormBase { | |
protected $entity_query; | |
protected $city_storage; | |
protected $sc_storage; | |
public function __construct(QueryFactory $entity_query, EntityStorageInterface $sc_storage) { | |
$this->entity_query = $entity_query; | |
$this->sc_storage = $sc_storage; | |
} | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity.query'), | |
$container->get('entity.manager')->getStorage('service_center') | |
); | |
} | |
protected function getCityList() { | |
return $this->entity_query->getAggregate('service_center') | |
->condition('status', 1) | |
->groupBy('field_city') | |
->aggregate('id', 'COUNT') | |
->execute(); | |
} | |
protected function getCitySelect() { | |
$list = $this->getCityList(); | |
// dpm($list); | |
$select = []; | |
foreach ($list as $key => $item) { | |
$tid = $item['field_city_target_id']; | |
$term = Term::load($tid); | |
$select[$tid] = $term->label(); | |
} | |
return $select; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'service_filter'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$default_city = 2; | |
$attached = []; | |
$attached['drupalSettings']['thomas_service']['ServiceCenterItems'] = $this->getServiceCenters(); | |
//$form['#attached'] = $attached; | |
$form['city'] = array( | |
'#type' => 'select', | |
'#title' => $this->t('City'), | |
'#options' => $this->getCitySelect(), | |
'#default_value' => $default_city, | |
'#size' => 1, | |
'#ajax' => [ | |
// Could also use [ $this, 'colorCallback']. | |
'callback' => '::cityCallback', | |
'wrapper' => 'scm-wrapper-list', | |
], | |
'#attached' => $attached, | |
); | |
$form['scm_wrapper'] = [ | |
'#type' => 'container', | |
'#attributes' => ['id' => 'scm-wrapper'], | |
]; | |
$form['scm_wrapper']['map'] = [ | |
'#type' => 'markup', | |
'#markup' => '<div id="ssc-map" style="height: 490px; width: 100%"></div>', | |
'#attached' => [ | |
'library' => ['thomas_service/thomas_service_map'], | |
], | |
]; | |
$form['scm_wrapper_list'] = [ | |
'#type' => 'container', | |
'#attributes' => ['id' => 'scm-wrapper-list'], | |
]; | |
/* | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Submit'), | |
); | |
*/ | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
parent::validateForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
// Display result. | |
foreach ($form_state->getValues() as $key => $value) { | |
drupal_set_message($key . ': ' . $value); | |
} | |
} | |
/** | |
* Implements callback for Ajax event on color selection. | |
* | |
* @param array $form | |
* From render array. | |
* @param \Drupal\Core\Form\FormStateInterface $form_state | |
* Current state of form. | |
* | |
* @return array | |
* Color selection section of the form. | |
*/ | |
public function cityCallback(array &$form, FormStateInterface $form_state) { | |
$city = $form_state->getValue('city'); | |
// drupal_set_message($city, 'city'); | |
$attached = []; | |
$data = $this->getServiceCenterByCity($city); | |
$attached['drupalSettings']['thomas_service']['ServiceCenterItems'] = $data; | |
$items = $data[$city]['data']; | |
// dpm($data, 'items'); | |
$count = count($items); | |
$output = '<div id="sc-list"><div class="sc-list-count">Сервис центров в городе: ' . $count . '</div><ol>'; | |
foreach ($items as $item) { | |
$output .= '<li class="sc-item"><span class="sc-name">' . $item['name'] . '</span> <b>Телефоны:</b>' . $item['telephones'] . ' <b>Адрес:</b> ' . $item['address'].'</li>'; | |
} | |
$output .= '</ol></div>'; | |
$form['scm_wrapper_list']['list'] = [ | |
'#type' => 'markup', | |
'#markup' => $output, | |
'#attached' => $attached, | |
]; | |
return $form['scm_wrapper_list']; | |
} | |
protected function getServiceCenterByCity($city) { | |
$data = []; | |
$service_ids = $this->entity_query->get('service_center') | |
->condition('status', 1) | |
->condition('field_city', $city) | |
->execute(); | |
$services = $this->sc_storage->loadMultiple($service_ids); | |
$json = [ | |
'type' => 'FeatureCollection', | |
'features' => [], | |
]; | |
foreach ($services as $id => $entity) { | |
$data[$id] = [ | |
'id' => $entity->id(), | |
'name' => $entity->label(), | |
'gps' => [$entity->field_geo_lon->value, $entity->field_geo_lat->value], | |
'telephones' => $entity->field_telephones->value, | |
'address' => $entity->field_adres->value, | |
]; | |
$json_item = [ | |
'type' => 'Feature', | |
'id' => $entity->id(), | |
'geometry' => [ | |
'type' => 'Point', | |
'coordinates' => [$entity->field_geo_lon->value, $entity->field_geo_lat->value], | |
], | |
'properties' => [ | |
'balloonContent' => $entity->field_telephones->value, | |
'clusterCaption' => $entity->label(), | |
'hintContent' => $entity->label(), | |
], | |
]; | |
$json['features'][] = $json_item; | |
} | |
//dpm($services, 'se'); | |
return [$city => [ | |
'data' => $data, | |
'json' => Json::encode($json), | |
]]; | |
} | |
protected function getServiceCenters() { | |
$list = $this->getCityList(); | |
// dpm($list); | |
$centers = []; | |
foreach ($list as $key => $item) { | |
$tid = $item['field_city_target_id']; | |
$center = $this->getServiceCenterByCity($tid); | |
$centers[$tid] = $center[$tid]; | |
} | |
return $centers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment