Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 141 lines (131 sloc) 4.87 kb
7e3e3871 »
2009-06-29 Initial import
1 <?php
2 /**
3 * Library of cast-related functions.
4 * @package FanKit
5 */
6
7 /**
9df95517 »
2009-07-04 Changing from pages to posts, since I can use post "templates" with t…
8 * Add a cast member. Note that the character's name is the name of the Wordpress post corresponding to $cast_id.
9 * @param int $cast_id the post id corresponding to the wordpress post that holds the character.
7e3e3871 »
2009-06-29 Initial import
10 * @param array $characters (Optional) An array of characters played by this actor.
11 */
08a37ee7 »
2009-07-01 Changed:
12 function fk_cast_add($cast_id, $characters=array()){
7e3e3871 »
2009-06-29 Initial import
13 // TODO: pseudonym?
14 global $wpdb, $fk_settings;
15 $name = get_post_field('post_title', $cast_id);
16 // Add cast member. Right now very basic, but may expand later.
17 $x = $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->cast_table
18 ( cast_id, name )
19 VALUES ( %d, %s )",
20 $cast_id, $name));
21
22 // Add a new cast-character relation.
23 foreach( (array) $characters as $character_id ){
24 $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->cast2character_table
25 ( cast_id, character_id )
26 VALUES ( %d, %d )",
27 $cast_id, $character_id));
28 }
29 }
30
31 /**
32 * Update a castmember.
33 * Valid keys for $new:
34 * cast_id Assign a new id to the castmember. (This also changes the name.)
35 * characters (array) Set the characters played by this castmember. Adds/subtracts as necessary.
36 * ...that's it so far.
37 * @param int $cast_id
238f34d8 »
2009-07-04 fk-lib-cast.php:
38 * @param array $characters Array of character post IDs that this cast member plays.
7e3e3871 »
2009-06-29 Initial import
39 */
238f34d8 »
2009-07-04 fk-lib-cast.php:
40 function fk_cast_edit($cast_id, $characters){
7e3e3871 »
2009-06-29 Initial import
41 global $wpdb, $fk_settings;
2397e736 »
2009-07-04 fk-box-cast.php
42 $old = array(
238f34d8 »
2009-07-04 fk-lib-cast.php:
43 'name' => get_the_title($cast_id),
7e3e3871 »
2009-06-29 Initial import
44 'characters' => fk_cast_get_characters_for($cast_id)
45 );
238f34d8 »
2009-07-04 fk-lib-cast.php:
46 $new = array(
47 'name' => $_POST['post_title'],
48 'characters' => $characters);
2397e736 »
2009-07-04 fk-box-cast.php
49 $merged = wp_parse_args($new, $old);
7e3e3871 »
2009-06-29 Initial import
50
51 foreach( $merged as $field => $value ){
2397e736 »
2009-07-04 fk-box-cast.php
52 if( $old[$field] === $value ){
7e3e3871 »
2009-06-29 Initial import
53 // Don't do anything if value hasn't changed.
54 continue;
55 }
238f34d8 »
2009-07-04 fk-lib-cast.php:
56 switch($field){
57 case 'characters':
2397e736 »
2009-07-04 fk-box-cast.php
58 // Delete characters that are only in $old
59 $to_delete = array_diff($old['characters'], $value);
7e3e3871 »
2009-06-29 Initial import
60 $to_delete = implode(',', $to_delete);
61 // Add characters that are only in $value
2397e736 »
2009-07-04 fk-box-cast.php
62 $to_add = array_diff($value, $old['characters']);
7e3e3871 »
2009-06-29 Initial import
63 $to_add = implode(',', $to_add);
64 // add
65 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast2character_table SET cast_id = %d WHERE cast_id = $cast_id AND character_id IN %s",
66 $value, $to_add));
67 // delete by setting cast to 0 - FIXME
68 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast2character_table SET cast_id = 0 WHERE cast_id = $cast_id AND character_id IN %s",
69 $to_delete));
238f34d8 »
2009-07-04 fk-lib-cast.php:
70 break;
71 case 'name':
72 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast_table SET name = %s WHERE cast_id = %d",
73 $value, $cast_id));
74 break;
7e3e3871 »
2009-06-29 Initial import
75 }
76 }
77 }
78
79 /**
80 * Delete a cast member.
81 * @param int $cast_id The post_id of the character to delete.
82 * @param bool $delete_characters (default false) If set to true, deletes characters played by this actor.
83 * TODO
84 */
08a37ee7 »
2009-07-01 Changed:
85 function fk_cast_delete($cast_id, $delete_characters = false){
7e3e3871 »
2009-06-29 Initial import
86 global $wpdb, $fk_settings;
87 // todo: have option for deleting characters
88 if( $delete_characters === true ){
89 // TODO: does this work?
90 // Delete all characters from episodes where they were played by this actor (other actors may have played them in other episodes).
91 $wpdb->query($wpdb->prepare("DELETE app FROM $fk_settings->appearance_table AS app
c538930e »
2009-07-04 Updating query so it works (I think)
92 INNER JOIN $fk_settings->cast2character_table AS cc
93 ON app.cast_id = %d AND app.character_id = cc.character_id", $cast_id));
7e3e3871 »
2009-06-29 Initial import
94 }
95 }
96
238f34d8 »
2009-07-04 fk-lib-cast.php:
97 function fk_cast_exists($cast_id){
98 global $wpdb, $fk_settings;
99 $does_exist = (null !== $wpdb->get_row($wpdb->prepare("SELECT cast_id FROM $fk_settings->cast_table WHERE cast_id = %d", $cast_id)) );
100 return $does_exist;
101 }
102
7e3e3871 »
2009-06-29 Initial import
103 /**
104 * Returns array of objects with cast_id and name properties set, ordered alphabetically by first name.
105 */
08a37ee7 »
2009-07-01 Changed:
106 function fk_cast_get_all(){
7e3e3871 »
2009-06-29 Initial import
107 global $wpdb, $fk_settings;
108 $all_cast = $wpdb->get_results($wpdb->prepare("SELECT cast_id, name FROM $fk_settings->cast_table"));
109 return $all_cast;
110 }
111
112 /**
113 * Get all characters played by actor with id $cast_id.
114 * @param int $cast_id
115 * @return array Array of character_id's, or empty array if actor does not play any characters.
116 */
117 function fk_cast_get_characters_for($cast_id){
118 global $wpdb, $fk_settings;
119 $characters = $wpdb->get_col($wpdb->prepare("SELECT character_id FROM $fk_settings->cast2character_table
120 WHERE cast_id = %d", $cast_id));
121 return $characters;
122 }
123
124 // AJAX
125 /**
9df95517 »
2009-07-04 Changing from pages to posts, since I can use post "templates" with t…
126 * Returns JSON-encoded array of post ids for episodes.
7e3e3871 »
2009-06-29 Initial import
127 */
128 add_action('wp_ajax_fk_ajax_get_episodes', 'wp_ajax_fk_ajax_get_episodes');
129 function fk_ajax_get_episodes(){
130 if( ! function_exists('json_encode') ){
7cc32695 »
2009-07-01 fk-lib-cast.php:
131 require_once(ABSPATH.'/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php');
7e3e3871 »
2009-06-29 Initial import
132 function json_encode($obj){
7cc32695 »
2009-07-01 fk-lib-cast.php:
133 $json = new Moxiecode_JSON();
7e3e3871 »
2009-06-29 Initial import
134 return $json->encode($obj);
135 }
136 }
9df95517 »
2009-07-04 Changing from pages to posts, since I can use post "templates" with t…
137 $episodes = get_posts('meta_key=_fk_type&meta_value=episode');
7e3e3871 »
2009-06-29 Initial import
138 print_r($episodes);
139 }
140 ?>
Something went wrong with that request. Please try again.