LogableBehavior.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. App::uses('CakeSession', 'Model/Datasource');
  3. App::uses('ModelBehavior', 'Model');
  4. if (!defined('CLASS_USER')) {
  5. define('CLASS_USER', 'User');
  6. }
  7. /**
  8. * Logs saves and deletes of any model
  9. *
  10. * Requires the following to work as intended :
  11. *
  12. * - "Log" model ( empty but for a order variable [created DESC]
  13. * - "logs" table with these fields required :
  14. * - id [int] :
  15. * - title [string] : automagically filled with the display field of the model that was modified.
  16. * - created [date/datetime] : filled by cake in normal way
  17. *
  18. * - actsAs = array("Tools.Logable"); on models that should be logged
  19. *
  20. * Optional extra table fields for the "logs" table :
  21. *
  22. * - "description" [string] : Fill with a descriptive text of what, who and to which model/row :
  23. * "Contact "John Smith"(34) added by User "Administrator"(1).
  24. *
  25. * or if u want more detail, add any combination of the following :
  26. *
  27. * - "" [string] : automagically filled with the class name of the model that generated the activity.
  28. * - "foreign_id" [int] : automagically filled with the primary key of the model that was modified.
  29. * - "action" [string] : automagically filled with what action is made (add/edit/delete)
  30. * - "user_id" [int] : populated with the supplied user info. (May be renamed. See bellow.)
  31. * - "change" [string] : depending on setting either :
  32. * [name (alek) => (Alek), age (28) => (29)] or [name, age]
  33. *
  34. * - "version_id" [int] : cooperates with RevisionBehavior to link the the shadow table (thus linking to old data)
  35. *
  36. * Remember that Logable behavior needs to be added after RevisionBehavior. In fact, just put it last to be safe.
  37. *
  38. * Optionally register what user was responisble for the activity :
  39. *
  40. * - Supply configuration only if defaults are wrong. Example given with defaults :
  41. *
  42. * public $actsAs = array('Logable' => array('userModel' => 'User', 'userKey' => 'user_id'));
  43. *
  44. * - In AppController (or single controller if only needed once) add these lines to beforeFilter :
  45. *
  46. * if (count($this->uses) && $this->{$this->modelClass}->Behaviors->attached('Logable')) {
  47. * $this->{$this->modelClass}->setUserData($this->activeUser);
  48. * }
  49. *
  50. * Where "$activeUser" should be an array in the standard format for the User model used :
  51. *
  52. * $activeUser = array( $UserModel->alias => array( $UserModel->primaryKey => 123, $UserModel->displayField => 'Alexander'));
  53. * // any other key is just ignored by this behaviour.
  54. *
  55. * @author Alexander Morland (alexander#maritimecolours.no)
  56. * @co-author Eskil Mjelva Saatvedt
  57. * @co-author Ronny Vindenes
  58. * @co-author Carl Erik Fyllingen
  59. * @contributor Miha
  60. * @category Behavior
  61. * @version 2.2
  62. * @modified 3.june 2009 by Miha
  63. * @modified 2011-11-17 ms (mark scherer) cake2.0 ready
  64. *
  65. * 2011-11-17 ms
  66. */
  67. class LogableBehavior extends ModelBehavior {
  68. public $user = null; # user data array
  69. public $UserModel = null;
  70. protected $_defaults = array(
  71. 'enabled' => true,
  72. 'on' => 'save', // validate/save
  73. 'userModel' => CLASS_USER,
  74. 'logModel' => 'Tools.Log',
  75. 'userKey' => 'user_id',
  76. 'change' => 'list',
  77. 'descriptionIds' => true,
  78. 'skip' => array(),
  79. 'ignore' => array(),
  80. 'classField' => 'model',
  81. 'foreignKey' => 'foreign_id',
  82. 'autoRelation' => false, # attach relation to the model (hasMany Log)
  83. );
  84. /**
  85. * Config options are :
  86. * - userModel : 'User'. Class name of the user model you want to use (User by default), if you want to save User in log
  87. * - userKey : 'user_id'. The field for saving the user to (user_id by default).
  88. * - change : 'list' > [name, age]. Set to 'full' for [name (alek) => (Alek), age (28) => (29)]
  89. * - descriptionIds : TRUE. Set to false to not include model id and user id in the title field
  90. * - skip: array(). String array of actions to not log
  91. * - ignore: array(). Fields to ignore
  92. *
  93. * @param Object $Model
  94. * @param array $config
  95. */
  96. public function setup(Model $Model, $config = array()) {
  97. if (!is_array($config)) {
  98. $config = array();
  99. }
  100. $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
  101. $this->settings[$Model->alias]['ignore'][] = $Model->primaryKey;
  102. $this->Log = ClassRegistry::init($this->settings[$Model->alias]['logModel']);
  103. if ($this->settings[$Model->alias]['userModel'] !== $Model->alias) {
  104. $this->UserModel = ClassRegistry::init($this->settings[$Model->alias]['userModel']);
  105. } else {
  106. $this->UserModel = $Model;
  107. }
  108. }
  109. public function settings(Model $Model) {
  110. return $this->settings[$Model->alias];
  111. }
  112. public function enableLog(Model $Model, $enable = null) {
  113. if ($enable !== null) {
  114. $this->settings[$Model->alias]['enabled'] = $enable;
  115. }
  116. return $this->settings[$Model->alias]['enabled'];
  117. }
  118. /**
  119. * Useful for getting logs for a model, takes params to narrow find.
  120. * This method can actually also be used to find logs for all models or
  121. * even another model. Using no params will return all activities for
  122. * the models it is called from.
  123. *
  124. * Possible params :
  125. * 'model' : mixed (null) String with className, null to get current or false to get everything
  126. * 'action' : string (null) String with action (add/edit/delete), null gets all
  127. * 'order' : string ('created DESC') String with custom order
  128. * 'conditions : array (array()) Add custom conditions
  129. * 'foreign_id' : int (null) Add a int
  130. *
  131. * (remember to use your own user key if you're not using 'user_id')
  132. * 'user_id' : int (null) Defaults to all users, supply id if you want for only one User
  133. *
  134. * @param Object $Model
  135. * @param array $params
  136. * @return array
  137. */
  138. public function findLog(Model $Model, $params = array()) {
  139. $defaults = array(
  140. $this->settings[$Model->alias]['classField'] => null,
  141. 'action' => null,
  142. 'order' => $this->Log->alias . '.id DESC',
  143. $this->settings[$Model->alias]['userKey'] => null,
  144. 'conditions' => array(),
  145. $this->settings[$Model->alias]['foreignKey'] => null,
  146. 'fields' => array(),
  147. 'limit' => 50,
  148. );
  149. $params = array_merge($defaults, $params);
  150. $options = array('order' => $params['order'], 'conditions' => $params['conditions'], 'fields' => $params['fields'], 'limit' => $params['limit']);
  151. if ($params[$this->settings[$Model->alias]['classField']] === null) {
  152. $params[$this->settings[$Model->alias]['classField']] = $Model->alias;
  153. }
  154. if ($params[$this->settings[$Model->alias]['classField']]) {
  155. if ($this->Log->hasField($this->settings[$Model->alias]['classField'])) {
  156. $options['conditions'][$this->settings[$Model->alias]['classField']] = $params[$this->settings[$Model->alias]['classField']];
  157. } elseif ($this->Log->hasField('description')) {
  158. $options['conditions']['description LIKE '] = $params[$this->settings[$Model->alias]['classField']] . '%';
  159. } else {
  160. return false;
  161. }
  162. }
  163. if ($params['action'] && $this->Log->hasField('action')) {
  164. $options['conditions']['action'] = $params['action'];
  165. }
  166. if ($params[$this->settings[$Model->alias]['userKey']] && $this->UserModel && is_numeric($params[$this->settings[$Model->alias]['userKey']])) {
  167. $options['conditions'][$this->settings[$Model->alias]['userKey']] = $params[$this->settings[$Model->alias]['userKey']];
  168. }
  169. if ($params[$this->settings[$Model->alias]['foreignKey']] && is_numeric($params[$this->settings[$Model->alias]['foreignKey']])) {
  170. $options['conditions'][$this->settings[$Model->alias]['foreignKey']] = $params[$this->settings[$Model->alias]['foreignKey']];
  171. }
  172. return $this->Log->find('all', $options);
  173. }
  174. /**
  175. * Get list of actions for one user.
  176. * Params for getting (one line) activity descriptions
  177. * and/or for just one model
  178. *
  179. * @example $this->Model->findUserActions(301, array('model' => 'BookTest'));
  180. * @example $this->Model->findUserActions(301, array('events' => true));
  181. * @example $this->Model->findUserActions(301, array('fields' => array('id','model'),'model' => 'BookTest');
  182. * @param Object $Model
  183. * @param integer $user_id
  184. * @param array $params
  185. * @return array
  186. */
  187. public function findUserActions(Model $Model, $user_id, $params = array()) {
  188. if (!$this->UserModel) {
  189. return null;
  190. }
  191. // if logged in user is asking for her own log, use the data we allready have
  192. if (isset($this->user) && isset($this->user[$this->UserModel->alias][$this->UserModel->primaryKey]) && $user_id == $this->user[$this->
  193. UserModel->alias][$this->UserModel->primaryKey] && isset($this->user[$this->UserModel->alias][$this->UserModel->displayField])) {
  194. $username = $this->user[$this->UserModel->alias][$this->UserModel->displayField];
  195. } else {
  196. $this->UserModel->recursive = -1;
  197. $user = $this->UserModel->find('first', array('conditions'=>array($this->UserModel->primaryKey => $user_id)));
  198. $username = $user[$this->UserModel->alias][$this->UserModel->displayField];
  199. }
  200. $fields = array();
  201. if (isset($params['fields'])) {
  202. if (is_array($params['fields'])) {
  203. $fields = $params['fields'];
  204. } else {
  205. $fields = array($params['fields']);
  206. }
  207. }
  208. $conditions = array($this->settings[$Model->alias]['userKey'] => $user_id);
  209. if (isset($params[$this->settings[$Model->alias]['classField']])) {
  210. $conditions[$this->settings[$Model->alias]['classField']] = $params[$this->settings[$Model->alias]['classField']];
  211. }
  212. $order = array($this->Log->alias . '.id' => 'DESC');
  213. if (isset($params['order'])) {
  214. $order = $params['order'];
  215. }
  216. $data = $this->Log->find('all', array(
  217. 'conditions' => $conditions,
  218. 'recursive' => -1,
  219. 'fields' => $fields,
  220. 'order' => $order
  221. ));
  222. if (!isset($params['events']) || (isset($params['events']) && $params['events'] == false)) {
  223. return $data;
  224. }
  225. $result = array();
  226. foreach ($data as $key => $row) {
  227. $one = $row[$this->Log->alias];
  228. $result[$key][$this->Log->alias]['id'] = $one['id'];
  229. $result[$key][$this->Log->alias]['event'] = $username;
  230. // have all the detail models and change as list :
  231. if (isset($one[$this->settings[$Model->alias]['classField']]) && isset($one['action']) && isset($one['change']) && isset($one[$this->
  232. settings[$Model->alias]['foreignKey']])) {
  233. if ($one['action'] === 'edit') {
  234. $result[$key][$this->Log->alias]['event'] .= ' edited ' . $one['change'] . ' of ' . strtolower($one[$this->settings[$Model->alias]['classField']]) .
  235. '(id ' . $one[$this->settings[$Model->alias]['foreignKey']] . ')';
  236. // ' at '.$one['created'];
  237. } elseif ($one['action'] === 'add') {
  238. $result[$key][$this->Log->alias]['event'] .= ' added a ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  239. settings[$Model->alias]['foreignKey']] . ')';
  240. } elseif ($one['action'] === 'delete') {
  241. $result[$key][$this->Log->alias]['event'] .= ' deleted the ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  242. settings[$Model->alias]['foreignKey']] . ')';
  243. }
  244. } elseif (isset($one[$this->settings[$Model->alias]['classField']]) && isset($one['action']) && isset($one[$this->settings[$Model->alias]['foreignKey']])) { // have model,foreign_id and action
  245. if ($one['action'] === 'edit') {
  246. $result[$key][$this->Log->alias]['event'] .= ' edited ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  247. settings[$Model->alias]['foreignKey']] . ')';
  248. // ' at '.$one['created'];
  249. } elseif ($one['action'] === 'add') {
  250. $result[$key][$this->Log->alias]['event'] .= ' added a ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  251. settings[$Model->alias]['foreignKey']] . ')';
  252. } elseif ($one['action'] === 'delete') {
  253. $result[$key][$this->Log->alias]['event'] .= ' deleted the ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  254. settings[$Model->alias]['foreignKey']] . ')';
  255. }
  256. } else { // only description field exist
  257. $result[$key][$this->Log->alias]['event'] = $one['description'];
  258. }
  259. }
  260. return $result;
  261. }
  262. /**
  263. * Use this to supply a model with the data of the logged in User.
  264. * Intended to be called in AppController::beforeFilter like this :
  265. *
  266. * if ($this->{$this->modelClass}->Behaviors->attached('Logable')) {
  267. * $this->{$this->modelClass}->setUserData($activeUser);/
  268. * }
  269. *
  270. * The $userData array is expected to look like the result of a
  271. * User::find(array('id'=>123));
  272. *
  273. * @param Object $Model
  274. * @param array $userData
  275. */
  276. public function setUserData(Model $Model, $userData = null) {
  277. if ($userData === null && isset($Model->Session)) {
  278. $userData = (array)$Model->Session->read('Auth');
  279. } elseif ($userData === null && class_exists('CakeSession')) {
  280. $userData = (array)CakeSession::read('Auth');
  281. }
  282. if ($userData !== null) {
  283. $this->user = $userData;
  284. }
  285. }
  286. /**
  287. * Used for logging custom actions that arent crud, like login or download.
  288. *
  289. * @example $this->Boat->customLog('ship', 66, array('title' => 'Titanic heads out'));
  290. * @param Object $Model
  291. * @param string $action name of action that is taking place (dont use the crud ones)
  292. * @param integer $id id of the logged item (ie foreign_id in logs table)
  293. * @param array $values optional other values for your logs table
  294. */
  295. public function customLog(Model $Model, $action, $id = null, $values = array()) {
  296. $logData[$this->Log->alias] = $values;
  297. /**
  298. @todo clean up $logData */
  299. if ($id === null) {
  300. $id = $Model->id;
  301. }
  302. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey']) && is_numeric($id)) {
  303. $logData[$this->Log->alias][$this->settings[$Model->alias]['foreignKey']] = $id;
  304. }
  305. $title = null;
  306. if (isset($values['title'])) {
  307. $title = $values['title'];
  308. unset($logData[$this->Log->alias]['title']);
  309. }
  310. $logData[$this->Log->alias]['action'] = $action;
  311. $this->_saveLog($Model, $logData, $title);
  312. }
  313. public function clearUserData(Model $Model) {
  314. $this->user = null;
  315. }
  316. public function setUserIp(Model $Model, $userIP = null) {
  317. if ($userIP === null) {
  318. //App::uses();
  319. $userIP = CakeRequest::clientIp();
  320. }
  321. $this->userIP = $userIP;
  322. }
  323. public function beforeDelete(Model $Model, $cascade = true) {
  324. $this->setUserData($Model);
  325. if (!$this->settings[$Model->alias]['enabled']) {
  326. return true;
  327. }
  328. if (isset($this->settings[$Model->alias]['skip']['delete']) && $this->settings[$Model->alias]['skip']['delete']) {
  329. return true;
  330. }
  331. $Model->recursive = -1;
  332. $Model->read();
  333. return true;
  334. }
  335. public function afterDelete(Model $Model) {
  336. if (!$this->settings[$Model->alias]['enabled']) {
  337. return true;
  338. }
  339. if (isset($this->settings[$Model->alias]['skip']['delete']) && $this->settings[$Model->alias]['skip']['delete']) {
  340. return true;
  341. }
  342. $logData = array();
  343. if ($this->Log->hasField('description')) {
  344. $logData[$this->Log->alias]['description'] = $Model->alias;
  345. if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
  346. $logData[$this->Log->alias]['description'] .= ' "' . $Model->data[$Model->alias][$Model->displayField] . '"';
  347. }
  348. if ($this->settings[$Model->alias]['descriptionIds']) {
  349. $logData[$this->Log->alias]['description'] .= ' (' . $Model->id . ') ';
  350. }
  351. $logData[$this->Log->alias]['description'] .= __('deleted');
  352. }
  353. $logData[$this->Log->alias]['action'] = 'delete';
  354. $this->_saveLog($Model, $logData);
  355. }
  356. public function beforeValidate(Model $Model) {
  357. if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] !== 'validate') {
  358. return true;
  359. }
  360. $this->_prepareLog($Model);
  361. return true;
  362. }
  363. public function beforeSave(Model $Model) {
  364. if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] !== 'save') {
  365. return true;
  366. }
  367. $this->_prepareLog($Model);
  368. return true;
  369. }
  370. protected function _prepareLog(Model $Model) {
  371. if ($this->user === null) {
  372. $this->setUserData($Model);
  373. }
  374. if ($Model->id && empty($this->old)) {
  375. $options = array('conditions' => array($Model->primaryKey => $Model->id), 'recursive' => -1);
  376. $this->old = $Model->find('first', $options);
  377. }
  378. }
  379. public function afterSave(Model $Model, $created) {
  380. if (!$this->settings[$Model->alias]['enabled']) {
  381. return true;
  382. }
  383. if (!empty($this->settings[$Model->alias]['skip']['add']) && $created) {
  384. return true;
  385. } elseif (!empty($this->settings[$Model->alias]['skip']['edit']) && !$created) {
  386. return true;
  387. }
  388. $keys = array_keys($Model->data[$Model->alias]);
  389. $diff = array_diff($keys, $this->settings[$Model->alias]['ignore']);
  390. if (count($diff) === 0 && empty($Model->logableAction)) {
  391. return false;
  392. }
  393. if ($Model->id) {
  394. $id = $Model->id;
  395. } elseif ($Model->insertId) {
  396. $id = $Model->insertId;
  397. }
  398. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey'])) {
  399. $logData[$this->Log->alias][$this->settings[$Model->alias]['foreignKey']] = $id;
  400. }
  401. if ($this->Log->hasField('description')) {
  402. $logData[$this->Log->alias]['description'] = $Model->alias . ' ';
  403. if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
  404. $logData[$this->Log->alias]['description'] .= '"' . $Model->data[$Model->alias][$Model->displayField] . '" ';
  405. }
  406. if ($this->settings[$Model->alias]['descriptionIds']) {
  407. $logData[$this->Log->alias]['description'] .= '(' . $id . ') ';
  408. }
  409. if ($created) {
  410. $logData[$this->Log->alias]['description'] .= __('added');
  411. } else {
  412. $logData[$this->Log->alias]['description'] .= __('updated');
  413. }
  414. }
  415. if ($this->Log->hasField('action')) {
  416. if ($created) {
  417. $logData[$this->Log->alias]['action'] = 'add';
  418. } else {
  419. $logData[$this->Log->alias]['action'] = 'edit';
  420. }
  421. }
  422. if ($this->Log->hasField('change')) {
  423. $logData[$this->Log->alias]['change'] = '';
  424. $db_fields = array_keys($Model->schema());
  425. $changed_fields = array();
  426. foreach ($Model->data[$Model->alias] as $key => $value) {
  427. if (isset($Model->data[$Model->alias][$Model->primaryKey]) && !empty($this->old) && isset($this->old[$Model->alias][$key])) {
  428. $old = $this->old[$Model->alias][$key];
  429. } else {
  430. $old = '';
  431. }
  432. if ($key !== 'modified' && !in_array($key, $this->settings[$Model->alias]['ignore']) && $value != $old && in_array($key, $db_fields)) {
  433. if ($this->settings[$Model->alias]['change'] === 'full') {
  434. $changed_fields[] = $key . ' (' . $old . ') => (' . $value . ')';
  435. } elseif ($this->settings[$Model->alias]['change'] === 'serialize') {
  436. $changed_fields[$key] = array('old' => $old, 'value' => $value);
  437. } else {
  438. $changed_fields[] = $key;
  439. }
  440. }
  441. }
  442. $changes = count($changed_fields);
  443. if (!$changes) {
  444. return true;
  445. }
  446. if ($this->settings[$Model->alias]['change'] === 'serialize') {
  447. $logData[$this->Log->alias]['change'] = serialize($changed_fields);
  448. } else {
  449. $logData[$this->Log->alias]['change'] = implode(', ', $changed_fields);
  450. }
  451. $logData[$this->Log->alias]['changes'] = $changes;
  452. }
  453. if (empty($logData)) {
  454. return true;
  455. }
  456. return $this->_saveLog($Model, $logData);
  457. }
  458. /**
  459. * Does the actual saving of the Log model. Also adds the special field if possible.
  460. *
  461. * If model field in table, add the Model->alias
  462. * If action field is NOT in table, remove it from dataset
  463. * If the userKey field in table, add it to dataset
  464. * If userData is supplied to model, add it to the title
  465. *
  466. * @param Object $Model
  467. * @param array $logData
  468. * @return void
  469. */
  470. public function _saveLog(Model $Model, $logData, $title = null) {
  471. if ($title !== null) {
  472. $logData[$this->Log->alias]['title'] = $title;
  473. } elseif ($Model->displayField == $Model->primaryKey) {
  474. $logData[$this->Log->alias]['title'] = $Model->alias . ' (' . $Model->id . ')';
  475. } elseif (isset($Model->data[$Model->alias][$Model->displayField])) {
  476. $logData[$this->Log->alias]['title'] = $Model->data[$Model->alias][$Model->displayField];
  477. } else {
  478. $Model->recursive = -1;
  479. $Model->read(array($Model->displayField));
  480. $logData[$this->Log->alias]['title'] = $Model->data[$Model->alias][$Model->displayField];
  481. }
  482. if ($this->Log->hasField($this->settings[$Model->alias]['classField'])) {
  483. // by miha nahtigal
  484. $logData[$this->Log->alias][$this->settings[$Model->alias]['classField']] = $Model->name;
  485. }
  486. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey']) && !isset($logData[$this->Log->alias][$this->settings[$Model->alias]['foreignKey']])) {
  487. if ($Model->id) {
  488. $logData[$this->Log->alias][$this->settings[$Model->alias]['foreignKey']] = $Model->id;
  489. } elseif ($Model->insertId) {
  490. $logData[$this->Log->alias][$this->settings[$Model->alias]['foreignKey']] = $Model->insertId;
  491. }
  492. }
  493. if (!$this->Log->hasField('action')) {
  494. unset($logData[$this->Log->alias]['action']);
  495. } elseif (isset($Model->logableAction) && !empty($Model->logableAction)) {
  496. $logData[$this->Log->alias]['action'] = implode(',', $Model->logableAction); // . ' ' . $logData[$this->Log->alias]['action'];
  497. unset($Model->logableAction);
  498. }
  499. if ($this->Log->hasField('version_id') && isset($Model->version_id)) {
  500. $logData[$this->Log->alias]['version_id'] = $Model->version_id;
  501. unset($Model->version_id);
  502. }
  503. if ($this->Log->hasField('ip') && $this->userIP) {
  504. $logData[$this->Log->alias]['ip'] = $this->userIP;
  505. }
  506. if ($this->Log->hasField($this->settings[$Model->alias]['userKey']) && $this->user && isset($this->user[$this->UserModel->alias])) {
  507. $logData[$this->Log->alias][$this->settings[$Model->alias]['userKey']] = $this->user[$this->UserModel->alias][$this->UserModel->primaryKey];
  508. }
  509. if ($this->Log->hasField('description')) {
  510. if (empty($logData[$this->Log->alias]['description'])) {
  511. $logData[$this->Log->alias]['description'] = __('Custom action');
  512. }
  513. if ($this->user && $this->UserModel && isset($this->user[$this->UserModel->alias])) {
  514. $logData[$this->Log->alias]['description'] .= ' ' . __('by') . ' ' . $this->settings[$Model->alias]['userModel'] . ' "' . $this->user[$this->UserModel->alias][$this->UserModel->displayField] . '"';
  515. if ($this->settings[$Model->alias]['descriptionIds']) {
  516. $logData[$this->Log->alias]['description'] .= ' (' . $this->user[$this->UserModel->alias][$this->UserModel->primaryKey] . ')';
  517. }
  518. } else {
  519. // UserModel is active, but the data hasnt been set. Assume system action.
  520. $logData[$this->Log->alias]['description'] .= __(' by System');
  521. }
  522. $logData[$this->Log->alias]['description'] .= '.';
  523. }
  524. $this->Log->create($logData);
  525. $this->Log->save(null, false);
  526. }
  527. }