/**
  * parse location into latitude and longitude
  * @param $location: address
  */
 public static function getLocationFromAddress($location)
 {
     $url = sprintf(self::geocode, urlencode($location));
     HW_HOANGWEB::load_class('HW_CURL');
     $resp = HW_CURL::curl_get($url);
     //other way
     #$resp = @file_get_contents($url);
     $json = json_decode($resp);
     if (isset($json->results[0])) {
         return $json->results[0]->geometry->location;
     }
 }
 /**
  * ajax handle
  */
 public function _hw_catch_gdrive_formfields()
 {
     if (!wp_verify_nonce($_REQUEST['nonce'], "hw_catch_gdrive_formfields_nonce")) {
         exit("No naughty business please");
     }
     $gform_id = urldecode($_REQUEST['gform_id']);
     //google form ID
     $fields = array();
     //get form content
     $url_action = hw_wpcf7_valid_gform_url($gform_id);
     $html = HW_CURL::curl_get($url_action);
     //parse form fields
     preg_match_all('#<(input|textarea|select).+?\\/?>#', $html, $result);
     if (isset($result[0]) && is_array($result[0])) {
         foreach ($result[0] as $f) {
             preg_match_all('#(name|aria-label)=\\"([^"]*)\\"#', $f, $atts);
             if (isset($atts[2]) && is_array($atts[2]) && count($atts[2])) {
                 $fname = strtolower($atts[2][0]);
                 //field name should be lowercase
                 if (strrpos($fname, 'entry.', -strlen($fname)) === FALSE) {
                     continue;
                 }
                 $fields[$fname] = isset($atts[2][1]) ? trim($atts[2][1]) : '';
                 //get fields name & label
                 //detect sendEmail field
                 if (trim($fields[$fname]) == 'sendEmail') {
                     $avaiable_fsendEmail = true;
                 }
             }
         }
     }
     $out = '<div class="result hwcf-parse-gform-result">';
     if (count($fields)) {
         $out .= '<span>Chú ý: tạo thêm thuộc tính "gfield" cho các trường form của bạn bằng cách tên các trường tương ứng dưới đây:
                 </span>';
         $ex_scfield = '';
         $has_special_field = false;
         //check exists form has special field
         $out .= '<table>';
         //display fields
         foreach ($fields as $name => $label) {
             //check for special field
             if (isset(self::$special_gfields[$label])) {
                 $has_special_field = true;
                 //marked exists one or more special fields
                 continue;
                 //ignore special fields
             }
             if (!$ex_scfield) {
                 $ex_scfield = '<tr><td colspan="2"><br/><span>VD: <strong>[text* your-name gfield:' . $name . ']</strong></span>';
                 $out .= $ex_scfield . '<hr/></td></tr>';
             }
             $out .= '<tr>';
             $out .= '<td valign="bottom"><div class="g-field"><span>' . $label . '</span><br/><input type="text" readonly value="gfield:' . $name . '"/></div></td>';
             $out .= '<td valign="bottom"><div class="g-field"><input type="text" value="placeholder \'' . $label . '\'" readonly/></div></td>';
             $out .= '</tr>';
         }
         //display special fields
         if ($has_special_field) {
             $special_fields_shortcode_tag = '[hw_wpcf7_special_fields hwspgf ';
             $reverse_fields = array_flip($fields);
             $out .= '<tr><td colspan="2" class="special-fields-label">Các trường đặc biệt sử dụng bởi Google Script</td></tr>';
             foreach (self::$special_gfields as $flabel => $desc) {
                 if (isset($reverse_fields[$flabel])) {
                     $special_fields_shortcode_tag .= $flabel . ':' . $reverse_fields[$flabel] . ' ';
                     $out .= '<tr class="gform-special-field">';
                     $out .= '<td valign="bottom" colspan=""><div class="g-field"><span>' . $flabel . '</span><br/><input type="text" readonly value="gfield:' . $reverse_fields[$flabel] . '"/></div></td>';
                     $out .= '<td valign="top"><div class="g-field">' . $desc . '</div></td>';
                     $out .= '</tr>';
                 }
             }
             $special_fields_shortcode_tag = trim($special_fields_shortcode_tag);
             //truncate space around this variable value
             $special_fields_shortcode_tag .= ']';
         }
         $out .= '</table>';
         if (!isset($avaiable_fsendEmail)) {
             $out .= '<span style="color:red">Cảnh báo: không tìm thấy nhãn trường (chứa nội dung gửi email) "sendEmail" trong google form. Để có thể gửi mail từ google spreadsheet vui lòng thêm trường với nhãn "sendEmail" trong google form có liên kết vào spreadsheet đó.</span>';
         } else {
             $out .= '<p>Chúc mừng: đã tìm thấy trường lưu nội dung gửi email trong google form của bạn. Nội dung này tự động lấy từ Email template ở box "Email" ngay bên dưới.</p>';
             $out .= '<p>Copy trường shortcode này vào nội dung trường form fields ở trên, đặt phía dưới cùng của form hoặc bất kỳ vị trí nào đều hoạt động.<br/><input type="text" style="width:100%" value="' . $special_fields_shortcode_tag . '" readonly/></p>';
         }
     } else {
         $out .= 'Không tìm thấy dữ liệu. Hãy kiểm tra lại google form ID?';
     }
     $out .= '</div>';
     if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
         //$result = json_encode($result);
         echo $out;
     } else {
         header("Location: " . $_SERVER["HTTP_REFERER"]);
     }
     die;
 }