/src/system/application/models/speaker_profile_model.php

https://github.com/rickogden/joind.in · PHP · 282 lines · 165 code · 30 blank · 87 comment · 12 complexity · 5178fc9d5f54a8657933293593f37b2c MD5 · raw file

  1. <?php
  2. /*
  3. * Speaker profile model
  4. */
  5. class Speaker_profile_model extends Model {
  6. function Speaker_profile_model(){
  7. parent::Model();
  8. }
  9. //----------------------
  10. /**
  11. * Check to see if a token belongs to a user
  12. * $uid integer User ID
  13. * $tid integer Token ID
  14. * Return boolean
  15. */
  16. function isUserToken($uid,$tid){
  17. $sql=sprintf("
  18. select
  19. speaker_profile.ID
  20. from
  21. speaker_profile,
  22. speaker_tokens
  23. where
  24. user_id=%s and
  25. speaker_profile_id=speaker_profile.ID and
  26. speaker_tokens.ID=%s
  27. ",$this->db->escape($uid), $this->db->escape($tid));
  28. $q=$this->db->query($sql);
  29. $ret=$q->result();
  30. return (isset($ret[0]->ID)) ? true : false;
  31. }
  32. /**
  33. * Fetch the profile information for the given user ID
  34. */
  35. function getProfile($uid){
  36. $this->db->select('speaker_profile.*, countries.name AS country');
  37. $this->db->from('speaker_profile');
  38. $this->db->join('countries', 'countries.ID=speaker_profile.country_id', 'left');
  39. $this->db->where('user_id', $uid);
  40. $q=$this->db->get();
  41. return $q->result();
  42. }
  43. function getProfileById($pid){
  44. $q=$this->db->get_where('speaker_profile',array('ID'=>$pid));
  45. return $q->result();
  46. }
  47. /**
  48. * Set up a new speaker profile
  49. */
  50. function setProfile($data){
  51. $this->db->insert('speaker_profile',$data);
  52. }
  53. /**
  54. * Given a user ID and key/value, update the user's profile
  55. */
  56. function updateProfile($uid,$data){
  57. $this->db->where('user_id',$uid);
  58. $this->db->update('speaker_profile',$data);
  59. }
  60. /**
  61. * Get the column names for the types of the speaker profile
  62. */
  63. function getProfileFields(){
  64. $fields=array();
  65. $q=$this->db->query('show columns from speaker_profile');
  66. foreach($q->result() as $k=>$v){
  67. if($v->Field!='ID'){ $fields[]=$v->Field; }
  68. }
  69. return $fields;
  70. }
  71. //----------------------
  72. /**
  73. * Get the details from the speaker's profile based on what the token defines
  74. * $token string Token name
  75. */
  76. function getDetailByToken($token){
  77. $tok_detail=$this->getTokenDetail($token);
  78. // Get the fields they're allowing for this token
  79. $fields=$this->getTokenAccess($tok_detail[0]->ID);
  80. // And get the user's profile...
  81. $profile=$this->getProfileById($tok_detail[0]->speaker_profile_id);
  82. $profile=$profile[0];
  83. $details=array();
  84. foreach($fields as $f){
  85. $name=$f->field_name;
  86. if(isset($profile->$name) && !empty($profile->$name)){ $details[$name]=$profile->$name; }
  87. }
  88. return $details;
  89. }
  90. /**
  91. * Get the full access (all tokens/all fields) information for a profile
  92. * $pid integer Profile ID
  93. * $tid[optional] integer Token ID
  94. */
  95. function getProfileAccess($pid,$tid=null){
  96. $data=array();
  97. $q=$this->db->get_where('speaker_tokens',array('speaker_profile_id'=>$pid));
  98. $data['token']=$q->result();
  99. if(isset($data['token'][0])){
  100. $data['fields']=$this->getTokenAccess($data['token'][0]->ID);
  101. }
  102. return (empty($data)) ? false : $data;
  103. }
  104. /**
  105. * Based on a user ID, get the token information for the user's profile
  106. * $uid integer User ID
  107. */
  108. function getUserProfileAccess($uid){
  109. $profile= $this->getProfile($uid);
  110. return $this->getProfileTokens($profile[0]->ID);
  111. }
  112. /**
  113. * $public boolean Return only public profile or all
  114. */
  115. function getUserPublicProfile($uid,$public=false){
  116. $profile= $this->getProfile($uid);
  117. if(!isset($profile[0])){ /* no profile! */ return array(); }
  118. $access = $this->getProfileTokens($profile[0]->ID);
  119. if($public===true){
  120. foreach($access as $a){ if($a->is_public=='Y'){
  121. // here's the tokens they have access to
  122. $ret =$this->getTokenAccess($a->ID);
  123. $data=array();
  124. foreach($ret as $k=>$v){
  125. $field = $v->field_name;
  126. $ret[$k]->val = $profile[0]->$field;
  127. $data[$field] = $profile[0]->$field;
  128. }
  129. return array('token'=>$a,'access'=>$ret,'data'=>$data);
  130. }}
  131. }
  132. return $access;
  133. }
  134. /**
  135. * Based on the token ID, gets the fields that it has access to
  136. * $tid integer Token ID
  137. */
  138. function getTokenAccess($tid){
  139. $q=$this->db->get_where('speaker_token_fields',array('speaker_token_id'=>$tid));
  140. return $q->result();
  141. }
  142. /**
  143. * Given the profile ID, get the tokens related to the profile
  144. * $tid integer Token ID
  145. */
  146. function getProfileTokens($pid){
  147. $q=$this->db->get_where('speaker_tokens',array('speaker_profile_id'=>$pid));
  148. return $q->result();
  149. }
  150. /**
  151. * Based on a token name, Get the detail from the tokens table
  152. * $token string Token name
  153. */
  154. function getTokenDetail($token){
  155. $q=$this->db->get_where('speaker_tokens',array('access_token'=>$token));
  156. return $q->result();
  157. }
  158. /**
  159. * Used to set up a token and the field access that goes along with it
  160. * $uid integer User ID
  161. * $name string Token name (user defined)
  162. * $fields array List of access fields
  163. */
  164. function setProfileAccess($uid,$name,$desc,$fields,$is_public=null){
  165. //First, insert into the token table...
  166. $profile= $this->getProfile($uid);
  167. $pid = $profile[0]->ID;
  168. $arr=array(
  169. 'speaker_profile_id' => $pid,
  170. 'access_token' => $name,
  171. 'description' => $desc,
  172. 'is_public' => ($is_public!==null) ? 'Y' : null,
  173. 'created' => time()
  174. );
  175. //Be sure we don't already have profile access like this
  176. $tokens=$this->getProfileTokens($pid);
  177. foreach($tokens as $t){ if($t->access_token==$name){ return false; }}
  178. //Keep going and do the insert...
  179. $this->db->insert('speaker_tokens',$arr);
  180. $tid=$this->db->insert_id();
  181. if($is_public!==null){ $this->setProfileViewable($uid,$tid,$is_public); }
  182. //Now, for each of the fields they gave us, put its name in the fields table
  183. foreach($fields as $f){
  184. $arr=array('speaker_token_id'=>$tid,'field_name'=>$f);
  185. $this->db->insert('speaker_token_fields',$arr);
  186. }
  187. return true;
  188. }
  189. /**
  190. * Update the token's access fields
  191. * $uid integer User ID
  192. * $tid integer Token ID
  193. * $fields array List of access fields
  194. */
  195. function updateProfileAccess($uid,$tid,$fields,$is_public=null){
  196. // Be sure we're supposed to work on this token
  197. if(!$this->isUserToken($uid,$tid)){ return false; }
  198. // drop all of the token access fields for the token...
  199. $this->db->where('speaker_token_id',$tid);
  200. $this->db->delete('speaker_token_fields');
  201. if($is_public!==null){ $this->setProfileViewable($uid,$tid,$is_public); }
  202. // Now add in our new ones
  203. foreach($fields as $f){
  204. $arr=array('speaker_token_id'=>$tid,'field_name'=>$f);
  205. $this->db->insert('speaker_token_fields',$arr);
  206. }
  207. return true;
  208. }
  209. /**
  210. * Remove the access based on a token ID
  211. * $uid integer User ID
  212. * $tid integer Token ID
  213. */
  214. function deleteProfileAccess($uid,$tid){
  215. // Be sure it's theirs first...
  216. $profile=$this->getProfile($uid); //print_r($profile);
  217. $tokens=$this->getProfileTokens($profile[0]->ID); //print_r($tokens);
  218. foreach($tokens as $t){
  219. if($t->ID==$tid){
  220. $this->db->where('ID',$tid); $this->db->delete('speaker_tokens');
  221. $this->db->where('speaker_token_id',$tid); $this->db->delete('speaker_token_fields');
  222. }
  223. }
  224. }
  225. /**
  226. * Set the selected access profile's viewable status
  227. * $uid integer User ID
  228. * $tid integer Token ID
  229. * $is_public [optional] Viewable status
  230. */
  231. function setProfileViewable($uid,$tid,$is_public=null){
  232. //first we need to set all of the current ones to non-public
  233. $sql=sprintf('
  234. select
  235. st.ID
  236. from
  237. speaker_profile sp,
  238. speaker_tokens st
  239. where
  240. sp.user_id=%s and
  241. sp.ID=st.speaker_profile_id
  242. ',$this->db->escape($uid));
  243. $q=$this->db->query($sql);
  244. foreach($q->result() as $token){
  245. $this->db->where('ID',$token->ID); $this->db->update('speaker_tokens',array('is_public'=>null));
  246. }
  247. // Now we can just set the one we need
  248. $this->db->where('ID',$tid);
  249. $this->db->update('speaker_tokens',array('is_public'=>$is_public));
  250. }
  251. }
  252. ?>