Skip to content
Newer
Older
100644 138 lines (129 sloc) 5.07 KB
7e3e387 @gabebw Initial import
authored Jun 29, 2009
1 <?php
2 /**
3 * Library of cast-related functions.
4 * @package FanKit
5 */
6
7 /**
8 * Add a cast member. Note that the character's name is the name of the Wordpress page corresponding to $cast_id.
9 * @param int $cast_id the page id corresponding to the wordpress page that holds the character.
10 * @param array $characters (Optional) An array of characters played by this actor.
11 */
12 function fk_add_cast($cast_id, $characters=array()){
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
38 * @param array $new Specify only the parameters you wish to change. You can use query-string syntax like in get_pages.
39 * @see get_pages()
40 */
41 function fk_edit_cast($cast_id, $new){
42 global $wpdb, $fk_settings;
43 $defaults = array('cast_id' => $cast_id,
44 'characters' => fk_cast_get_characters_for($cast_id)
45 );
46 // http://localhost/wpdocs/WordPress/_wp-includes---functions.php.html#functionwp_parse_args
47 $merged = wp_parse_args($new, $defaults);
48 // Update cast_id first, since it impacts all other queries
49 if( array_key_exists($merged, 'cast_id') && $defaults['cast_id'] !== $merged['cast_id'] ){
50 $new_cast_id = $merged['cast_id'];
51 $new_name = get_post_field('post_title', $cast_id);
52 // Change cast_id, ie point this actor at a different WP post.
53 $wpdb->query($wpdb->prepare('UPDATE '.$fk_settings->cast2character_table.' AS cc, '.$fk_settings->character_table.' AS ch
54 SET cc.cast_id = %1$d, ch.cast_id = %1$d, cc.name = %2$s WHERE cc.cast_id = %3$d AND ch.cast_id = %3$d',
55 $new_cast_id, $new_name, $cast_id));
56 $cast_id = $new_cast_id;
57 unset($merged['cast_id']);
58 }
59
60 foreach( $merged as $field => $value ){
61 if( $defaults[$field] === $value ){
62 // Don't do anything if value hasn't changed.
63 continue;
64 }
65 if( $field === 'characters' ){
66 // Delete characters that are only in $defaults
67 $to_delete = array_diff($defaults['characters'], $value);
68 $to_delete = implode(',', $to_delete);
69 // Add characters that are only in $value
70 $to_add = array_diff($value, $defaults['characters']);
71 $to_add = implode(',', $to_add);
72 // add
73 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast2character_table SET cast_id = %d WHERE cast_id = $cast_id AND character_id IN %s",
74 $value, $to_add));
75 // delete by setting cast to 0 - FIXME
76 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast2character_table SET cast_id = 0 WHERE cast_id = $cast_id AND character_id IN %s",
77 $to_delete));
78 }
79 }
80 }
81
82 /**
83 * Delete a cast member.
84 * @param int $cast_id The post_id of the character to delete.
85 * @param bool $delete_characters (default false) If set to true, deletes characters played by this actor.
86 * TODO
87 */
88 function fk_delete_page_cast($cast_id, $delete_characters = false){
89 global $wpdb, $fk_settings;
90 // todo: have option for deleting characters
91 if( $delete_characters === true ){
92 // TODO: does this work?
93 // Delete all characters from episodes where they were played by this actor (other actors may have played them in other episodes).
94 $wpdb->query($wpdb->prepare("DELETE app FROM $fk_settings->appearance_table AS app
95 INNER JOIN app $fk_settings->cast2character_table AS cc
96 WHERE app.cast_id = %d AND app.character_id = cc.character_id", $cast_id));
97 }
98 }
99
100 /**
101 * Returns array of objects with cast_id and name properties set, ordered alphabetically by first name.
102 */
103 function fk_get_all_cast(){
104 global $wpdb, $fk_settings;
105 $all_cast = $wpdb->get_results($wpdb->prepare("SELECT cast_id, name FROM $fk_settings->cast_table"));
106 return $all_cast;
107 }
108
109 /**
110 * Get all characters played by actor with id $cast_id.
111 * @param int $cast_id
112 * @return array Array of character_id's, or empty array if actor does not play any characters.
113 */
114 function fk_cast_get_characters_for($cast_id){
115 global $wpdb, $fk_settings;
116 $characters = $wpdb->get_col($wpdb->prepare("SELECT character_id FROM $fk_settings->cast2character_table
117 WHERE cast_id = %d", $cast_id));
118 return $characters;
119 }
120
121 // AJAX
122 /**
123 * Returns JSON-encoded array of page ids for episodes.
124 */
125 add_action('wp_ajax_fk_ajax_get_episodes', 'wp_ajax_fk_ajax_get_episodes');
126 function fk_ajax_get_episodes(){
127 if( ! function_exists('json_encode') ){
128 require( WP_CONTENT_URL . plugin_basename(__FILE__) . '/php-json/json.php');
129 function json_encode($obj){
130 $json = new Services_JSON();
131 return $json->encode($obj);
132 }
133 }
134 $episodes = get_pages('meta_key=_fk_type&meta_value=episode');
135 print_r($episodes);
136 }
137 ?>
Something went wrong with that request. Please try again.