Create a business directory with K2

The power of K2 content construction kit saves you the cost and complexity of acquiring and managing a separate extension just to publish a business directory with the standard data fields used in the leading directories such as Yellowpage.com, Superpages.com etc, complete with Google Map and a contact form.

Step 1

Create an Extra Field group called directory and then the Extra Fields you desire.

directoryfields.jpg

 

Step 2

Make a duplicate of com_k2/templates/default and name it appropriately as 'directory'

catdirectory

 

Step 3

Edit category_item.php. This is the file for category item view

Find line <?php if($this->item->params->get('catItemExtraFields') && count($this->item->extra_fields)): ?>

To filter the fields and display only specific data, edit the conditional line <?php if($extraField->value != ''): ?> as

<?php if($extraField->value != '' && in_array($extraField->alias, array('address','primarphone','fax','weburl'))): ?>

The field aliases are shown for each field you created and must be entered in the array exactly.

Note: You can move the Extra Fields code block to any place in the file to adjust the display as desired.

 

Step 4

Edit item.php

Find line <?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>

Edit the same conditional line as before, only this time you need to ignore fields, in this case, the email address so note the ! exclamation before in_array.

<?php if($extraField->value != '' && !in_array($extraField->alias, array('emailaddress'))): ?>

 

Now at the top of the file after the JEXEC definition, add the following

if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)) {
$fields = $this->item->extraFields;
}

This is to enable extraction of individual field values from the object via each field's alias.

Get the address field value and display the Google map. Place the code where you want the map displayed.

<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)) { ?>
<address class="gmaps"><?php echo $fields->address->value; ?></address>

<script>
jQuery(document).ready(function(){
  jQuery(".gmaps").each(function(){
    var embed ="<iframe width='100%' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com/maps?&amp;q="+ encodeURIComponent( jQuery(this).text() ) +"&amp;output=embed'></iframe>";
    jQuery(this).html(embed);

   });
});
</script>
<?php } ?>

 

Get the email address value and display a contact form. This requires a form code snippet and there are many available. In this case we use a simple form script which sends mail via PHP mail. If you need Sendmail or use remote SMTP, another scripting is needed.

In order to avoid clutter in the item.php file, create a specific file in the same directory and name it contactform.php and insert the following coding.

<?php defined('_JEXEC') or die;

if(isset($_POST['email'])) {

    $email_to = $fields->contactform->value;

    function died($error) {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists

    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $subject = $_POST['subject']; // required
    $message = $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered is invalid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The Name you entered is invalid.<br />';
  }

  if(strlen($message) < 2) {
    $error_message .= 'The message you entered is invalid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Hello.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= clean_string($message)."\n";
    $email_message .= "\n---\n".clean_string($first_name)."\n";
    $email_message .= clean_string($telephone)."\n";


// create email headers
$subject = clean_string($subject);
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message, $headers);

?>

Thank you for contacting us. We will be in touch with you very soon.

<?php } ?>


<form name="contactform" method="post" action="<?php echo JURI::current(); ?>">

  <label for="first_name">Name *</label>
  <input  type="text" name="first_name" maxlength="50" size="30">

  <label for="email">Email Address *</label>
  <input  type="text" name="email" maxlength="80" size="30">

  <label for="telephone">Phone</label>
  <input  type="text" name="telephone" maxlength="30" size="30">

  <label for="subject">Subject *</label>
  <input  type="text" name="subject" maxlength="50" size="30">

  <label for="message">Message *</label>
  <textarea  name="message" maxlength="1000" cols="25" rows="6"></textarea>
  <p><input class="btn" type="submit" value="Submit"></p>
</form>

 

Now include the file. Place the following where you want the from to display

<?php if( !empty($fields->contactform->value) ) {
echo '<hr /><h4>Contact ' .$this->item->title.'</h4>';
include_once (JPATH_COMPONENT.'/templates/directory/contactform.php');
}
?>

 

Step 5

Create a category, assign to the directory extra fields group and choose the directory template. Be sure to enable extra fields in Category Item View and Item view options.


Tuesday, 25 August 2015 11:22  |   9239 views

 

Copyright © 2017 CMSEnergizer.com. All Rights Reserved.
This website is powered by the Joomla!©™ 3 Website Framework | File download and article manager component by K2 | Advanced Module Manager and Modules Anywhere by NoNumber Elements | Website hosting and development by WebsiteDons

CMSEnergizer.com is owned and operated by Emuzement Net Inc, 4532 West Kennedy Blvd. Ste 180 33609 Tampa, Florida USA