Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

How to create an address book in SS?


Go to End


18 Posts   4851 Views

Avatar
jimw

Community Member, 13 Posts

20 June 2009 at 2:24am

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

Avatar
Howard

Community Member, 215 Posts

20 June 2009 at 10:55pm

Edited: 21/06/2009 11:49am

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';
 
}

?>

Avatar
jimw

Community Member, 13 Posts

21 June 2009 at 2:08pm

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.

Avatar
haantje72

Community Member, 69 Posts

18 January 2010 at 1:40am

Edited: 18/01/2010 1:43am

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.

Avatar
haantje72

Community Member, 69 Posts

20 January 2010 at 5:30am

Edited: 23/01/2010 10:54pm

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

Avatar
haantje72

Community Member, 69 Posts

23 January 2010 at 10:54pm

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

Avatar
bummzack

Community Member, 904 Posts

23 January 2010 at 11:22pm

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 %>

Avatar
haantje72

Community Member, 69 Posts

23 January 2010 at 11:48pm

Edited: 23/01/2010 11:59pm

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.

Go to Top