Categories
General Php SilverStripe

Posting links to Facebook from PHP – not from localhost

While working on an update for one of my SilverStripe modules, I came upon a problem which held me back for some time, changing configurations and doing all kinds of tests.

The thing is – Facebook engine verifies the link you’re trying to post, so the post from localhost fails with “Unknown error”. You need to give Facebook a link it can scrape and extract meta data from it, so for testing purposes use an existing link, and when your test is online try how the regular link would work.

Categories
CMS Php SilverStripe

Silverstripe 3.0 Grid Fields with Thumbnails

Silverstripe 3 has been out for a few months now, and most the big bugs have been fixed… This tutorial describes the code required to set up a grid field to manage ‘Has Many’ relations. Eg – a staff page that lists many staff members.

Also, we’ll add in some code that will make the table list in the CMS display a wee thumbnail of each staff member – making it much easier for CMS users to manage the content.

Create the Staff Page

Create StaffPage.php

This is the page on your site that contains all the staff page. Eg, one ‘staff page’ -> ‘has many’ -> ‘staff members’

Create the Staff Member Object

Create StaffMember.php

This is the object that contains data about each staff member. They belong to a Staff Page

In the code above, we simple create a DataObject, and define two feilds, a name, and a details.

We also attach the DataObject to an image object (allowing us to add an image) and to a page – this forms the connection with the Staff page it belongs to

At this stage, upload the files, run a dev/build and create a ‘staffPage’ in your CMS. You should be able to see a datagrid and start adding staff members and photos.

Displaying Thumbnails in your Grid Field in the CMS

However, the grid field will probably just list your items by their ID – which is a bit meaningless.

By adding the code above into the StaffMember.php file,  we can define the fields (summary_fields) that are shown on the summary table in the CMS.

The function getThumbnail takes the attached Image, and creates a thumbnail to use in the summary fields.

Categories
JavaScript jQuery Php SilverStripe Tutorial

Silverstripe 3 – Per user page access permissions

Most of the times group access in SilverStripe is sufficient for controlling user access, but for this project I had a specific situation where every user needs to have a dedicated page. In order to avoid unnecessary editing and creating groups for each individual user, I decided to extend SiteTree and create a page with per user access control.

So, I’ve created a new page type, since I only needed it for a single page type, but you can extend/decorate SiteTree any way you like.

Let’s get to the code. This goes to mysite/code/FilePage.php

So, let’s break it down a bit:

I’ve created a new variable – CanViewTypeExtended to replace SiteTree’s CanViewType, since I couldn’t find the way to add an option (If you know a way, feel free to drop a line). It replicates SiteTree’s CanViewType with OnlyTheseMembers option added, which is our per user access type.

Then, we have has one ViewerMember, which holds actual user ID for single user (I’ve limited it to one user, since using any more would be a group).

The rest is pretty basic, mostly copied from SiteTree with a bit of additions, getSettingsFields  is a standard function for updating the Settings tab in CMS. There we have first included the JavaScript file, which will be shown later, and is only for decoration – showing and hiding fields based on selection.

Then we have created Member selection field, to pick a member to which the access will be granted, and replicated creating CanViewType field from SiteTree.php with addition of our new OnlyTheseMembers option.

After this is saved, all that is left is to check user permissions in canView() method. Since we don’t use CanViewType any more, but have replaced it with CanViewTypeExtended, the entire function is copied from SiteTree.php, except for the last part which grants the access if current member is our selected member:

if($this->CanViewTypeExtended == ‘OnlyTheseMembers’ && $member && $member->ID == $this->ViewerMemberID)
return true;

So, here’s the remaining js file, which goes to mysite/javascript/CMSMain.EditFormMember.js

 

Categories
Php SilverStripe

Install Tidy on Ubuntu

Not something big or important, but I just tried several ways to get tidy library on my Ubuntu box, and none other then this worked. Might save some searching to others.

sudo apt-get install php5-tidy

Tidy is recommended for running SilverStripe (not required).

php5 comes with Tidy library preinstalled but it has to be compiled –with-tidy which is sometimes skipped (well, on the box I’m setting up for example).

Categories
CMS Php SilverStripe

Silverstripe 3 Grid Field Config Options

The grid field system in Silverstripe 3 has a number of preset config options – here’s what they do, and what they look like:

No Config

If we don’t add any config options, the grid field just displays a list of items…  We can’t view the record details, or edit anything though…

grid1

$gridfield = new GridField(“RegisterEvents”, “RegisterEvent”, $this->RegisterEvents());
$fields->addFieldToTab(‘Root.Events’, $gridfield);

