PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/application/models/application_logs/ApplicationLogs.class.php

https://gitlab.com/x33n/ProjectPier-Core
PHP | 259 lines | 134 code | 27 blank | 98 comment | 20 complexity | 99b1342a14d3379efefb290d15348ade MD5 | raw file
  1. <?php
  2. /**
  3. * Application logs manager class
  4. *
  5. * @http://www.projectpier.org/
  6. */
  7. class ApplicationLogs extends BaseApplicationLogs {
  8. const ACTION_ADD = 'add';
  9. const ACTION_EDIT = 'edit';
  10. const ACTION_DELETE = 'delete';
  11. const ACTION_CLOSE = 'close';
  12. const ACTION_OPEN = 'open';
  13. /**
  14. * Create new log entry and return it
  15. *
  16. * Delete actions are automatically marked as silent if $is_silent value is not provided (not NULL)
  17. *
  18. * @param ApplicationDataObject $object
  19. * @param Project $project
  20. * @param DataManager $manager
  21. * @param boolean $save Save log object before you save it
  22. * @return ApplicationLog
  23. */
  24. static function createLog(ApplicationDataObject $object, $project, $action = null, $is_private = false, $is_silent = null, $save = true) {
  25. if (is_null($action)) {
  26. $action = self::ACTION_ADD;
  27. } // if
  28. if (!self::isValidAction($action)) {
  29. throw new Error("'$action' is not valid log action");
  30. } // if
  31. if (is_null($is_silent)) {
  32. $is_silent = $action == self::ACTION_DELETE;
  33. } else {
  34. $is_silent = (boolean) $is_silent;
  35. } // if
  36. $manager = $object->manager();
  37. if (!($manager instanceof DataManager)) {
  38. throw new Error('Invalid object manager');
  39. } // if
  40. $log = new ApplicationLog();
  41. if ($project instanceof Project) {
  42. $log->setProjectId($project->getId());
  43. } // if
  44. $log->setTakenById(logged_user()->getId());
  45. $log->setRelObjectId($object->getObjectId());
  46. $log->setObjectName($object->getObjectName());
  47. $log->setRelObjectManager(get_class($manager));
  48. $log->setAction($action);
  49. $log->setIsPrivate($is_private);
  50. $log->setIsSilent($is_silent);
  51. if ($save) {
  52. $log->save();
  53. } // if
  54. // Update is private for this object
  55. if ($object instanceof ProjectDataObject) {
  56. ApplicationLogs::setIsPrivateForObject($object);
  57. } // if
  58. return $log;
  59. } // createLog
  60. /**
  61. * Update is_private flag value for all previous related log entries related with specific object
  62. *
  63. * This method is called whenever we need to add new log entry. It will keep old log entries related to that specific
  64. * object with current is_private flag value by updating all of the log entries to new value.
  65. *
  66. * @param ProjectDataObject $object
  67. * @return boolean
  68. */
  69. static function setIsPrivateForObject(ProjectDataObject $object) {
  70. return DB::execute('UPDATE ' . ApplicationLogs::instance()->getTableName(true) . ' SET `is_private` = ? WHERE `rel_object_id` = ? AND `rel_object_manager` = ?',
  71. $object->isPrivate(),
  72. $object->getObjectId(),
  73. get_class($object->manager()
  74. )); // execute
  75. } // setIsPrivateForObject
  76. /**
  77. * Mass set is_private for a given type. If $ids is present limit update only to object with given ID-s
  78. *
  79. * @param boolean $is_private
  80. * @param string $type
  81. * @parma array $ids
  82. * @return boolean
  83. */
  84. static function setIsPrivateForType($is_private, $type, $ids = null) {
  85. $limit_ids = null;
  86. if (is_array($ids)) {
  87. $limit_ids = array();
  88. foreach ($ids as $id) {
  89. $limit_ids[] = DB::escape($id);
  90. } // if
  91. $limit_ids = count($limit_ids) > 0 ? implode(',', $limit_ids) : null;
  92. } // if
  93. $sql = DB::prepareString('UPDATE ' . ApplicationLogs::instance()->getTableName(true) . ' SET `is_private` = ? WHERE `rel_object_manager` = ?', array($is_private, $type));
  94. if ($limit_ids !== null) {
  95. $sql .= " AND `rel_object_id` IN ($limit_ids)";
  96. } // if
  97. return DB::execute($sql);
  98. } // setIsPrivateForType
  99. /**
  100. * Return entries related to specific project
  101. *
  102. * If $include_private is set to true private entries will be included in result. If $include_silent is set to true
  103. * logs marked as silent will also be included. $limit and $offset are there to control the range of the result,
  104. * usually we don't want to pull the entire log but just the few most recent entries. If NULL they will be ignored
  105. *
  106. * @param Project $project
  107. * @param boolean $include_private
  108. * @param boolean $include_silent
  109. * @param integer $limit
  110. * @param integer $offset
  111. * @return array
  112. */
  113. static function getProjectLogs(Project $project, $include_private = false, $include_silent = false, $limit = null, $offset = null) {
  114. trace(__FILE__,"getProjectLogs({$project->getId()}, $include_private, $include_silent, $limit, $offset)");
  115. $private_filter = $include_private ? 1 : 0;
  116. $silent_filter = $include_silent ? 1 : 0;
  117. $all_logs = self::findAll(array(
  118. 'conditions' => array('`is_private` <= ? AND `is_silent` <= ? AND `project_id` = (?)', $private_filter, $silent_filter, $project->getId()),
  119. 'order' => '`created_on` DESC',
  120. 'limit' => $limit,
  121. 'offset' => $offset
  122. )); // findAll
  123. return ApplicationLogs::filterLogs($all_logs);
  124. } // getProjectLogs
  125. /**
  126. * Exclude logs from uninstalled plugins
  127. *
  128. * @param Project $project
  129. * @param boolean $include_private
  130. * @param boolean $include_silent
  131. * @param integer $limit
  132. * @param integer $offset
  133. * @return array
  134. */
  135. static function filterLogs($all_logs) {
  136. trace(__FILE__,"filterLogs({$all_logs})");
  137. if ($all_logs==NULL) return;
  138. $filtered_logs = array();
  139. //Only show logs related to installed applications
  140. foreach ($all_logs as $log)
  141. {
  142. trace(__FILE__,"filterLogs($all_logs) - foreach ");
  143. //if ($log->getObject()==NULL) continue;
  144. $manager_class_name = $log->getRelObjectManager();
  145. //Search the string 'plugins' in the file path containing the class, if false is a core application else is a plugin
  146. if (!isset($GLOBALS['autoloader_classes'][strtoupper($manager_class_name)])) continue;
  147. if (strpos($GLOBALS['autoloader_classes'][strtoupper($manager_class_name)], 'plugins') == false)
  148. //if (strpos($GLOBALS['autoloader_classes'][strtoupper(get_class($log->getObject()))], 'plugins') == false)
  149. {
  150. trace(__FILE__,"filterLogs($all_logs) - core ".$manager_class_name);
  151. //the class is a core application
  152. $filtered_logs[]=$log;
  153. }
  154. else
  155. {
  156. trace(__FILE__,"filterLogs($all_logs) - plugin: ".$manager_class_name);
  157. //TODO
  158. //the class comes from a plugin
  159. //so we need to filter logs from uninstalled plugins
  160. //$log_class = $manager_class_name; //get_class($log->getObject()->manager());
  161. //$plugin = Plugins::findOne(array('conditions'=>array("`log_providers` like '%".$log_class."%'")));
  162. //error_log("Looking for plugin with log_provider $log_class, found ".$plugin->getName());
  163. //if($plugin instanceof Plugin && $plugin->isInstalled()) {
  164. // $filtered_logs[] = $log;
  165. //} //if
  166. if (class_exists($manager_class_name)) {
  167. $filtered_logs[] = $log;
  168. }
  169. }
  170. }
  171. return $filtered_logs;
  172. } // filterLogs
  173. /**
  174. * Return overall (for dashboard or RSS)
  175. *
  176. * This function will return array of application logs that match the function arguments. Entries can be filtered by
  177. * type (private, silent), projects (if $project_ids is array, if NULL project ID is ignored). Result set can be
  178. * also limited using $limit and $offset params
  179. *
  180. * @param boolean $include_private
  181. * @param boolean $include_silent
  182. * @param mixed $project_ids
  183. * @param integer $limit
  184. * @param integer $offset
  185. * @return array
  186. */
  187. static function getOverallLogs($include_private = false, $include_silent = false, $project_ids = null, $limit = null, $offset = null) {
  188. $private_filter = $include_private ? 1 : 0;
  189. $silent_filter = $include_silent ? 1 : 0;
  190. if (is_array($project_ids)) {
  191. $conditions = array('`is_private` <= ? AND `is_silent` <= ? AND `project_id` IN (?)', $private_filter, $silent_filter, $project_ids);
  192. } else {
  193. $conditions = array('`is_private` <= ? AND `is_silent` <= ?', $private_filter, $silent_filter);
  194. } // if
  195. $all_logs = self::findAll(array(
  196. 'conditions' => $conditions,
  197. 'order' => '`created_on` DESC',
  198. 'limit' => $limit,
  199. 'offset' => $offset,
  200. )); // findAll
  201. return ApplicationLogs::filterLogs($all_logs);
  202. } // getOverallLogs
  203. /**
  204. * Clear all logs related with specific project
  205. *
  206. * @param Project $project
  207. * @return boolean
  208. */
  209. static function clearByProject(Project $project) {
  210. return self::delete(array('`project_id` = ?', $project->getId()));
  211. } // clearByProject
  212. /**
  213. * Check if specific action is valid
  214. *
  215. * @param string $action
  216. * @return boolean
  217. */
  218. static function isValidAction($action) {
  219. static $valid_actions = null;
  220. if (!is_array($valid_actions)) {
  221. $valid_actions = array(
  222. self::ACTION_ADD,
  223. self::ACTION_EDIT,
  224. self::ACTION_DELETE,
  225. self::ACTION_CLOSE,
  226. self::ACTION_OPEN
  227. ); // array
  228. } // if
  229. return in_array($action, $valid_actions);
  230. } // isValidAction
  231. } // ApplicationLogs
  232. ?>