Saturday, May 30, 2015

Yii EGMap With Autocomplete Search Location


1. Add this function into extension EGMap.php
  public function addAutocomplete($inputId = null)
{
   
    if ($inputId === null)
      $autocomplete = new EGMapAutocomplete();
    else
      $autocomplete = new EGMapAutocomplete($inputId);

    $this->resources->add('autocomplete', new CTypedList('EGMapAutocomplete'));    
    $this->resources->itemAt('autocomplete')->add($autocomplete);
}
public function getAutocompleteJs()
{
    $return = '';
    if (null !== $this->resources->itemAt('autocomplete'))
    {
        foreach ($this->resources->itemAt('autocomplete') as $autocomplete)
        {
            $return .= $autocomplete->toJs($this->getJsName());
            $return .= "\n      ";
        }
    }
    return $return;
}

2. Create New file EGMapAutocomplete.php
class EGMapAutocomplete extends EGMapBase
{
  //String $url url of image
  protected $inputId;
  protected $marker_object = 'google.maps.places.Autocomplete';  
  
  const GMAP_LIBRARY = 'http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false';
   
  public function __construct( $inputId = 'searchTextField',$js_name = 'autocomplete')
  {
    $this->inputId = $inputId;
    $this->setJsName($js_name);    
  }
  
  /**
   * 
   * @return inputId
   */
  public function getInputId()
  {
    return $this->inputId;
  }

  /**
   * 
   * @return string js code to create the autoComplete
   */
 public function toJs($map_js_name = 'map')
 {
  $this->options['map'] = $map_js_name;
    $return = "var input = document.getElementById('".EGMap::encode($this->inputId)."');". PHP_EOL;
  $return .= $this->getJsName() . ' = new ' . $this->marker_object . '(input);' . PHP_EOL;
    $return .= $this->getJsName().".bindTo('bounds', ".$map_js_name.");" . PHP_EOL;

    $event = new EGMapEvent('place_changed', "function() {
          ".$map_js_name."_info_window.close();
          var place = autocomplete.getPlace();
          if (place.geometry.viewport) {
            ".$map_js_name.".fitBounds(place.geometry.viewport);
          } else {
            ".$map_js_name.".setCenter(place.geometry.location);
            ".$map_js_name.".setZoom(17);  
          }
          ".$map_js_name."_marker.setPosition(place.geometry.location);
          
          var address = '';
          if (place.address_components) {
            address = [(place.address_components[0] &&
                        place.address_components[0].short_name || ''),
                       (place.address_components[1] &&
                        place.address_components[1].short_name || ''),
                       (place.address_components[2] &&
                        place.address_components[2].short_name || '')
                      ].join(' ');
          }

          ".$map_js_name."_info_window.setContent('
' + place.name + '
' + address); ".$map_js_name."_info_window.open(map, marker); }",false); $return .= $event->getEventJs($this->getJsName()) . PHP_EOL; return $return; } }
3. Add $params .= '&libraries=places'; this into registerMapScript function before CGoogleApi::init();
4. Add $init_events[] = $this->getAutocompleteJs(); into registerMapScript function bellow $init_events[] array.
 

Yii::import('ext.EGMap.*');
$gMap = new EGMap();
$gMap->setJsName('map');
$gMap->setWidth(827);
$gMap->setHeight(400);
$gMap->zoom = 6;
$gMap->addAutocomplete();

$mapTypeControlOptions = array(
  'position' => EGMapControlPosition::RIGHT_TOP,
  'style' => EGMap::MAPTYPECONTROL_STYLE_HORIZONTAL_BAR
);
 
$gMap->mapTypeId = EGMap::TYPE_ROADMAP;
$gMap->mapTypeControlOptions = $mapTypeControlOptions;
 
$icon = new EGMapMarkerImage("http://google-maps-icons.googlecode.com/files/workoffice.png");
 

$icon->setSize(32, 37);
$icon->setAnchor(16, 16.5);
$icon->setOrigin(0, 0);

$count = $dataProvider->getTotalItemCount();
 
if ($count > 0) {
 $i = 0;
 
 foreach($dataProvider->getData() as $record) {
 

     $marker[$i] = new EGMapMarker($record->lat, $record->lng, array('title' => Yii::t('catalog', $record->nama),
             'icon'=>$icon, 'draggable'=>false), 'marker', array());
 
     $info_window_m[$i] = new EGMapInfoWindow('
'.$record->nama.'
'); $marker[$i]->addHtmlInfoWindow($info_window_m[$i]); $gMap->addMarker($marker[$i]); $i++; } $gMap->setCenter($marker[0]->lat, $marker[0]->lng); $gMap->zoom = 12; } else { $gMap->setCenter(23.716564, 90.402359); } $gMap->renderMap(array(), Yii::app()->language);

Tuesday, May 26, 2015

Yii CJuiDatePicker

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model' => $model,
    'attribute' => 'from_date',
    'options' => array(           
        'dateFormat' => 'dd-mm-yy',    
        'showOtherMonths' => true,   
        'selectOtherMonths' => true,
        'changeYear' => true,         
        'changeMonth' => true,        
        'showButtonPanel' => FALSE,  
    ),
    'htmlOptions' => array(
        'size' => '10',
        'maxlength' => '10',
    ),
));

Table prefix in Yii

'db'=>array(
 'connectionString' => 'mysql:host=localhost;dbname=proficiency',
 'emulatePrepare' => true,
 'username' => 'test',
 'password' => '12345665',
 'charset' => 'utf8',
        'tablePrefix' => 'my_',
                    
  ),

Saturday, May 23, 2015

DataProvider with date condition

$dataProvider2=new CActiveDataProvider('LongCourse', array(  
     'criteria'=>array(
        'condition'=>"DATE(t.course_show_date) >= DATE(NOW()) AND published=:published ",
        'params'=>array(':published'=>'Yes'),
 ),     
 'sort'=>array(
            'defaultOrder'=>'id DESC',
        ),
       'pagination'=>false,   
       )); 

Friday, May 22, 2015

Yii file_exists() Checking

if(file_exists("".Yii::getPathOfAlias('webroot')."/images/certificate/".$model->certificate_coc_no"")){
  
   echo CHtml::image(Yii::app()->baseUrl."/images/certificate/".$model->certificate_coc_no,
      $model->name,
      array("width"=>"100px" ,"height"=>"80px", "style"=>"vertical-align:middle; margin-left: auto; margin-right: auto; display: block; float: none;"));
   
  }
OR
if(file_exists("".YiiBase::getPathOfAlias('webroot')."/images/certificate/".$model->certificate_coc_no"")){
  
   echo CHtml::image(Yii::app()->baseUrl."/images/certificate/".$model->certificate_coc_no,
      $model->name,
      array("width"=>"100px" ,"height"=>"80px", "style"=>"vertical-align:middle; margin-left: auto; margin-right: auto; display: block; float: none;"));
   
  }