GridFieldConfig_RecordViewer

 

This option just adds in the ability to click the magnifying glass and view the details of each item – but not edit anything.

grid2

GridFieldConfig_RecordEditor

 

Now we’re starting to get something useful – a list that lets us Add, View, and Remove records…

grid3

GridFieldConfig_RelationEditor

The final option is the Relation Edition set up. This adds features to work with ‘has-many’ and ‘many-many’ relationships.

See our article about Many_Many relations for more details

Custom Config

Or, you can just create an empty configuration, and add the bits you need….

 

Categories
CMS Php SilverStripe

Extending SilverStripe 3 Site Settings

When you first install your SilverStripe site, the ‘Settings’ menu is pretty limited, offering just Title, Tagline, and a choice of templates.

This is fine, but it’s often useful to add other options, such as logo upload, links to facebook, uploading a banner image that appears on every page.

MySiteConfig.php

In your ‘mysite/code/’ folder, create a new file called ‘MySiteConfig.php’, and insert the following text:

This file creates a ‘DataExtension’. A DataExtension, as the name suggests, is used to extend any set of data. You declare the additional databse fields and relations just as you would when creating a new page.

And you use the function ‘public function updateCMSFields {}’ to add the fields to the CMS page, just as you would when creating a new page.

_config.php

In your ‘mysite’ folder, open the ‘_config,php’ file, and add the following line:

This line simple tells SilverStripe to take the ‘SiteConfig’ (the basic settings that already exist) and add ‘MySiteConfig’ to it.

Dev/Build

Go to your site, log in, and add ‘/dev/build’ to the URL – eg ‘mysite.com/dev/build’ to refresh the database.

Now, in the admin area, go to the site settings page and you should see your new options.

Using these fields in the Template

You can now add a logo, banner image, and enter the facebook / vimeo links – the data is stored in the database, but we need to show it on our page.

We do this by adding ‘$SiteConfig.’ in front of the names of the fields. Eg:

Categories
Php SilverStripe

SilverStripe 3.0 – Batch Translate

As a common request around the web is to translate the entire Site Tree. Following the post on: https://www.silverstripe.org/customising-the-cms/show/7318 Which was made for SS 2.3.2, I’ve adopted the code to work with SilverStripe 3.0 (Tested on 3.0.5). It gives you the ability to select entire site tree, or just parts of it, and translate it to draft or to published site.

Categories
CMS Php

Some customization of SilverStripe 3.0

I just started with SilverStripe, and here are a few things I stumbled upon while testing the possibilities of this Framework/CMS, hope it will be helpful to someone.
First, some things you can do right in your mysite/_config.php. You can remove most of the admin menu links by using:
CMSMenu::remove_menu_item(‘ReportAdmin’); // Reports link for example
Also, you can define the Admin page title, not to display SilverStripe with:
LeftAndMain::setApplicationName(“MyApplication”);

There are two ways to remove the index.php/ part from the url, but before you do that make sure your mod_rewrite is working. First way is to edit the first line of code in index.php so it says:

define('BASE_SCRIPT_URL',''); // Originally : define('BASE_SCRIPT_URL','index.php/');

The other way is to just completely delete index.php

And for those that don’t like the “Help” link in the admin menu, you have to go a bit deeper and edit /framework/admin/code/LeftAndMain.php.

As it says at line 173:

// can’t be done in cms/_config.php as locale is not set yet

So, you can just comment out the following lines:

/*CMSMenu::add_link(
'Help',
_t('LeftAndMain.HELP', 'Help', 'Menu title'),
self::$help_link
);*/

Happy coding!

Categories
Php

Use Zend Framework 2 from sub folder

I just started testing the Zend Framework 2, and I wanted to give it a quick look around with the Skeleton Application. I didn’t want to add virtual hosts to my local Apache, as I have more important stuff I’m working on, so I just wanted to make a folder for it and run the application.

I’ve read some post about placing it in sub folder, but the real thing was much simpler. What I did was installing the application in https://localhost/zend/, then just copied everything from “public” folder to the sub folder root . After that, it was enough just to comment out the line:

 

It’s just as easy as that, navigate to  https://localhost/zend/, and you should be presented with skeleton application.

Categories
CMS General Php

Conference Republic

Conference Republic web site is online. It’s the first web site running completely on my Complete MeSs system.

The web site design was done by CMYK You.

It’s running on PHP/MySQL base, with jQuery as the JavaScript framework of choice.

You can visit the website at this link.