Jump to:

22993 Posts in 11829 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » How to create an address book in SS?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 2529 Views
  • jimw
    Avatar
    Community Member
    13 Posts

    How to create an address book in SS? Link to this post

    Since I'm very new to SS, and since I didn't find an address book extension, I need to build one if I am going to use SS for a project I'm working on. How would I tackle this? It looks like I could use the ContactForm tutorial as a start, just making it more elaborate with many other fields.

    Any suggestions would be appreciated.

    Thank you, jim

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: How to create an address book in SS? Link to this post

    Would you like to be able to add contacts from the CMS or front end? Either way you start by creating a Contact.php dataobject then you can either assign those dataobjects to a page or you could use modeladmin which is better for managing hundreds or thousands of objects such as heaps of contacts.

    If you include these two files in your mysite/code folder then visit www.yoursite.com/dev/build it will create the object and will also create a managment interface if you visit www.yoursite.com/admin/contacts

    Hope this gets you started!

    Contact.php

    <?php

    class Contact extends DataObject
    {
       static $db = array (
          'Name' => 'Varchar',
          'Address' => 'Varchar',
          'Phone' => 'Int'
       );
       
       static $has_one = array (
          'Attachment' => 'Image'
       );

    static $searchable_fields = array(
    'Name',
    'Phone'
    );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextareaField('Address', "Address"),
             new PhoneNumberField('Phone', "Phone Number"),
             new FileIFrameField('Attachment')
          );
       }
    }

    ?>

    ContactAdmin.php

    <?php

    class ContactAdmin extends ModelAdmin {

    public static $managed_models = array(
    'Contact'
    );

    static $url_segment = 'contacts'; // will be linked as /admin/contacts
    static $menu_title = 'Address Book';

    }

    ?>

  • jimw
    Avatar
    Community Member
    13 Posts

    Re: How to create an address book in SS? Link to this post

    Thank you very much. That certainly was simple. I tried it and it worked like a charm. Now I can expand on that and add some more fields and pieces of information that I would like to store.

  • haantje72
    Avatar
    Community Member
    69 Posts

    Re: How to create an address book in SS? Link to this post

    i've been experimenting etc for hours. Made an standard ContactPage.php and admin described as above. Easy got it into the cms but....
    How do i get the page seen in the website.
    Made an ContactPage.php standard, did not show up, copied the db names in $db = array. In Contact.php in one array call ContactPage
    In the cms i can select a pagetype contact but do not think the right way

    ContactPage.ss
    In the cms i can make a new page with type contact, but allways shows a page with no adresses?
    after $Content i filled $Name, $Address, etc.
    even tried it with <% control Contact %> <% end control %>

    The only thing i want is a searchbar on the page to search city, Name and Province. The result must show then.

    Im really stuck. Im no php programmer so some things look like abc to me.
    Someone please help me.

  • haantje72
    Avatar
    Community Member
    69 Posts

    Re: How to create an address book in SS? Link to this post

    Anyone? If i can make this work i can finish my site!

  • haantje72
    Avatar
    Community Member
    69 Posts

    Re: How to create an address book in SS? Link to this post

    After days of searching im this far;

    i made in mysite/code the 2 pages Contact.php and ContactAdmin.php as described above. I works.

    Then i made an page ContactPage.php with the following code

    <?php

    class ContactPage extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    }

    class ContactPage_Controller extends Page_Controller {

    }
    ?>

    In the cms i can select a pagetype ContactPage

    Then i made in themes/blackcandy/templates/Layout my ContactPage.ss with the following code

    <div class="typography">
    <% if Menu(2) %>
    <% include SideBar %>
    <div id="Content">
    <% end_if %>

    <% if Level(2) %>
    <% include BreadCrumbs %>
    <% end_if %>
    <h2>$Title</h2>
    $Content
    $Name , $Address
    $Form
    $PageComments
    <% if Menu(2) %>
    </div>
    <% end_if %><div id="Sidebar"><br /><br />$Sidebar</div>
    </div>

    In my template i just see the comma between $Name , $Address and no data is given in the page.

    What am i doing wrong???

    Actually i just want an searchbar in this page users can search for city and postal and then when they hit search the data shows... so just like the admin section but of course without editing functions.
    How can i do this?

    Thanks for response

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How to create an address book in SS? Link to this post

    Hi

    The $Name and $Address are emtpy, because the template parser is looking for this values on the ContactPage class. And they aren't defined there.
    Apparently you have Contact DataObjects you'd like to output, so you have to get them from somewhere. So you need to provide a method to access the contacts on your ContactPage class (add the following to your ContactPage class):

    public function getContacts(){
    return DataObject::get('Contact');
    }

    Now in your template (ContactPage.ss) you should be able to list all contacts like this:

    <% control Contacts %>
    <p>$Name, $Address</p>
    <% end_control %>

  • haantje72
    Avatar
    Community Member
    69 Posts

    Re: How to create an address book in SS? Link to this post

    Yessss, thanks a lot... it works.

    Any suggestion how to include the searchfunction as noted in Contact.php?
    static $searchable_fields = array(
    'Name',
    'Phone'
    );

    or change the normal searchform as described in mysite/page.php
    /**
        * Site search form
        */
       function SearchForm() {
          $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'Search')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }

    thank for reply.

    2529 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.