Skip to content
Newer
Older
100644 212 lines (199 sloc) 7.99 KB
7e3e387 @gabebw Initial import
authored
1 <?php
2 /**
3 * Library of character-related functions. Nearly all of them take post_id's as arguments (instead of, say, a name),
4 * and return post_id's as well. There are some obvious exceptions, such as fk_get_character_id().
5 * @package FanKit
6 */
7
8 /**
9 * Add a character.
10 * @param int $character_id The post_id for this character.
11 * @param int $cast_id (optional) The id of the actor/actress who plays this character.
12 * @param array $appearances (optional) An array of episode_id's that the character appeared in.
13 */
35c7374 @gabebw fk-lib-episode.php
authored
14 function fk_character_add($character_id, $cast_id = null, $appearances=array()){
7e3e387 @gabebw Initial import
authored
15 global $wpdb, $fk_settings;
16
17 if( fk_character_exists($character_id) ){
18 return false;
19 } else {
20 $name = get_post_field('post_title', $character_id);
21 $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->character_table
22 ( character_id, name )
23 VALUES ( %d, %s )",
24 $character_id, $name));
25 }
26 if( is_int($cast_id) ){ // we check because a character can be added with no cast member set for it
27 $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->cast2character_table
28 ( cast_id, character_id )
29 VALUES ( %d, %d )",
30 $cast_id, $character_id));
31 }
32 foreach( (array) $appearances as $episode_id){
33 if( ! is_numeric($episode_id) ){
34 continue;
35 }
36 // todo: is there a way to add all appearances in just one mysql query?
37 // maybe have $episode_id optionally be an array
036d92a @gabebw fk-lib-character.php:
authored
38 fk_character_add_appearance($character_id, $episode_id);
7e3e387 @gabebw Initial import
authored
39 }
40 }
41
42 /**
43 * Delete a character. Deletes appearances, relationships to cast members as well. DOES NOT delete actor who played the character.
44 * @param int $character_id The id of the character to delete.
45 */
08a37ee @gabebw Changed:
authored
46 function fk_character_delete($character_id){
7e3e387 @gabebw Initial import
authored
47 global $wpdb, $fk_settings;
3cb7682 @gabebw fk-lib-episode.php:
authored
48 // Use three queries because it's not guaranteed to to be in all 3, and JOINs get complicated.
49 $wpdb->query($wpdb->prepare("DELETE FROM $fk_settings->character_table WHERE character_id = %d",
50 $character_id));
51 $wpdb->query($wpdb->prepare("DELETE FROM $fk_settings->cast2character_table WHERE character_id = %d",
52 $character_id));
53 $wpdb->query($wpdb->prepare("DELETE FROM $fk_settings->appearance_table WHERE character_id = %d",
54 $character_id));
7e3e387 @gabebw Initial import
authored
55 }
56
57 /**
35c7374 @gabebw fk-lib-episode.php
authored
58 * Edit a character, eg add an episode, change who plays them, etc.
7e3e387 @gabebw Initial import
authored
59 * @param int $character_id
35c7374 @gabebw fk-lib-episode.php
authored
60 * @param string $new_cast The new cast member who plays the characters
61 * @param array $new_appearances Array of episode ID's that the character appears in
7e3e387 @gabebw Initial import
authored
62 */
63544bf @gabebw Instead of $new array, use individual arguments when editing/adding a…
authored
63 function fk_character_edit($character_id, $new_cast, $new_appearances){
7e3e387 @gabebw Initial import
authored
64 global $wpdb, $fk_settings;
63544bf @gabebw Instead of $new array, use individual arguments when editing/adding a…
authored
65 $new = array('cast' => $new_cast,
35c7374 @gabebw fk-lib-episode.php
authored
66 'name' => $_POST['post_title'], // in $_POST but not yet saved
63544bf @gabebw Instead of $new array, use individual arguments when editing/adding a…
authored
67 'appearances' => $new_appearances);
68 $defaults = array('cast' => fk_character_get_actor($character_id),
35c7374 @gabebw fk-lib-episode.php
authored
69 'name' => get_post_field('post_title', $character_id), // previously-saved, not-yet-updated value
63544bf @gabebw Instead of $new array, use individual arguments when editing/adding a…
authored
70 'appearances' => fk_character_get_appearances($character_id));
7e3e387 @gabebw Initial import
authored
71 $merged = wp_parse_args($new, $defaults);
72 foreach( $merged as $field => $value ){
73 if( $defaults[$field] === $value || is_null($value) ){
74 // Don't do anything if value hasn't changed, or if the value is null.
75 continue;
76 }
77 if( $field === 'appearances' ){
78 // Delete episodes that are only in $defaults
79 $to_delete = array_diff($defaults['appearances'], $value);
80 // Add episodes that are only in $value (ie have not yet been added)
81 $to_add = array_diff($value, $defaults['appearances']);
82 foreach( $to_add as $episode_id ){
08a37ee @gabebw Changed:
authored
83 fk_character_add_appearance($character_id, $episode_id);
7e3e387 @gabebw Initial import
authored
84 }
85 foreach( $to_delete as $episode_id ){
63544bf @gabebw Instead of $new array, use individual arguments when editing/adding a…
authored
86 fk_character_delete_appearance($character_id, $episode_id);
7e3e387 @gabebw Initial import
authored
87 }
35c7374 @gabebw fk-lib-episode.php
authored
88 } elseif( $field === 'cast' ){
7e3e387 @gabebw Initial import
authored
89 $cast_id = $value;
90 // Change/Add the cast member who plays this character
91 $update_result = $wpdb->query($wpdb->prepare("UPDATE $fk_settings->cast2character_table
92 SET cast_id = %d WHERE character_id = %d",
93 $cast_id, $character_id));
94 if( 0 === $update_result ){
95 // Did not update anything, so relationship doesn't exist. Create it.
96 $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->cast2character_table
97 ( cast_id, character_id )
98 VALUES ( %d, %d )",
99 $cast_id, $character_id));
100 }
35c7374 @gabebw fk-lib-episode.php
authored
101 } elseif( $field === 'name' ){
102 $wpdb->query($wpdb->prepare("UPDATE $fk_settings->character_table
103 SET name = %s WHERE character_id = %d",
104 $value, $character_id));
7e3e387 @gabebw Initial import
authored
105 }
106 }
107 }
108
109 /* Small utility functions */
110
111 /**
112 * Get list of episodes that character appeared in.
113 * @param int $character_id The character_id corresponding to the character to search the DB for.
b8fea5e @gabebw fk-lib-character.php:
authored
114 * @returns array An array of episode_id's, sorted by season and ep_num. If character does not appear in any episodes, returns empty array.
7e3e387 @gabebw Initial import
authored
115 */
116 function fk_character_get_appearances($character_id){
117 global $wpdb, $fk_settings;
b8fea5e @gabebw fk-lib-character.php:
authored
118 $appearances = $wpdb->get_col($wpdb->prepare("SELECT ep.episode_id FROM $fk_settings->episode_table AS ep INNER JOIN $fk_settings->appearance_table AS app ON ep.episode_id = app.episode_id AND app.character_id = %d ORDER BY ep.season, ep.ep_num ASC",
7e3e387 @gabebw Initial import
authored
119 $character_id));
120 return $appearances;
121 }
122
123 /**
124 * Get the actor who plays the character indicated by the id.
15326bc @gabebw fk-lib-character.php
authored
125 * @param int $character_id The character_id of the character.
7e3e387 @gabebw Initial import
authored
126 * @returns int|bool $id Returns character_id of actor who plays the character, or false if character has no assigned actor.
127 */
128 function fk_character_get_actor($character_id){
129 global $wpdb, $fk_settings;
15326bc @gabebw fk-lib-character.php
authored
130 if( $character_id === 0 ){
131 // Trying to get actor from an unsaved post.
132 return false;
133 }
2ccd3be @gabebw fk-lib-character.php
authored
134 $cast_id = $wpdb->get_var($wpdb->prepare("SELECT cast_id FROM $fk_settings->cast2character_table WHERE character_id =%d",
7e3e387 @gabebw Initial import
authored
135 $character_id));
136 if( is_null($cast_id) ){
137 $cast_id = false;
138 }
139 return $cast_id;
140 }
141
142 /**
143 * Check if $character appeared in an episode.
144 * @param int $character_id The character_id corresponding to character.
145 * @param int $episode_id The episode_id of the given episode.
146 * @param int $ep_num Episode number
147 * @return bool True if character did appear in given episode, false otherwise.
148 */
149 function fk_character_appears_in($character_id, $episode_id){
150 $appearances = fk_character_get_appearances($character_id);
151 $did_appear_in = in_array($episode_id, $appearances);
152 return $did_appear_in;
153 }
154
155 /**
156 * Check if a character exists.
157 * @param int $character_id
158 * @return bool True if character exists, false otherwise.
159 */
160 function fk_character_exists($character_id){
161 global $wpdb, $fk_settings;
162 $var = $wpdb->get_var($wpdb->prepare("SELECT character_id FROM $fk_settings->character_table WHERE character_id = %d",
163 $character_id));
164 $does_exist = ( $var !== null );
165 return $does_exist;
166 }
167
168 /**
169 * Add a character to a specific episode.
170 * @param int $character_id
171 * @param int $episode_id
172 * @return bool False if character already added to episode or episode or character does not exist, true otherwise.
173 */
08a37ee @gabebw Changed:
authored
174 function fk_character_add_appearance($character_id, $episode_id){
7e3e387 @gabebw Initial import
authored
175 global $wpdb, $fk_settings;
176 if( ! ( fk_character_exists($character_id) && fk_episode_exists($episode_id) ) ){
177 return false;
178 }
179 // Check if character is already in that ep
180 if( fk_character_appears_in($character_id, $episode_id) ){
181 return false;
182 }
183 $wpdb->query($wpdb->prepare("INSERT INTO $fk_settings->appearance_table
184 ( character_id, episode_id )
185 VALUES ( %d, %d )",
186 $character_id, $episode_id));
187 return true;
188 }
189
190 /**
191 * Remove a character from an episode.
192 * @return bool False if character did not appear in episode, true otherwise. (I think.)
193 */
1cf72e4 @gabebw fk-lib-character.php:
authored
194 function fk_character_delete_appearance($character_id, $episode_id){
7e3e387 @gabebw Initial import
authored
195 global $wpdb, $fk_settings;
196 $return = $wpdb->query($wpdb->prepare("DELETE FROM $fk_settings->appearance_table
197 WHERE character_id = %d AND episode_id = %d",
198 $character_id, $episode_id));
199 return $return;
200 }
201
202 /**
203 * Get all characters.
9df9551 @gabebw Changing from pages to posts, since I can use post "templates" with t…
authored
204 * @return array Returns an array of objects with character_id and name variables. Returns empty array if no character posts exist.
7e3e387 @gabebw Initial import
authored
205 */
08a37ee @gabebw Changed:
authored
206 function fk_character_get_all(){
7e3e387 @gabebw Initial import
authored
207 global $wpdb, $fk_settings;
208 $characters = $wpdb->get_results($wpdb->prepare("SELECT character_id, name FROM $fk_settings->character_table ORDER BY name ASC"));
209 return $characters;
210 }
211 ?>
Something went wrong with that request. Please try again.