How to create marker cluster yii2 from mysql? - yii2

I want to create marker cluster google maps from myql database in yii2 framework, I couldn't use the mysql database in google maps marker cluster
<!-- language: lang-html -->
<div class="maps-index" width="1070px">
<?php
$map = new Map([
'center' => new LatLng(['lat' => 52.1326, 'lng' => 5.2913]), // Center of Netherlands
'zoom' => 7,
]);
$cities = [
'Utrecht' => new LatLng(['lat' => 52.0841659, 'lng' => 5.0124518]),
'Groningen' => new LatLng(['lat' => 53.2216845, 'lng' => 6.4947123]),
'Amsterdam' => new LatLng(['lat' => 52.3745291, 'lng' => 4.7585311]),
'Amstelveen' => new LatLng(['lat' => 52.2861961, 'lng' => 4.7820703]),
'Hilversum' => new LatLng(['lat' => 52.2315507, 'lng' => 5.0905084]),
'Ede' => new LatLng(['lat' => 52.052436, 'lng' => 5.6301072]),
'Amersfoort' => new LatLng(['lat' => 52.1589302, 'lng' => 5.3077834]),
'Zwolle' => new LatLng(['lat' => 52.514148, 'lng' => 5.9669147]),
'Leiden' => new LatLng(['lat' => 52.1517316, 'lng' => 4.4466504]),
'Rotterdam' => new LatLng(['lat' => 51.9279514, 'lng' => 4.4203666]),
'The Hague' => new LatLng(['lat' => 52.0716334, 'lng' => 4.2398287]),
'Delft' => new LatLng(['lat' => 51.9995297, 'lng' => 4.3286785]),
'Eindhoven' => new LatLng(['lat' => 51.4455368, 'lng' => 5.3814706]),
];
foreach ($cities as $city => $lat_lng) {
$marker = new Marker([
'position' => $lat_lng,
'title' => $city,
'clickable' => true,
]);
$marker->attachInfoWindow(new InfoWindow(['content' => "<strong>{$city}</strong>"]));
$map->addOverlay($marker);
}
$map->center = $map->getMarkersCenterCoordinates();
$map->zoom = $map->getMarkersFittingZoom() - 1;
?>
<div class="row">
<div class="col-sm-12">
<?= $map->display(); ?>
</div>
</div>
</div>
<?php
/* #var $this yii\web\View */
use dosamigos\google\maps\LatLng;
use dosamigos\google\maps\overlays\InfoWindow;
// edofre objects instead of dosamigos
use edofre\markerclusterer\Map;
use edofre\markerclusterer\Marker;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = 'Maps Clusterer';
?>
<div class="maps-index" width="1070px">
<?php
$map = new Map([
'center' => new LatLng(['lat' => 52.1326, 'lng' => 5.2913]), // Center of Netherlands
'zoom' => 7,
]);
$cities = [
'Utrecht' => new LatLng(['lat' => 52.0841659, 'lng' => 5.0124518]),
'Groningen' => new LatLng(['lat' => 53.2216845, 'lng' => 6.4947123]),
'Amsterdam' => new LatLng(['lat' => 52.3745291, 'lng' => 4.7585311]),
'Amstelveen' => new LatLng(['lat' => 52.2861961, 'lng' => 4.7820703]),
'Hilversum' => new LatLng(['lat' => 52.2315507, 'lng' => 5.0905084]),
'Ede' => new LatLng(['lat' => 52.052436, 'lng' => 5.6301072]),
'Amersfoort' => new LatLng(['lat' => 52.1589302, 'lng' => 5.3077834]),
'Zwolle' => new LatLng(['lat' => 52.514148, 'lng' => 5.9669147]),
'Leiden' => new LatLng(['lat' => 52.1517316, 'lng' => 4.4466504]),
'Rotterdam' => new LatLng(['lat' => 51.9279514, 'lng' => 4.4203666]),
'The Hague' => new LatLng(['lat' => 52.0716334, 'lng' => 4.2398287]),
'Delft' => new LatLng(['lat' => 51.9995297, 'lng' => 4.3286785]),
'Eindhoven' => new LatLng(['lat' => 51.4455368, 'lng' => 5.3814706]),
];
foreach ($cities as $city => $lat_lng) {
$marker = new Marker([
'position' => $lat_lng,
'title' => $city,
'clickable' => true,
]);
$marker->attachInfoWindow(new InfoWindow(['content' => "<strong>{$city}</strong>"]));
$map->addOverlay($marker);
}
$map->center = $map->getMarkersCenterCoordinates();
$map->zoom = $map->getMarkersFittingZoom() - 1;
?>
<div class="row">
<div class="col-sm-12">
<?= $map->display(); ?>
</div>
</div>
</div>

