9099d4452090bbc9c598383eea0b1b82f8a0381e
[kohadocs.git] / source / extending_koha.rst
1 .. include:: images.rst
2
3 Extending Koha
4 ==================
5
6 This chapter shows how you can add various enhancements and customizations
7 in and around Koha by using mostly the existing configuration options.
8
9 .. _amazon-lookup-script-for-koha-libraries-label:
10
11 Amazon lookup script for Koha libraries
12 --------------------------------------------------------
13
14 We order most of our materials from Amazon, so I've been looking for a
15 convenient way to tell if a book under consideration is in our catalog
16 already.
17
18 Greasemonkey & a custom user script fit the bill nicely:
19
20 -  https://addons.mozilla.org/en-US/firefox/addon/748
21
22 -  http://userscripts.org/scripts/show/56847
23
24 A few caveats:
25
26 -  Like most scripts, this one was designed to work with Firefox; I
27    haven't explored getting it to work with other browsers.
28
29 -  I'm not a JavaScript programmer -- this was adapted from others'
30    work. Just a few lines would have to be changed to get the script to
31    work with your catalog.
32
33 -  It depends on the existence of ISBN for the item in question, so
34    movies, older books, etc. would not work.
35
36 Others have added all sorts of bells & whistles: XISBN lookups to search
37 for related titles, custom messages based on the status of items (on
38 order, on hold, etc.), ... just search the UserScripts site for Amazon +
39 library. For a later date!
40
41 .. _keyword-clouds-label:
42
43 Keyword Clouds
44 -----------------------------------------
45
46 In addition to the traditional tag cloud available in Koha, there is a
47 way to generate clouds for popular subjects within Koha.
48
49 The :ref:`Author/Subject Cloud cron job <subject/author-clouds-label>` is used to help
50 with this process. This cron job sends its output to files.
51
52 -  /home/koha/mylibrary/koharoot/koha-tmpl/cloud-author.html
53
54 -  /home/koha/yourlibrary/koharoot/koha-tmpl/cloud-subject.html
55
56 This means that you can produce clouds for authors, collective author,
57 all kind of subjects, classifications, etc. And since it works on zebra
58 indexes, it is quick, even on large DBs. Tags clouds are sent to files.
59 It's up to library webmaster to deal with those files in order to
60 include them in :ref:`OPACMainUserBlock <opacmainuserblock-label>`, or include
61 them into their library CMS.
62
63 Some libraries even send the file into a Samba shared folder where
64 webmaster take them, eventually clean them a little bit before
65 integrating them into navigation widgets or pages.
66
67 .. _newest-titles-pulldown-label:
68
69 Newest Titles Pulldown
70 ---------------------------------------------
71
72 Often we want to add a way for our patrons to do searches for the newest
73 items. In this example I'll show you how to create a pull down menu of
74 the newest items by item type. These tips will work (with a couple
75 changes) for collection codes or shelving locations as well.
76
77 First, it's important to note that every link in Koha is a permanent
78 link. This means if I do a search for everything of a specific item type
79 sorted by the acquisitions date and bookmark that URL, whenever I click
80 it I'll see the newest items of that type on the first few pages of the
81 results.
82
83 I took this knowledge and wrote a form takes this functionality in to
84 consideration. It basically just does a search of your Koha catalog for
85 a specific item type and sorts the results by acquisitions date.
86
87 The first thing I did was write a MySQL statement to generate a list of
88 item types for me - why copy and paste when you can use the power of
89 MySQL?
90
91 ::
92
93     select concat('<option value=\"mc-itype:', itemtype, '\">',description,'</option>') from itemtypes
94
95 The above looks at the itemtypes table and slaps the necessary HTML
96 around each item type for me. I then exported that to CSV and opened it
97 in my text editor and added the other parts of the form.
98
99 ::
100
101     <p><strong>New Items</strong></p>
102     <p><form name="searchform" method="get" action="/cgi-bin/koha/opac-search.pl">
103     <input name="idx" value="kw" type="hidden">
104     <input name="sort_by" value="acqdate_dsc" type="hidden">
105     <input name="do" value="OK" type="hidden">
106     <select name="limit" onchange="this.form.submit()">
107     <option>-- Please choose --</option>
108     <option value="mc-itype:BOOK">Book</option>
109     <option value="mc-itype:BOOKCD">Book on CD</option>
110     <option value="mc-itype:DVD">DVD</option>
111     <option value="mc-itype:LRG_PRINT">Large print book</option>
112     <option value="mc-itype:MAGAZINE">Magazine</option>
113     <option value="mc-itype:NEWSPAPER">Newspaper</option>
114     <option value="mc-itype:VIDEO">Videocassette</option>
115     </select>
116     </form>
117     </p>
118
119 Now, what does all of that mean? The important bits are these:
120
121 First the starting of the form.
122
123 ::
124
125     <p><form name="searchform" method="get" action="/cgi-bin/koha/opac-search.pl">
126
127 This tells the browser to take any value selected and put it at the end
128 of this http://YOURSITE/cgi-bin/koha/opac-search.pl. If you want to
129 embed this form on your library website (and not on your OPAC) you can
130 put the full OPAC URL in there.
131
132 Next, there is a hidden value that is telling the search to sort by
133 acquisitions date descending (newest items at the top):
134
135 ::
136
137     <input name="sort_by" value="acqdate_dsc" type="hidden">
138
139 And finally you have an option for each item type you want people to
140 search.
141
142 ::
143
144     <option value="mc-itype:BOOK">Book</option>
145
146 These options each include the string "mc-itype:" which tells Koha to do
147 an item type search.
148
149 Once you have all of that in place you can copy and paste the form to
150 somewhere on your OPAC. The `Farmington Public Libraries
151 OPAC <http://catalog.farmingtonlibraries.org>`__ has a few examples of
152 this on the left.
153
154 .. _new-titles-slider-for-opac-label:
155
156 New titles slider for OPAC
157 --------------------------
158
159 Often times libraries will want to add a flowing widget with new
160 materials at the library to their main public catalog page. To do this
161 you can use a widget from any number of services (usually for a cost) or
162 you can `enable plugins <#pluginsystem>`__ in Koha and use the `Cover
163 Flow
164 plugin <http://git.bywatersolutions.com/koha-plugins.git/shortlog/refs/heads/cover_flow>`__
165 which is based on the
166 `Flipster <https://github.com/drien/jquery-flipster>`__, a responsive
167 jQuery coverflow plugin.
168
169 Once the plugin is installed, the steps to get your coverflow to show up
170 are as follows:
171
172 First, you need to create one or more public reports for your coverflow
173 widget or widgets to be based on. This is how the plugin knows what the
174 content of your widget should contain. Each report needs only three
175 columns; title, biblionumber, and isbn. It is important that you have a
176 good and valid isbn, as that is the datum used to actually fetch the
177 cover. In the iteration of the plugin, we are using Amazon cover images,
178 but I believe in the end I will make the cover image fetcher
179 configurable so we can use any data source for cover image fetching.
180
181 Second, we need to configure the plugin. The plugin configuration is a
182 single text area that uses YAML ( actually, it’s JSON, whcih is a subset
183 of YAML ) to store the configuration options. In this example it looks
184 like this:
185
186 ::
187
188     - id: 42
189       selector: #coverflow
190       options:
191       style: coverflow
192
193 In this example, we are telling the plugin to use the report with id 42,
194 and use it to create a coverflow widget to replace the HTML element with
195 the id “coverflow”. The options list is passed directly to Flipster, so
196 any options supported by Flipster can be set from the plugin
197 configuration! In fact, in addition to the traditional coverflow,
198 Flipster has a “carousel” mode which is a much more compact version of
199 the coverflow. You can also configure which cover the widget will start
200 on, among other options.
201
202 At the time the plugins options are saved or updated, the plugin will
203 then generate some minified JavaScript code that is automatically stored
204 in the Koha system preference OPACUserJS. Here is an example of the
205 output:
206
207 ::
208
209     /* JS for Koha CoverFlow Plugin
210      This JS was added automatically by installing the CoverFlow plugin
211      Please do not modify */$(document).ready(function(){$.getScript("/plugin/Koha/Plugin/Com/ByWaterSolutions/CoverFlow/jquery-flipster/src/js/jquery.flipster.min.js",function(data,textStatus,jqxhr){$("head").append("<link id='flipster-css' href='/plugin/Koha/Plugin/Com/ByWaterSolutions/CoverFlow/jquery-flipster/src/css/jquery.flipster.min.css' type='text/css' rel='stylesheet' />");$('#coverflow').load("/coverflow.pl?id=42",function(){var opt={'items':'.item','minfactor':15,'distribution':1.5,'scalethreshold':0,'staticbelowthreshold':false,'titleclass':'itemTitle','selectedclass':'selectedItem','scrollactive':true,'step':{'limit':4,'width':10,'scale':true}};$('#coverflow').flipster({style:'coverflow',});});});});
212     /* End of JS for Koha CoverFlow Plugin */
213
214 Why do this? For speed! Rather than regenerating this code each and
215 every time the page loads, we can generate it once, and use it over and
216 over again.
217
218 If you inspect the code closely, you’ll notice it references a script
219 “coverflow.pl”. This is a script that is included with the coverflow
220 plugin. Since we need to access this from the OPAC ( and we don’t want
221 to set off any XSS attack alarms ), we need to modify the web server
222 configuration for the public catalog and add the followup to it:
223
224 ::
225
226     ScriptAlias /coverflow.pl "/var/lib/koha/mykoha/plugins/Koha/Plugin/Com/ByWaterSolutions/CoverFlow/coverflow.pl"
227
228 This line gives us access to the coverflow.pl script from the OPAC. This
229 script retrieves the report data and passes it back to the public
230 catalog for creating the coverflow widget. Koha::Cache is supported in
231 order to make the widget load as quickly as possible!
232
233 The final step is to put your selector element somewhere in your public
234 catalog. In this example, I put the following in the system preference
235 OpacMainUserBlock:
236
237 ::
238
239     <span id="coverflow">Loading...</span>
240
241 Once that is in place, you need only refresh your OPAC page, and there
242 you have it, your very own catalog coverflow widget! Not only do these
243 coverflows look great on a computer screen, but they look great on
244 mobile platforms as well, and are even touch responsive!
245
246 |image1316|
247
248 .. _cataloging-and-searching-by-color-label:
249
250 Cataloging and Searching by Color
251 ----------------------------------------------------
252
253 One of the icon sets installed in Koha includes a series of colors. This
254 set can be used to catalog and search by color if you'd like. This guide
255 will walk you use changing collection code to color in Koha so that you
256 can do this.
257
258 The following SQL could be used to add these colors to the CCODE
259 authorized value category in a batch. If you wanted to use these colors
260 for another authorized value you'd have to edit this to use that
261 category:
262
263 ::
264
265         insert into authorised_values (category, authorised_value, lib,
266          lib_opac, imageurl) values ('CCODE','000000','Black','','colors/000000.png');
267         insert into authorised_values (category, authorised_value, lib,
268          lib_opac, imageurl) values ('CCODE','66cc66','Bright Green','','colors/66cc66.png');
269         insert into authorised_values (category, authorised_value, lib,
270          lib_opac, imageurl) values ('CCODE','99cc33','Olive','','colors/99cc33.png');
271         insert into authorised_values (category, authorised_value, lib,
272          lib_opac, imageurl) values ('CCODE','3333cc','Dark Blue','','colors/3333cc.png');
273         insert into authorised_values (category, authorised_value, lib,
274          lib_opac, imageurl) values ('CCODE','006600' ,'Dark Green','','colors/006600.png');
275         insert into authorised_values (category, authorised_value, lib,
276          lib_opac, imageurl) values ('CCODE','6600cc','Plum','','colors/6600cc.png');
277         insert into authorised_values (category, authorised_value, lib,
278          lib_opac, imageurl) values ('CCODE','6666cc','Purple','','colors/6666cc.png');
279         insert into authorised_values (category, authorised_value, lib,
280          lib_opac, imageurl) values ('CCODE','9999cc','Light Purple','','colors/9999cc.png');
281         insert into authorised_values (category, authorised_value, lib,
282          lib_opac, imageurl) values ('CCODE','660000' ,'Burgundy','','colors/660000.png');
283         insert into authorised_values (category, authorised_value, lib,
284          lib_opac, imageurl) values ('CCODE','663333' ,'Brown','','colors/663333.png');
285         insert into authorised_values (category, authorised_value, lib,
286          lib_opac, imageurl) values ('CCODE','990000' ,'Red','','colors/990000.png');
287         insert into authorised_values (category, authorised_value, lib,
288          lib_opac, imageurl) values ('CCODE','996633' ,'Tan','','colors/996633.png');
289         insert into authorised_values (category, authorised_value, lib,
290          lib_opac, imageurl) values ('CCODE','999999' ,'Gray','','colors/999999.png');
291         insert into authorised_values (category, authorised_value, lib,
292          lib_opac, imageurl) values ('CCODE','cc66cc','Pink','','colors/cc66cc.png');
293         insert into authorised_values (category, authorised_value, lib,
294          lib_opac, imageurl) values ('CCODE','cc99cc','Bubble Gum','','colors/cc99cc.png');
295         insert into authorised_values (category, authorised_value, lib,
296          lib_opac, imageurl) values ('CCODE','cc3333' ,'Orange','','colors/cc3333.png');
297         insert into authorised_values (category, authorised_value, lib,
298          lib_opac, imageurl) values ('CCODE','cc6666' ,'Peach','','colors/cc6666.png');
299         insert into authorised_values (category, authorised_value, lib,
300          lib_opac, imageurl) values ('CCODE','cc9999' ,'Rose','','colors/cc9999.png');
301         insert into authorised_values (category, authorised_value, lib,
302          lib_opac, imageurl) values ('CCODE','cccc00' ,'Gold','','colors/cccc00.png');
303         insert into authorised_values (category, authorised_value, lib,
304          lib_opac, imageurl) values ('CCODE','ffff33' ,'Yellow','','colors/ffff33.png');
305         insert into authorised_values (category, authorised_value, lib,
306          lib_opac, imageurl) values ('CCODE','ffffcc','Cream','','colors/ffffcc.png');
307         insert into authorised_values (category, authorised_value, lib,
308          lib_opac, imageurl) values ('CCODE','ffffff','White','','colors/ffffff.png');
309
310 If you would like to choose the colors manually you can do that via the
311 :ref:`Authorized Values` administration area.
312
313 |image1118|
314
315 Next you'll want to :ref:`update the frameworks <marc-bibliographic-frameworks-label>` so
316 that the 952$8 (if you're using collection code) label to says Color.
317
318 Once you have that in place you can start to catalog items by color.
319
320 Finally you'll want to add the following JQuery to your preferences so
321 that it will relabel 'Collection' to 'Color'
322
323 :ref:`IntranetUserJS`
324
325 ::
326
327     $(document).ready(function(){
328         $("#advsearch-tab-ccode a:contains('Collection')").text("Color");
329         $("#holdings th:contains('Collection')").text("Color");
330         });
331
332 :ref:`OPACUserJS`
333
334 ::
335
336     $(document).ready(function(){
337         $("#advsearch-tab-ccode a:contains('Collection')").text("Color");
338         $('#item_ccode').text("Color");
339         });
340
341
342 .. _koha-search-on-your-site-label:
343
344 Using Koha as a Content Management System (CMS)
345 -----------------------------------------------
346
347 .. _setup-label:
348
349 Setup
350 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351
352 These are instructions for taking a default install of Koha and allowing
353 it to function as a little content management system. This will allow a
354 library to publish an arbitrary number of pages based on a template.
355 This example uses the template for the main opac page, but you could
356 just as well use any template you wish with a bit more editing. This may
357 be appropriate if you have a small library, want to allow librarians to
358 easily add pages, and do not want to support a complete CMS.
359
360 -  Copy /usr/share/koha/opac/cgi-bin/opac/opac-main.pl to
361    /usr/share/koha/opac/cgi-bin/opac/pages.pl (in the same directory)
362
363 -  Edit pages.pl in an editor
364
365 -  At approximately line 33 change this code:
366
367    ::
368
369        template_name  => "opac-main.tt",
370
371 -  To this code:
372
373    ::
374
375        template_name  => "pages.tt",
376
377 -  At approximately line 62 after this code:
378
379    ::
380
381        $template->param(
382                   koha_news       => $all_koha_news,
383                   koha_news_count => $koha_news_count,
384                   display_daily_quote => C4::Context->preference('QuoteOfTheDay'),
385                   daily_quote         => $quote,
386                   );
387
388 -  Add these lines:
389
390    ::
391
392                my $page = "page_" . $input->param('p');          # go for "p" value in URL and do the concatenation
393                my $preference = C4::Context->preference($page);  # Go for preference
394                $template->{VARS}->{'page_test'} = $preference;   # pass variable to template pages.tt
395
396 -  Note pages.pl file must have Webserver user execution permissions,
397    you can use `chmod <http://en.wikipedia.org/wiki/Chmod>`__ command if
398    you are actually logged in as such user:
399
400    ::
401
402                $chmod 755 pages.pl
403
404 -  In the browser go to Home > Administration > System Preferences >
405    Local Use and add a New Preference called "page\_test"
406
407 -  Fill it out as so
408
409    -  Explanation: test page for pages tiny cms
410
411    -  Variable: page\_test
412
413    -  Value: Lorem ipsum
414
415    -  Click the TextArea link (or enter "TextArea" into the input field
416       below it)
417
418    -  variable options (last field): 80\|50
419
420 -  In a browser go to http://youraddress/cgi-bin/koha/pages.pl?p=test
421    The page should come up with the words "Lorem ipsum" in the main
422    content area of the page. (replace "youraddress" with localhost,
423    127.0.0.1, or your domain name depending on how you have Apache set
424    up.)
425
426 -  To add more pages simply create a system preference where the title
427    begins with "page\_" followed by any arbitrary letters. You can add
428    any markup you want as the value of the field. Reference the new page
429    by changing the value of the "p" parameter in the URL.
430
431 To learn more visit the Koha wiki page on this topic:
432 http://wiki.koha-community.org/wiki/Koha_as_a_CMS
433
434 .. _editing-the-pages-template-label:
435
436 Editing the pages template
437 ^^^^^^^^^^^^^^^^^^^^^^^^^^
438
439 -  Copy
440    /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/modules/opac-main.tt
441    to
442    /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/modules/pages.tt
443
444 -  Edit
445    /usr/share/koha/opac/htdocs/opac-tmpl/bootstrap/en/modules/pages.tt
446
447 -  At approximately line 61, change this:
448
449    ::
450
451                [% IF ( OpacMainUserBlock ) %]<div id="opacmainuserblock">[% OpacMainUserBlock %]</div>[% END %]
452
453 -  To this:
454
455    ::
456
457                [% IF ( page_test ) %]<div id="opacmainuserblock">[% page_test %]</div>[% END %]
458
459 Remark: You may wish to disable your News block of these CMS style pages
460 e.g. when you do not want it displayed on the CMS style pages or where
461 the News block is long enough that it actually makes the 'page\_test'
462 include scroll outside the default viewport dimensions. In that case,
463 remove the following code from your pages.tt template.
464
465 ::
466
467                       [% IF ( koha_news_count ) %]
468                           <div id="news">
469                           <table class="table table-bordered">
470                           [% FOREACH koha_new IN koha_news %]
471                             <thead><tr><th>[% koha_new.title %]</th></tr></thead>
472                             <tbody><tr><td><p>[% koha_new.new %]</p>
473                             <p class="newsfooter"><i>(published on [% koha_new.newdate %])</i></p></td></tr></tbody>
474                           [% END %]
475                           </table>
476                           </div>
477                          [% END %]
478
479
480 .. _troubleshooting-label:
481
482 Troubleshooting
483 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
484
485 If you have problems check file permissions on pages.pl and pages.tt.
486 They should have the same user and group as other Koha files like
487 opac-main.pl.
488
489 .. _bonus-points-label:
490
491 Bonus Points
492 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
493
494 Instead of using the address
495 http://youraddress/cgi-bin/koha/pages.pl?p=test you can shorten it to
496 http://youraddress/pages.pl?p=test Just open up
497 /etc/koha/koha-httpd.conf and add the follow at about line 13:
498
499 ::
500
501     ScriptAlias /pages.pl "/usr/share/koha/opac/cgi-bin/opac/pages.pl"
502
503 Then restart Apache.
504
505 .. _usage-label:
506
507 Usage
508 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
509
510 After setting up Koha as a CMS you can create new pages following these
511 instructions:
512
513 .. _adding-pages-label:
514
515 Adding Pages
516 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
517
518 To add a new page you need to add a system preference under Local Use.
519
520 -  Get there: More > Administration > Global System Preferences > Local
521    Use
522
523 -  Click 'New Preference'
524
525 -  Enter in a description in the Explanation field
526
527 -  Enter a value that starts with 'page\_' in the Variable field
528
529 -  Enter starting HTML in the Value field
530
531    |image1076|
532
533 -  Set the Variable Type to Textarea
534
535 -  Set the Variable options to something like 20\|20 for 20 rows and 20
536    columns
537
538    |image1077|
539
540 .. _viewing-your-page-label:
541
542 Viewing your page
543 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
544
545 You can view your new page at
546 http://YOUR-OPAC/cgi-bin/koha/pages.pl?p=PAGENAME where PAGENAME is the
547 part you entered after 'page\_' in the Variable field.
548
549 .. _example-label:
550
551 **Example**
552
553 This process can be used to create recommended reading lists within
554 Koha. So once the code changes have been made per the instructions on
555 'Koha as a CMS' you go through the 'Adding a New Page' instructions
556 above to great a page for 'Recommended Reading Lists'
557
558 |image1078|
559
560 Next we need to create pages for our various classes (or categories). To
561 do this, return to the 'Adding a New Page' section and create a
562 preference for the first class.
563
564 |image1079|
565
566 Next you'll want to link your first page to your new second page, go to
567 the page\_recommend preference and click 'Edit.' Now you want to edit
568 the HTML to include a link to your newest page:
569
570 |image1080|
571
572 .. _live-examples-label:
573
574 **Live Examples**
575
576 -  The Crawford Library at Dallas Christian College is using this method
577    for their recommended reading lists: http://opac.dallas.edu/
578
579 Koha search on your site
580 -------------------------------------------
581
582 Often you'll want to add a Koha search box to your library website. To
583 do so, just copy and paste the following code in to your library website
584 and update the YOURCATALOG bit with your catalog's URL and you're set to
585 go.
586
587 ::
588
589     <form name="searchform" method="get" action="http://YOURCATLOG/cgi-bin/koha/opac-search.pl" id="searchform">
590     <input id="transl1" name="q" type="text"><p>
591     <select name="idx" id="masthead_search">
592     <option value="kw">Keyword</option>
593     <option value="ti">Title</option>
594     <option value="au">Author</option>
595     <option value="su">Subject</option>
596     <option value="nb">ISBN</option>
597     <option value="se">Series</option>
598     <option value="callnum">Call Number</option>
599     </select>
600     <input value="Search" id="searchsubmit" type="submit">
601     </p></form>