Initial commit.
[cf_tmp.git] / functions.php
blob68ffd759cdcf3107a3e5c7832b86769911fae49b
1 <?php
2 /**
3 * WordPress template for the Circular Facts project.
5 * The template arranges the information of the website into thematic
6 * `circuits'. The diagram on the front page is built from this structure.
7 * Most of this file points in that direction.
9 * @package Circular_Facts
12 /** Tell WordPress to run cf_setup() when the 'after_setup_theme' hook is run.
13 * This is copy paste from the twentyten template and stackoverflow
14 * http://wordpress.stackexchange.com/questions/9/how-do-i-add-support-to-my-theme-for-custom-menus
16 add_action("after_setup_theme", "cf_setup");
18 /** This theme uses wp_nav_menu() in several locations:
19 * index.php
20 * page.php
21 * programme.php
22 * single.php
24 * Sets up theme defaults and registers support for various WordPress features.
26 * Note that this function is hooked into the after_setup_theme hook, which runs
27 * before the init hook. The init hook is too late for some features, such as indicating
28 * support post thumbnails.
30 * @uses add_theme_support() To add support for post thumbnails feed links.
31 * @uses register_nav_menus() To add support for navigation menus.
33 function cf_setup() {
34 add_theme_support("automatic-feed-links");
35 register_nav_menus(array("primary" => "Primary Navigation"));
38 /**
39 * Returns a random codepoint from the `work' selection.
41 * It's used by the programme and single views to print a codepoint after the
42 * title.
44 function the_codepoint() {
45 $work_codepoints = array(0x25a2, 0x25a4, 0x25a5, 0x25a6, 0x25a9, 0x25ef, 0x25c9, 0x25b3, 0x25bc, 0x25d0, 0x25d3, 0x25b1);
46 $mycodepoint = array_rand($work_codepoints, 2);
47 printf("<span class=\"codepoint\">&#x%s;</span>", dechex($work_codepoints[$mycodepoint[0]]));
50 /**
51 * This is the basic building block of the front page diagram.
53 * This function prints a `work', connected by a dashed line to a (possibly
54 * empty) list of corresponding `media'.
56 * These `works' are put together by work2unicode() into `circuits'.
58 * In turn, the front page diagram is composed of several `circuits' and is
59 * put together by diagram()
61 * Random codepoints are chosen for the `work' and for each of the `media', if
62 * any.
64 * @param array $media a work's media as produced by the read_data function
66 function media2unicode($media) {
67 $work_codepoints = array(0x25a2, 0x25a4, 0x25a5, 0x25a6, 0x25a9, 0x25ef, 0x25c9, 0x25b3, 0x25bc, 0x25d0, 0x25d3, 0x25b1);
68 $media_codepoints = array(0x259e, 0x2595, 0x2583, 0x258f, 0x259e, 0x258c, 0x259e, 0x259e, 0x2595, 0x258f, 0x258e, 0x259a, 0x258a, 0x258e, 0x258b);
69 $dashed_line = 0x2508;
70 $mycodepoint = array_rand($work_codepoints, 2);
71 printf("<a href=\"%s\" class=\"diagram\">%s", "?p=" . $media[0],
72 "&#x" . dechex($work_codepoints[$mycodepoint[0]]) . ";");
73 if (count($media) != 1) {
74 printf(" %s ", "&#x" . dechex($dashed_line) . ";");
76 for ($i = 1; $i < count($media); $i++) {
77 $medium_codepoint = array_rand($media_codepoints, 2);
78 print "&#x" . dechex($media_codepoints[$medium_codepoint[0]]) . ";";
80 print "</a>";
81 print " ";
84 /**
85 * This is one of two buildings block of the front page diagram.
87 * It is called by the diagram function in conjuction with
88 * circuit2auxiliaries.
90 * Puts together a `circuit' by concatenating a number of `works'.
92 * Prints an arrow between `works'.
94 * Randomizes the `works'.
96 * @param array $works an artist's works as produced by the read_data function
98 function work2unicode($works) {
99 $work_codepoints = array(0x25a2, 0x25a4, 0x25a5, 0x25a6, 0x25a9, 0x25ef, 0x25c9, 0x25b3, 0x25bc, 0x25d0, 0x25d3, 0x25b1);
100 $right_arrow = 0x25b8;
101 for ($i = 0; $i < count($works); $i++) {
102 $mycodepoint = array_rand($work_codepoints, 2);
103 printf("<a href=\"%s\" class=\"diagram\">%s", "?p=" . $works[$i][0],
104 "&#x" . dechex($work_codepoints[$mycodepoint[0]]) . ";");
105 print "</a>";
106 print " ";
107 if ($i != count($works) - 1) {
108 printf("%s ", "&#x" . dechex($right_arrow) . ";");
114 * This is one of two buildings block of the front page diagram.
116 * It is called by the diagram function in conjuction with work2unicode.
118 * Prints the attachments and the external links that have been assigned to
119 * one `circuit'.
121 * There are two loops. One handles the bookmarks. The other handles the
122 * `auxiliaries', ie the attachments of the posts.
124 * @param array $works an artist's works as produced by the read_data function
126 function circuit2auxiliaries($artist) {
127 # any $artist[myindex][0] is ok, as they're all in the same category
128 $my_categories = get_the_category($artist[0][0]);
129 $my_current_category = $my_categories[0]->name;
130 $media_codepoints = array(0x259e, 0x2595, 0x2583, 0x258f, 0x259e, 0x258c, 0x259e, 0x259e, 0x2595, 0x258f, 0x258e, 0x259a, 0x258a, 0x258e, 0x258b);
131 $my_urls = get_bookmarks("category_name=$my_current_category");
132 shuffle($my_urls);
133 $my_media = get_attachments_by_media_tags("media_tags=$my_current_category");
134 if (count($my_urls) != 0 || $my_media) {
135 printf(" %s ", "&#x" . dechex(0x2508) . ";");
137 # loop 1
138 foreach ($my_urls as $my_url) {
139 $mycodepoint = array_rand($media_codepoints, 2);
140 printf("<a href=\"%s\" class=\"diagram\" target=\"_blank\">%s</a>", $my_url->link_url,
141 "&#x" . dechex($media_codepoints[$mycodepoint[0]]) . ";");
143 # loop 2
144 if ($my_media) {
145 shuffle($my_media);
146 foreach ($my_media as $medium) {
147 $mycodepoint = array_rand($media_codepoints, 2);
148 if ($medium->post_mime_type == "application/pdf") {
149 printf("<a href=\"%s\" class=\"diagram\">%s</a>", $medium->guid, "&#x" . dechex($media_codepoints[$mycodepoint[0]]) . ";");
150 } else {
151 printf("<a href=\"%s\" class=\"diagram\" rel=\"lightbox\">%s</a>", $medium->guid, "&#x" . dechex($media_codepoints[$mycodepoint[0]]) . ";");
155 if (count($my_urls) != 0 || $my_media) {
156 print " ";
161 * Puts together the diagram by concatenating a number of `circuits'.
163 * Prints opening/closing brackets at the beginning/end of each circuit.
164 * Prints an x between circuits. It's the first call in a small chain
165 * reaction:
167 * <code>
168 * diagram -> work2unicode
169 * diagram -> circuit2auxiliaries
170 * </code>
172 * These two functions are called one after the other. They're both sent the
173 * complete array of an artist's works as returned by the read_data function.
175 * @param array $artists categories as returned by the read_data function
177 function diagram($artists) {
178 shuffle($artists);
179 for ($i = 0; $i < count($artists); $i++) {
180 $left_bracket = 0x255f;
181 $right_bracket = 0x2562;
182 $x = 0x2573;
183 if ($i != 0) {
184 printf("%s ", "&#x" . dechex($right_bracket) . ";");
185 printf("%s ", "&#x" . dechex($x) . ";");
187 printf("%s ", "&#x" . dechex($left_bracket) . ";");
188 work2unicode($artists[$i]);
189 circuit2auxiliaries($artists[$i]);
191 print "&#x" . dechex($right_bracket) . ";";
195 * Place website data in an array suitable for the Unicode diagram.
197 * It takes in an array as returned by the `get_categories' query. Every
198 * artist is assigned one category. The return value of the function is an
199 * array of arrays that looks like this:
201 * <code>
202 * artist
203 * work
204 * work->ID
205 * </code>
207 * The bottommost array is superflous. It's a leftover from a previous
208 * version. There's no need for an array of id's for a given work.
210 * We used to print the corresponding attachments after every work. Now we
211 * print a `total sum' of attachments at the end of every circuit.
213 * I've no time at the moment to look further into this :(
215 * @param array $categories the website's categories
217 function read_data($categories) {
218 $artist_index = 0;
219 foreach ($categories as $artist_id) {
220 $works = get_posts("category=$artist_id->cat_ID");
221 $works_index = 0;
222 foreach ($works as $work_post) {
223 $work_id = $work_post->ID;
224 $artists[$artist_index][$works_index][] = $work_id;
225 $works_index++;
227 $artist_index++;
229 return $artists;