Related

Call to undefined method app\models\User::instance() setting up kartik export yii2

Trying to set up kratik grid export in Yii after doing everything I keep getting
Call to undefined method app\models\User::instance()
here is my code
$exportedValues = [['class' => 'kartik\\grid\\SerialColumn'], ['attribute' => 'deduction_date'], ['attribute' => 'deduction_amount'], ['attribute' => 'deduction_remark'], ['class' => 'kartik\\grid\\ActionColumn']];
$export = ExportMenu::widget(['dataProvider' => $dataProvider, 'columns' => $exportedValues, 'noExportColumns' => [0, 3], 'columnSelectorOptions' => ['label' => 'Columns', 'class' => 'btn btn-danger'], 'target' => '_blank', 'fontAwesome' => true, 'dropdownOptions' => ['label' => 'Export', 'class' => 'btn btn-success']]);
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel,
'columns' => [['class' => 'yii\\grid\\SerialColumn'],
'first_name', 'last_name', 'email_id', 'phone', ['class' => 'yii\\grid\\ActionColumn']]]); ?>
Try to install kartik, or check your kartik version
php composer.phar require kartik-v/yii2-widgets "*"
Had the same issue.
on the source code of ExportMenu.php I replaced:
$model = $modelClass::instance();
by:
$model = new $modelClass;
solved it for me.

Dynamic value graph creation in yii2 using GoogleChart?

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances
<?php
$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
'sales'=>array(),
'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => array(
array('Year', 'Sales', 'Expenses'),
array($arr['id'],$arr['sales'],$arr['expenses']),
),
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
?>
first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.
you have to create an array of values and pass it to the graph widget as following
$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses');
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
$graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $graph_data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
Try this
action
$model=Employee::find()->select(['id','sales','expenses'])->all();
$data[]=["id","sales","expenses"];
foreach ($model as $item) {
$data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
}
return $this->render('test',['data'=>$data]);
view
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

Drupal custom form get and record current user

