/[drupal]/contributions/modules/moviedb/mdb_common.inc
ViewVC logotype

Contents of /contributions/modules/moviedb/mdb_common.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon Feb 23 20:51:03 2009 UTC (9 months ago) by ultimatedruid
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.2: +39 -34 lines
File MIME type: text/x-php
no message
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Tells the module what to do.
6 */
7
8
9 /**
10 * Manually create a person
11 *
12 */
13 function moviedb_person_add($name) {
14 global $user;
15 $actnode->uid = $user->uid;
16 $actnode->name = $user->name;
17 $actnode->type = 'mdb_person';
18 $actnode->title = $name;
19 $actnode->status = 1;
20
21 //Make sure the data is ok and save him
22 node_validate($actnode);
23 if (!form_get_errors()) {
24 $actnode = node_submit($actnode);
25 node_save($actnode);
26 return $actnode->nid;
27 }
28 else {
29 return FALSE;
30 }
31 }
32
33 /**
34 * Check if a person with that name exists in the databasse
35 *
36 * @param string $name
37 * @return nid
38 */
39 function moviedb_person_exists($name) {
40
41 $result = db_query_range("SELECT nid FROM {node} WHERE title = '%s'", $name, 0, 1);
42
43 if (db_num_rows($result)) {
44 $result = db_fetch_object($result);
45 return $result->nid;
46 }
47 else {
48 return FALSE;
49 }
50
51 }
52
53 /**
54 * Get the id of a person.
55 *
56 * @param string $name
57 * @return nid
58 */
59 function moviedb_person_get_id($name) {
60 $id = moviedb_person_exists($name);
61 if (!$id) {
62 $id = moviedb_person_add($name);
63 }
64
65 return $id;
66 }
67
68 /**
69 * Process people sent by the form
70 *
71 * @param int $mid the movie id
72 * @param string $string the raw submitted people string
73 * @param string $role the role
74 */
75 function moviedb_people_process($mid, $string, $role) {
76
77 if (!$table = moviedb_person_role_to_table($role)) {
78 return FALSE;
79 }
80
81 //Put the people in a table
82 $people = explode(', ', $string);
83
84 //Loop through the people
85 foreach ($people as $weight => $name) {
86
87 //Trim the name
88 $name = trim($name);
89
90 //If the name is empty, go to next
91 if (!$name) {
92 continue;
93 }
94
95 //Get the id..
96 $person_id = moviedb_person_get_id($name);
97
98 //Something's wrong. don't add him
99 if (!$person_id) {
100 continue;
101 }
102
103 //Add the person to the movie
104 db_query("INSERT INTO {$table} (mid, pid, weight) VALUES (%d, %d, %d)", $mid, $person_id, $weight);
105 }
106 }
107
108 /**
109 * Retrieve people from the database
110 *
111 * @param int $mid The movie id
112 * @param string $role the role of the person
113 * @return array
114 */
115 function moviedb_people_load($mid, $role) {
116
117 if (!$table = moviedb_person_role_to_table($role)) {
118 return FALSE;
119 }
120
121
122 $people = db_query("SELECT p.title, p.nid, j.weight FROM {node} as p INNER JOIN {%s} as j ON p.nid = j.pid WHERE j.mid = %d ORDER BY j.weight ASC", $table, $mid);
123
124 $return = FALSE;
125
126 while ($person = db_fetch_object($people)) {
127 $return['object'][] = $person;
128
129 if ($return['form']) {
130 $return['form'] .= ', '. $person->title;
131 }
132 else {
133 $return['form'] .= $person->title;
134 }
135 }
136
137 return $return;
138 }
139
140
141 /**
142 * Get the movies a person participated in
143 *
144 * @param int $pid
145 * The person's id
146 * @param string $role
147 * The role (director, actor...)
148 *
149 * @return array of movie objects
150 */
151 function moviedb_person_movies($pid, $role) {
152
153 if (!$table = moviedb_person_role_to_table($role)) {
154 return FALSE;
155 }
156
157 $movies = db_query("SELECT n.title AS title, m.releaseyear AS releaseyear, n.nid AS nid FROM {node} as n INNER JOIN {%s} as j ON n.nid = j.mid INNER JOIN {moviedb} as m ON m.nid = n.nid WHERE j.pid = %d ORDER BY m.releaseyear DESC", $table, $pid);
158
159 $return = FALSE;
160
161 while ($movie = db_fetch_object($movies)) {
162 $return[] = $movie;
163 }
164
165 return $return;
166
167 }
168
169 /**
170 * Utility function to sort the filmography
171 *
172 * @param array $a
173 * @param array $b
174 * @return int
175 */
176 function moviedb_person_usort($a, $b) {
177 return count($a) - count($b);
178 }
179
180
181
182
183 /**
184 * Get the table name from the person's role
185 *
186 * @param string $role
187 * actor, director, writer, producer...
188 *
189 * @return string of the table name
190 */
191 function moviedb_person_role_to_table($role) {
192 switch ($role) {
193 case 'actor':
194 case 'actors':
195 $table = 'moviedb_actors';
196 break;
197
198 case 'director':
199 case 'directors':
200 $table = 'moviedb_directors';
201 break;
202
203 case 'writer':
204 case 'writers':
205 $table = 'moviedb_writers';
206 break;
207
208 case 'producer':
209 case 'producers':
210 $table = 'moviedb_producers';
211 break;
212
213 //A wrong role.. exit now!
214 default:
215 $table = FALSE;
216 break;
217 }
218
219 return $table;
220
221 }

  ViewVC Help
Powered by ViewVC 1.1.2