Candy editor
Programmer's guide
- This guide was last edited on the 11th July 2011
- Copyright Peter Fox @
Purpose of Candy editor
This scheme is useful for where occasional alterations are required to the details of an existing
web page or two. This is not a CMS or blog. A typical application is to allow a trader to
tweak their offers, opening hours and so on. Another application is for a club that just wants
a simple web page with what's on, who's-who and the odd photo of events and people. CE is
ideal for maintaining list of events.
Requirements
Note Replacable images and uploaded reference documents are always in the same
directory as the page being edited.
All you need is a server that runs PHP and of course the ability to edit files and FTP them.
Some extra functionality will be lost if your server can't send emails.
Installing
This guide is local. The rest gets FTPd to the 'home' page of the server.
Pretty much
EVERYTHING goes in the home directory.
That is the same directory as the page(s) being edited.
Logs will go in ../info
Backups are pushed onto a stack of sub-directories of ../safety: prev1 to prevn
The download consists of a zipped collection of files which make up a demonstration system.
The simplest way to get started is to put all the files except the guide on the server.
"Home" directory is the one containing the page to edit.
Path | File | Notes |
home | candyedit.php | The PHP file which does everything |
home | candyedit.js | Javascript used by client |
home | candyedit.css | Styling for editing |
home | jquery.js | jQuery javascript framework. The exact name may vary. See Configuring |
home | essential images | candy*.gif/png |
home | demo images | club*.jpg/gif |
home | clubIndex.htm | Start here to prove system is working |
home | clubDemo.htm | A sample page to get started with |
home/info | .htaccess | Protects against the passwords and logs being viewed |
home/info | pageInfo.txt | Tiny page by page configuration file containing access passwords |
home/safety | - | Backups will go here |
Try the demo
IE compatibility issueIn Internet Explorer press F12 (Developer tools) then select
Document mode to be standards. If the display changes then edit your source to put the HTML 5 doctype
in. ie. <!DOCTYPE html> to zap the 'Microsoft knows best' quirks mode.
At this point you should have a working demonstration system.
Point your browser at your server/clubIndex.htm.
If the page being edited is http://server.com/foo.htm then the way to edit that page is
http://server.com/candyedit.php?page=foo.htm. When we're editing the page there
may be slight layout alterations but the idea is to keep the layout the same and simply highlight editable
bits with candy stripes.
Configuring
There are two configuration tasks:
-
This isn't a super-secure way of storing passwords
but it should be invisible to browsers. Test by pointing your browser at it.
The .htaccess file is supposed to block browsers. If not then consult
your server's documentation.
Edit ../info/pageInfo.txt on a page-by-page basis.
- Instructions are contained in the file.
- Each line refers to one page. In fact this file will often only have one
active line something like index.htm pw=supersecret editlink=YES
- IMPORTANT everything is case sensitive
- pw= sets the password. (Required)
- If editlink=YES is present then a grey square will be added to the page
(as in the demo) which links to the edit page.
- Edit candyedit.php
All the configuration parameters are at the top of the code. These are documented in the code.
Particular ones to note are:
JQUERY_PATH | This is the jQuery javascript library.
As supplied this points to a valid file.
You are free to use a later version or different location.
More details here.
|
INTERNAL _PASSWORD |
You may find that the .htaccess file doesn't protect
pageInfo.txt which contains the password(s). If so
you can delete pageInfo.txt and provide a password here.
Note that this password applies to all the pages in this directory. In practice
this shouldn't be much of a problem as most CE applications will be a small
handful of pages edited by the same person.
|
MAX_UPLOAD_SIZE_MB MAX_IMG_SIZE_MB |
These are sanity checks on stupidly large files being uploaded.
Think what is reasonable. Remember that users will try to upload
5MB photos for thumbmails etc.
IMPORTANT PHP will have it's internal upload limits which may be
as little as 4Mb. This is set in php.ini. See
the discussion in the code for how to deal with this if it is a problem.
|
JQ_DOC_READY |
Ignore this if you don't use jQuery
When hacking a page for editing CE needs to know if jQuery is already being used
by the page. If this string is found in the original then that's the signal to
just add in our code not build the whole thing from scratch. There are a number of
ways to implement the document.ready event hook. You may
have to tweak this search string to exactly match you version.
|
CHANGE_NOTIFY_EMAIL |
Where to send an alert to say there is some editing going on.
Leave blank to disable.
Multiple addresses can be separated by semi-colon. eg.
secretary@users.org;support@foo.com
This feature may be useful for checking that users haven't made a complete hash of things.
Not all servers can send email or hosts block it.
|
EMAIL_CHANGE_FROM |
If you are using the notification of changes email feature then you'll need to
provide a From: address. Be aware that your server or host may have
strict rules about outgoing email so if it doesn't work you should check with them.
|
VERSION_DELAY |
This is set to 3 minutes in the demo system so you can see the backing up
working. This is far too short for a real system.
CE makes a backup each time editing is started more than a certain time after
the previous starting of an edit. For example if this is set to 2 a user
will be able to fiddle around making a number of attempts without second
and third edits causing backups to be shifted onto and down the stack.
On the other hand if they did some edits in the morning then came back for
more edits in the afternoon (providing 2 hours had elapsed) there would be
another backup made.
If you are using the 'email on editing activity' feature this gets triggered at
this point. (So remember if you get a notification you may want to wait a while
before looking at the changes the user has made.)
|
VERSION_DEPTH |
How many previous versions are to be kept on the backup chain.
|
EDIT_LINK_STYLE |
If you are using the editlink=YES setting in pageInfo.txt
then this is the css that will be used to style the <A> tag that does the trick.
As supplied it appears as a 3-pixel grey square.
|
| |
Checklist
Image upload size adjusted
Password set
Version delay=2
Original page backed-up
Making bits editable
All you do is add class=ed to span, div, h1, h2,
h3, h4, li, tr, img and a tags.
General notes
Image tags
- All editable images are uploaded to the same directory as the page being edited.
(This makes backing up and restoring a great deal simpler.)
You only have to have editable images in the 'home' directory. In fact (see below)
it may be a good idea to have your fixed images in a sub-directory.
- What could possibly go wrong?
- How about you haven't specified heightheight
or width and the user uploads a huge photo?
Unfortunatly you can't specify both max-height
and max-width in CSS or HTML without unpleasant stretching effects.
- How about you have a fixed image called say new.jpg and the user uploads
a different file that happens to have the same name for something else? To be on the
safe side you can put your fixed images in a sub-directory.
Only the editable bits will get backed-up to the version stack. This is fine.
- To protect against stupidity you can only replace images with the same extension as the original.
(.jpeg and .jpg are considered the same.)
A tags
The allowed file extensions are set in candyedit.php
The use of the a tag should be restricted to reference material such as for example PDF.
Editing these links is a way for this type of content to be uploaded.
- Sometimes you want a single link to be restricted to that content. For example
there will only ever be one copy of the club's rules but it may get updated from
time to time. This is the default case.
Or you may want to allow the user to keep adding new documents then do something like this:
<h3>Minutes of meetings</h3>
<div class=ed>March 2011 <a href="minutesMar11.pdf">PDF</a></div>
Or in a list
<li class=ed>March minutes<a href="minutesMar11.pdf">PDF</a>
Or in a table
<tr class=ed><td>March minutes</td><td><a href="minutesMar11.pdf">PDF</a></td></tr>
The outer div/li/tr is cloneable as required then the link can be edited.
- What could possibly go wrong?
- The user uploads an old copy of a document. Here's how it happens: They edit document foo.doc
then export to foo.pdf but don't know where it has been filed in their system and they eventially
find an ancient copy and upload that.
- They reuse a repetitive document which needs a new name each time it is edited but don't rename or 'save-as'.
For example MinutesJan2011.doc should be renamed MinutesFeb2011.doc
the next month otherwise they will overwrite last months with this month's instead of adding.
Tables
Hidden elements
Imagine a user deletes all the rows of a table.
Then there is nothing left for them to edit!
To deal with this the
last deletion is not physically implemented.
Instead the element gets the class
edx substituted for
ed.
CE automatically adds .edx {display:none}
to the live document head which hides the marked elements. However
as they still show up when editing they are not lost forever.
As shown in the demo you can deliberately use class=edx for elements that only get used on high days and holidays.
- Rows are treated as a complete unit. Only attach class=ed to the TR tag.
- Cells may contain editable items.
- Typically for a table you'd leave the headings uneditable.
- If the user tries to delete the last remaining row of a table it will be hidden.
Tell users
Keep it simpler than simple
For most people, updating a web page is a chore. They're probably not confident,
haven't got materials and have forgotten what they did last time.
At least at the start give them the bare minimum of editing options. If they get on OK with
that then you can think of adding more flexibility.
Think of the most important updating functions, the ones that they really want and stick to those
for starters.
What users need to know
- Editing URL and password.
- A printout of the normal page with editable bits highlighted.
- A reminder that changes and typing mistakes are published instantly!
Getting started
In practice the biggest difficulty for users is getting
the necessary information. No data = no editing!
Arrange a time when you're available on the phone for them to have a go. Tell them to
collect the necessary checked information first as this is for real.
- You can point them to the menu bar to get started.
- Show them how to edit a div or span and save the page.
- If that goes OK then go onto lists and tables
- Leave image uploading to later.
- Leave A-tags to last.
Common mistake when creating a sequence of documents: Edit the same
document without changing its name. When this is uploaded as next month's minutes it will
overwrite the previous version. Users must give new names to
new documents
Technical details
- Backup and restore
- A backup of the whole home directory (excluding candy editor code) is made to
home/safety/prev1 unless it has already been done recently.
See VERSION_DELAY.
- A chain of earlier backups is maintained with prev1 being
shifted down to prev2 and so on.
See VERSION_DEPTH.
- A user can only look at the previous versions, but you can copy everything from one of the
safety/prevn directories back to the 'home' if necessary.
- Logging
- The log files are simple text files kept in home/info. There is one for each page.
- The format is simple: timestamp, IP address of editor and message.
- Email notification from the server
- Using jQuery
Ignore this section if you don't use jQuery
- CE uses jQuery so there is the possibility of version mis-match when working with your already installed
jQuery library. CE is clever enough to be able to recognise if jQuery is already in use and adapt but there are
some tricky bits you may want to check.
- IMPORTANT CE looks to see if it can find the document.ready() function in
the existing page. This tells it if jQuery is being used. This is a string search so we need to make sure
that CE is looking for EXACTLY the right thing. JQ_DOC_READY is a string set
in the Defines at the top of candyedit.php. It is simplest
and most reliable to cut and paste your version MUST BE ALL ON ONE LINE WITHOUT NEW LINES.
- The jQuery library is fairly stable, so providing you are not using a very old version you can,
just to be tidy, point CE at your existing library by setting JQUERY_PATH
to point at it.
- Linking page to editor
It is an option to get CE to add an insignificant link on the original page.
The default is a tiny grey block at the very top
left of the page. When the mouse hovers over it you'll see cross-hairs.
This is handy if users are not good at bookmarking and keep having to ask
you how to start the editor.
- To switch this on add editlink=YES to the line(s)
relating to the page in question in ../info/pageInfo.txt
- To tweak the appearance and location in the original tweak EDIT_LINK_STYLE
in candyedit.php.
Development
Origins
CE was put together by Peter Fox @ in June 2011.
Yes you can contribute to development
CE is an open source project so you can see and play with the code. Often people have
small but useful contributions that might take only a couple of hours to do and then, this is the
good bit, the improvements are available to other users.
Bugs and improvements
Fix them yourself or explain clearly with screenshots if applicable
what the problem is. @
WISYWIG
Technical people tend to think that more features must be better. NO NO NO. Candy Editor is meant to be
as simple as it can possibly be. That is deliberate. We let the web page builder deal with the design
and leave the user to maintain the content. If they want a blog use blogging software. If they want a CMS
then use that. If they want pretty content then they can either ask the page builder to do a professional
job of re-design or use their word processor to create documents they can easily upload as PDFs etc.
Internationalsation
Internationalising a free open source application like this is a
really cool way to contribute a little bit to better programs for everyone. It doesn't take long but
it makes it accessible to lots more people.
Editing the javascript candyedit.js |
All the strings that need altering are listed at the top of the code .
How you go about making the alterations are up to you but it may be best to copy the strings as a block
and from there search and replace en_ with an appropriate language code such as say de
for German.
|
Editing the PHP candyedit.php |
This is more tricky than the javascript as often literal text is combined with program variables.
All the places that need attention are marked with i18n. Note leave "OK"
alone as that is used as a signal by the javascript on completion of the AJAX call
|