I looked up on the internet and write an custom module.
I want to add a form for people to add some information into database.
I use "data" module to create a table called "profile".
And my module's code is (addfood.module):
<?php
/**
* Implements hook_menu().
*/
function addfood_menu() {
$items['food/add'] = array(
'title' => '新增食物檔案',
'page callback' => 'addfood_page',
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_permission.
*/
function addfood_permission() {
return array(
'addfood module' => array(
'title' => t('Addfood module permission'),
));
}
/**
* Returns form render array.
*/
function addfood_form($form, &$form_state) {
if (user_access('addfood module')) {
//Allowed
$form['name'] = array(
'#title' => t('名稱'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['calorie'] = array(
'#title' => t('卡路里'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('填寫卡路里(單位:千卡)'),
);
$form['water'] = array(
'#title' => t('水'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['protein'] = array(
'#title' => t('蛋白質'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['saturated_fat'] = array(
'#title' => t('飽和脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['trans_fat'] = array(
'#title' => t('反式脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['carbohydrates'] = array(
'#title' => t('碳水化合物'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['dietary_fiber'] = array(
'#title' => t('膳食纖維'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['cholesterol'] = array(
'#title' => t('膽固醇'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sodium'] = array(
'#title' => t('鈉'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sugars'] = array(
'#title' => t('糖'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['notes'] = array(
'#title' => t('Please explain your what makes you a prime candidate for our beta test'),
'#type' => 'textarea',
'#resizable' => TRUE,
'#description' => t('Beta spaces are limited, but let us know if there is a really good reason to let you in.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
} else {
//Access denied
header('Location: ../user/login?destination=food/add');
}
}
/**
* Menu callback.
*/
function addfood_page() {
return drupal_get_form('addfood_form');
}
/**
* Submission handler for form_example -> Insert into database
*/
function addfood_form_submit($form, &$form_state) {
$fe_id = db_insert('profile')
->fields(array(
'name' => $form_state['values']['name'],
'calorie' => $form_state['values']['calorie'],
'water' => $form_state['values']['water'],
'protein' => $form_state['values']['protein'],
'saturated_fat' => $form_state['values']['saturated_fat'],
'trans_fat' => $form_state['values']['trans_fat'],
'carbohydrates' => $form_state['values']['carbohydrates'],
'dietary_fiber' => $form_state['values']['dietary_fiber'],
'cholesterol' => $form_state['values']['cholesterol'],
'sodium' => $form_state['values']['sodium'],
'notes' => $form_state['values']['notes'],
))
->execute();
drupal_set_message(t('HO GYA Submit!!!!'));
return $form;
}
?>
If I want to record who add the information automatically(by get the current username).
Anyone has idea how to do it? Thank you.
Figure is my table schema.
(source: libk.info)
You can use global user variable to get current user. You can test it:
<?php
global $user;
print_r($user->name);
?>

Drupal Html array. How to reference a changing radio selection within a fieldset

I cannot make this Drupal html array fire the visible property on change of radio option. I have moved everything inside of the fieldset, not sure if this makes any difference.
Does anyone know why it isn't firing?
function services_formation_founders($form, &$form_state) {
$form = array();
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => t('Founders form'),
);
$form['founder']['add_officer'] = array(
'#type' => 'fieldset',
'#title' => t('Add Founder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(TRUE,
),
),
);
$form['founder']['add_officer']['founder_type'] = array(
'#type' => 'radios',
'#options' => array(
'individual' => t('Individual'),
'corporate' => t('Corporation'),
),
'#default_value'=>'individual',
'#title' => t('What type of Founder?')
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
$form['founder']['add_officer']['individual'] = array(
'#type' => 'textfield',
'#title' => t('Individual'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "individual"),
),
),
);
$form['founder']['add_officer']['corporation'] = array(
'#type' => 'textfield',
'#title' => t('Corporation'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "corporate"),
),
),
);
return $form;
}
Place $ sign at line no 2
$form = array();

Zend Framework decorator DL and DD style

If you add a Subform ( called "Step1" ) you will have this code:
<dl class="zend_form">
<dt id="Step1-label"></dt>
<dd id="Step1-element">
<fieldset id="fieldset-Step1" class="Step">
<dl>
.....
</dl>
</fieldset>
</dd>
</dl>
How can I add a class in the DL tag and DD tag?
For EXAMPLE:
<dl class="Step1DL">
<dd id="Step1-element" class="Step1DD">
How can I do it with the Zend Decorator?
Thanks again...
Try below code in model form file,
<?php
class Admin_Model_Form_Test extends Zend_Form
{
public $elementDecorators = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
array('Label', array('tag' => 'dt','class'=>'labmyaccountR'),
));
public $requiredElementDecorators = array(
'ViewHelper',
'Errors',
array('Description',array('escape'=>false,'tag'=>'span', 'placement' => 'append')),
array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
array('Label', array('tag' => 'dt','class'=>'labmyaccountR'),
));
public function EditForm($data = array())
{
$this->setMethod(Zend_Form::METHOD_POST);
$this->setEncType(Zend_Form::ENCTYPE_MULTIPART);
$this->setAction(
$this->getView()->getHelper('url')->url(array(
'controller' => 'test',
'action'=>'edittest'
))
);
$this->setDecorators(array(
'Description',
'FormElements',
'Form'
));
$fnameNotEmpty = new Zend_Validate_NotEmpty();
$fnameNotEmpty->setMessage('Tax value should not be empty');
$fnameStrlen = new Zend_Validate_StringLength(1, 20);
$name = new Zend_Form_Element_Text('taxvalue', array(
'label' => 'Sales Tax *',
'value' => $data['value'],
'class' => 'text-size text',
'tabindex' => '1',
'required' => true,
'validators' => array(
array($fnameNotEmpty, true),
array($fnameStrlen, true)
),
'filters' => array('StringTrim'),
'decorators' => $this->requiredElementDecorators,
));
$name->addValidator('Float',true);
$this->addElement($name);
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Update',
'tabindex' => '20',
'decorators' => $this->elementDecorators,
));
$submit->removeDecorator('Label');
$submit->removeDecorator('label');
$this->addElement($submit);
$id = new Zend_Form_Element_Hidden('tax_id', array(
'value' => $data['tax_id']
));
$id->removeDecorator('label');
$this->addElement($id);
return $this;
}
}
?>
try this, after you create the element:
for example:
$element = new Zend_Form_Element_Text('anelement');
$element->addDecorators(array(array('HtmlTag',array('tag' => 'dd', 'class' => 'yourclass' )));