User.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. class Application_Model_User
  3. {
  4. private $_userInstance;
  5. public function __construct($userId)
  6. {
  7. if (empty($userId)) {
  8. $this->_userInstance = $this->createUser();
  9. } else {
  10. $this->_userInstance = CcSubjsQuery::create()->findPK($userId);
  11. if (is_null($this->_userInstance)) {
  12. throw new Exception();
  13. }
  14. }
  15. }
  16. public function getId()
  17. {
  18. return $this->_userInstance->getDbId();
  19. }
  20. public function isGuest()
  21. {
  22. return $this->getType() == UTYPE_GUEST;
  23. }
  24. public function isHostOfShow($showId)
  25. {
  26. $userId = $this->_userInstance->getDbId();
  27. return CcShowHostsQuery::create()
  28. ->filterByDbShow($showId)
  29. ->filterByDbHost($userId)->count() > 0;
  30. }
  31. public function isHost()
  32. {
  33. return $this->isUserType(UTYPE_HOST);
  34. }
  35. public function isPM()
  36. {
  37. return $this->isUserType(UTYPE_PROGRAM_MANAGER);
  38. }
  39. public function isAdmin()
  40. {
  41. return $this->isUserType(UTYPE_ADMIN);
  42. }
  43. public function canSchedule($p_showId)
  44. {
  45. $type = $this->getType();
  46. $result = false;
  47. if ($type === UTYPE_ADMIN ||
  48. $type === UTYPE_PROGRAM_MANAGER ||
  49. self::isHostOfShow($p_showId)) {
  50. $result = true;
  51. }
  52. return $result;
  53. }
  54. // TODO : refactor code to only accept arrays for isUserType and
  55. // simplify code even further
  56. public function isUserType($type)
  57. {
  58. if (!is_array($type)) {
  59. $type = array($type);
  60. }
  61. $real_type = $this->_userInstance->getDbType();
  62. return in_array($real_type, $type);
  63. }
  64. public function setLogin($login)
  65. {
  66. $user = $this->_userInstance;
  67. $user->setDbLogin($login);
  68. }
  69. public function setPassword($password)
  70. {
  71. $user = $this->_userInstance;
  72. $user->setDbPass(md5($password));
  73. }
  74. public function setFirstName($firstName)
  75. {
  76. $user = $this->_userInstance;
  77. $user->setDbFirstName($firstName);
  78. }
  79. public function setLastName($lastName)
  80. {
  81. $user = $this->_userInstance;
  82. $user->setDbLastName($lastName);
  83. }
  84. public function setType($type)
  85. {
  86. $user = $this->_userInstance;
  87. $user->setDbType($type);
  88. }
  89. public function setEmail($email)
  90. {
  91. $user = $this->_userInstance;
  92. $user->setDbEmail(strtolower($email));
  93. }
  94. public function setCellPhone($cellPhone)
  95. {
  96. $user = $this->_userInstance;
  97. $user->setDbCellPhone($cellPhone);
  98. }
  99. public function setSkype($skype)
  100. {
  101. $user = $this->_userInstance;
  102. $user->setDbSkypeContact($skype);
  103. }
  104. public function setJabber($jabber)
  105. {
  106. $user = $this->_userInstance;
  107. $user->setDbJabberContact($jabber);
  108. }
  109. public function getLogin()
  110. {
  111. $user = $this->_userInstance;
  112. return $user->getDbLogin();
  113. }
  114. public function getPassword()
  115. {
  116. $user = $this->_userInstance;
  117. return $user->getDbPass();
  118. }
  119. public function getFirstName()
  120. {
  121. $user = $this->_userInstance;
  122. return $user->getDbFirstName();
  123. }
  124. public function getLastName()
  125. {
  126. $user = $this->_userInstance;
  127. return $user->getDbLastName();
  128. }
  129. public function getType()
  130. {
  131. $user = $this->_userInstance;
  132. return $user->getDbType();
  133. }
  134. public function getEmail()
  135. {
  136. $user = $this->_userInstance;
  137. return $user->getDbEmail();
  138. }
  139. public function getCellPhone()
  140. {
  141. $user = $this->_userInstance;
  142. return $user->getDbCellPhone();
  143. }
  144. public function getSkype()
  145. {
  146. $user = $this->_userInstance;
  147. return $user->getDbSkypeContact();
  148. }
  149. public function getJabber()
  150. {
  151. $user = $this->_userInstance;
  152. return $user->getDbJabberContact();
  153. }
  154. public function save()
  155. {
  156. $this->_userInstance->save();
  157. }
  158. public function delete()
  159. {
  160. if (!$this->_userInstance->isDeleted()) {
  161. $this->_userInstance->delete();
  162. }
  163. }
  164. public function getOwnedFiles()
  165. {
  166. $user = $this->_userInstance;
  167. // do we need a find call at the end here?
  168. return $user->getCcFilessRelatedByDbOwnerId();
  169. }
  170. public function donateFilesTo($user) // $user is object not user id
  171. {
  172. $my_files = $this->getOwnedFiles();
  173. foreach ($my_files as $file) {
  174. $file->reassignTo($user);
  175. }
  176. }
  177. public function deleteAllFiles()
  178. {
  179. $my_files = $this->getOwnedFiles();
  180. foreach ($my_files as $file) {
  181. $file->delete();
  182. }
  183. }
  184. private function createUser()
  185. {
  186. $user = new CcSubjs();
  187. return $user;
  188. }
  189. public static function getUsersOfType($type)
  190. {
  191. return CcSubjsQuery::create()->filterByDbType($type)->find();
  192. }
  193. public static function getFirstAdmin() {
  194. $admins = Application_Model_User::getUsersOfType('A');
  195. if (count($admins) > 0) { // found admin => pick first one
  196. return $admins[0];
  197. } else {
  198. Logging::warn("Warning. no admins found in database");
  199. return null;
  200. }
  201. }
  202. public static function getFirstAdminId()
  203. {
  204. $admin = self::getFirstAdmin();
  205. if ($admin) {
  206. return $admin->getDbId();
  207. } else {
  208. return null;
  209. }
  210. }
  211. public static function getUsers(array $type, $search=null)
  212. {
  213. $con = Propel::getConnection();
  214. $sql_gen = "SELECT login AS value, login AS label, id as index FROM cc_subjs ";
  215. $types = array();
  216. $params = array();
  217. for ($i=0; $i<count($type); $i++) {
  218. $p = ":type{$i}";
  219. $types[] = "type = $p";
  220. $params[$p] = $type[$i];
  221. }
  222. $sql_type = join(" OR ", $types);
  223. $sql = $sql_gen ." WHERE (". $sql_type.") ";
  224. $sql .= " AND login ILIKE :search";
  225. $params[":search"] = "%$search%";
  226. $sql = $sql ." ORDER BY login";
  227. return Application_Common_Database::prepareAndExecute($sql, $params, "all");
  228. }
  229. public static function getUserCount()
  230. {
  231. $sql_gen = "SELECT count(*) AS cnt FROM cc_subjs";
  232. $query = Application_Common_Database::prepareAndExecute($sql_gen, array(),
  233. Application_Common_Database::COLUMN);
  234. return ($query !== false) ? $query : null;
  235. }
  236. public static function getHosts($search=null)
  237. {
  238. return Application_Model_User::getUsers(array('H'), $search);
  239. }
  240. public static function getUsersDataTablesInfo($datatables)
  241. {
  242. $con = Propel::getConnection(CcSubjsPeer::DATABASE_NAME);
  243. $displayColumns = array("id", "login", "first_name", "last_name", "type");
  244. $fromTable = "cc_subjs";
  245. // get current user
  246. $username = "";
  247. $auth = Zend_Auth::getInstance();
  248. if ($auth->hasIdentity()) {
  249. $username = $auth->getIdentity()->login;
  250. }
  251. $res = Application_Model_Datatables::findEntries($con, $displayColumns, $fromTable, $datatables);
  252. // mark record which is for the current user
  253. foreach ($res['aaData'] as &$record) {
  254. if ($record['login'] == $username) {
  255. $record['delete'] = "self";
  256. } else {
  257. $record['delete'] = "";
  258. }
  259. $record = array_map('htmlspecialchars', $record);
  260. }
  261. return $res;
  262. }
  263. public static function getUserData($id)
  264. {
  265. $sql = <<<SQL
  266. SELECT login, first_name, last_name, type, id, email, cell_phone, skype_contact,
  267. jabber_contact
  268. FROM cc_subjs
  269. WHERE id = :id
  270. SQL;
  271. return Application_Common_Database::prepareAndExecute($sql, array(
  272. ":id" => $id), 'single');
  273. }
  274. public static function getCurrentUser()
  275. {
  276. $userinfo = Zend_Auth::getInstance()->getStorage()->read();
  277. if (is_null($userinfo)) {
  278. return null;
  279. }
  280. try {
  281. return new self($userinfo->id);
  282. } catch (Exception $e) {
  283. //we get here if $userinfo->id is defined, but doesn't exist
  284. //in the database anymore.
  285. Zend_Auth::getInstance()->clearIdentity();
  286. return null;
  287. }
  288. }
  289. }