LogableBehavior.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. 'userKey' => 'user_id',
  75. 'change' => 'list',
  76. 'descriptionIds' => true,
  77. 'skip' => array(),
  78. 'ignore' => array(),
  79. 'classField' => 'model',
  80. 'foreignKey' => 'foreign_id',
  81. 'autoRelation' => false, # attach relation to the model (hasMany Log)
  82. );
  83. /**
  84. * Config options are :
  85. * - userModel : 'User'. Class name of the user model you want to use (User by default), if you want to save User in log
  86. * - userKey : 'user_id'. The field for saving the user to (user_id by default).
  87. * - change : 'list' > [name, age]. Set to 'full' for [name (alek) => (Alek), age (28) => (29)]
  88. * - descriptionIds : TRUE. Set to false to not include model id and user id in the title field
  89. * - skip: array(). String array of actions to not log
  90. * - ignore: array(). Fields to ignore
  91. *
  92. * @param Object $Model
  93. * @param array $config
  94. */
  95. public function setup(Model $Model, $config = array()) {
  96. if (!is_array($config)) {
  97. $config = array();
  98. }
  99. $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
  100. $this->settings[$Model->alias]['ignore'][] = $Model->primaryKey;
  101. $this->Log = ClassRegistry::init('Tools.Log');
  102. if ($this->settings[$Model->alias]['userModel'] != $Model->alias) {
  103. $this->UserModel = ClassRegistry::init($this->settings[$Model->alias]['userModel']);
  104. } else {
  105. $this->UserModel = $Model;
  106. }
  107. }
  108. public function settings(Model $Model) {
  109. return $this->settings[$Model->alias];
  110. }
  111. public function enableLog(Model $Model, $enable = null) {
  112. if ($enable !== null) {
  113. $this->settings[$Model->alias]['enabled'] = $enable;
  114. }
  115. return $this->settings[$Model->alias]['enabled'];
  116. }
  117. /**
  118. * Useful for getting logs for a model, takes params to narrow find.
  119. * This method can actually also be used to find logs for all models or
  120. * even another model. Using no params will return all activities for
  121. * the models it is called from.
  122. *
  123. * Possible params :
  124. * 'model' : mixed (null) String with className, null to get current or false to get everything
  125. * 'action' : string (null) String with action (add/edit/delete), null gets all
  126. * 'order' : string ('created DESC') String with custom order
  127. * 'conditions : array (array()) Add custom conditions
  128. * 'foreign_id' : int (null) Add a int
  129. *
  130. * (remember to use your own user key if you're not using 'user_id')
  131. * 'user_id' : int (null) Defaults to all users, supply id if you want for only one User
  132. *
  133. * @param Object $Model
  134. * @param array $params
  135. * @return array
  136. */
  137. public function findLog(Model $Model, $params = array()) {
  138. $defaults = array(
  139. $this->settings[$Model->alias]['classField'] => null,
  140. 'action' => null,
  141. 'order' => 'created DESC',
  142. $this->settings[$Model->alias]['userKey'] => null,
  143. 'conditions' => array(),
  144. $this->settings[$Model->alias]['foreignKey'] => null,
  145. 'fields' => array(),
  146. 'limit' => 50,
  147. );
  148. $params = array_merge($defaults, $params);
  149. $options = array('order' => $params['order'], 'conditions' => $params['conditions'], 'fields' => $params['fields'], 'limit' => $params['limit']);
  150. if ($params[$this->settings[$Model->alias]['classField']] === null) {
  151. $params[$this->settings[$Model->alias]['classField']] = $Model->alias;
  152. }
  153. if ($params[$this->settings[$Model->alias]['classField']]) {
  154. if ($this->Log->hasField($this->settings[$Model->alias]['classField'])) {
  155. $options['conditions'][$this->settings[$Model->alias]['classField']] = $params[$this->settings[$Model->alias]['classField']];
  156. } elseif ($this->Log->hasField('description')) {
  157. $options['conditions']['description LIKE '] = $params[$this->settings[$Model->alias]['classField']] . '%';
  158. } else {
  159. return false;
  160. }
  161. }
  162. if ($params['action'] && $this->Log->hasField('action')) {
  163. $options['conditions']['action'] = $params['action'];
  164. }
  165. if ($params[$this->settings[$Model->alias]['userKey']] && $this->UserModel && is_numeric($params[$this->settings[$Model->alias]['userKey']])) {
  166. $options['conditions'][$this->settings[$Model->alias]['userKey']] = $params[$this->settings[$Model->alias]['userKey']];
  167. }
  168. if ($params[$this->settings[$Model->alias]['foreignKey']] && is_numeric($params[$this->settings[$Model->alias]['foreignKey']])) {
  169. $options['conditions'][$this->settings[$Model->alias]['foreignKey']] = $params[$this->settings[$Model->alias]['foreignKey']];
  170. }
  171. return $this->Log->find('all', $options);
  172. }
  173. /**
  174. * Get list of actions for one user.
  175. * Params for getting (one line) activity descriptions
  176. * and/or for just one model
  177. *
  178. * @example $this->Model->findUserActions(301, array('model' => 'BookTest'));
  179. * @example $this->Model->findUserActions(301, array('events' => true));
  180. * @example $this->Model->findUserActions(301, array('fields' => array('id','model'),'model' => 'BookTest');
  181. * @param Object $Model
  182. * @param int $user_id
  183. * @param array $params
  184. * @return array
  185. */
  186. public function findUserActions(Model $Model, $user_id, $params = array()) {
  187. if (!$this->UserModel) {
  188. return null;
  189. }
  190. // if logged in user is asking for her own log, use the data we allready have
  191. if (isset($this->user) && isset($this->user[$this->UserModel->alias][$this->UserModel->primaryKey]) && $user_id == $this->user[$this->
  192. UserModel->alias][$this->UserModel->primaryKey] && isset($this->user[$this->UserModel->alias][$this->UserModel->displayField])) {
  193. $username = $this->user[$this->UserModel->alias][$this->UserModel->displayField];
  194. } else {
  195. $this->UserModel->recursive = -1;
  196. $user = $this->UserModel->find('first', array('conditions'=>array($this->UserModel->primaryKey => $user_id)));
  197. $username = $user[$this->UserModel->alias][$this->UserModel->displayField];
  198. }
  199. $fields = array();
  200. if (isset($params['fields'])) {
  201. if (is_array($params['fields'])) {
  202. $fields = $params['fields'];
  203. } else {
  204. $fields = array($params['fields']);
  205. }
  206. }
  207. $conditions = array($this->settings[$Model->alias]['userKey'] => $user_id);
  208. if (isset($params[$this->settings[$Model->alias]['classField']])) {
  209. $conditions[$this->settings[$Model->alias]['classField']] = $params[$this->settings[$Model->alias]['classField']];
  210. }
  211. $data = $this->Log->find('all', array(
  212. 'conditions' => $conditions,
  213. 'recursive' => -1,
  214. 'fields' => $fields
  215. ));
  216. if (! isset($params['events']) || (isset($params['events']) && $params['events'] == false)) {
  217. return $data;
  218. }
  219. $result = array();
  220. foreach ($data as $key => $row) {
  221. $one = $row['Log'];
  222. $result[$key]['Log']['id'] = $one['id'];
  223. $result[$key]['Log']['event'] = $username;
  224. // have all the detail models and change as list :
  225. if (isset($one[$this->settings[$Model->alias]['classField']]) && isset($one['action']) && isset($one['change']) && isset($one[$this->
  226. settings[$Model->alias]['foreignKey']])) {
  227. if ($one['action'] === 'edit') {
  228. $result[$key]['Log']['event'] .= ' edited ' . $one['change'] . ' of ' . strtolower($one[$this->settings[$Model->alias]['classField']]) .
  229. '(id ' . $one[$this->settings[$Model->alias]['foreignKey']] . ')';
  230. // ' at '.$one['created'];
  231. } elseif ($one['action'] === 'add') {
  232. $result[$key]['Log']['event'] .= ' added a ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  233. settings[$Model->alias]['foreignKey']] . ')';
  234. } elseif ($one['action'] === 'delete') {
  235. $result[$key]['Log']['event'] .= ' deleted the ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  236. settings[$Model->alias]['foreignKey']] . ')';
  237. }
  238. } elseif (isset($one[$this->settings[$Model->alias]['classField']]) && isset($one['action']) && isset($one[$this->settings[$Model->alias]['foreignKey']])) { // have model,foreign_id and action
  239. if ($one['action'] === 'edit') {
  240. $result[$key]['Log']['event'] .= ' edited ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  241. settings[$Model->alias]['foreignKey']] . ')';
  242. // ' at '.$one['created'];
  243. } elseif ($one['action'] === 'add') {
  244. $result[$key]['Log']['event'] .= ' added a ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  245. settings[$Model->alias]['foreignKey']] . ')';
  246. } elseif ($one['action'] === 'delete') {
  247. $result[$key]['Log']['event'] .= ' deleted the ' . strtolower($one[$this->settings[$Model->alias]['classField']]) . '(id ' . $one[$this->
  248. settings[$Model->alias]['foreignKey']] . ')';
  249. }
  250. } else { // only description field exist
  251. $result[$key]['Log']['event'] = $one['description'];
  252. }
  253. }
  254. return $result;
  255. }
  256. /**
  257. * Use this to supply a model with the data of the logged in User.
  258. * Intended to be called in AppController::beforeFilter like this :
  259. *
  260. * if ($this->{$this->modelClass}->Behaviors->attached('Logable')) {
  261. * $this->{$this->modelClass}->setUserData($activeUser);/
  262. * }
  263. *
  264. * The $userData array is expected to look like the result of a
  265. * User::find(array('id'=>123));
  266. *
  267. * @param Object $Model
  268. * @param array $userData
  269. */
  270. public function setUserData(Model $Model, $userData = null) {
  271. if ($userData === null && isset($Model->Session)) {
  272. $userData = (array)$Model->Session->read('Auth');
  273. } elseif ($userData === null && class_exists('CakeSession')) {
  274. $userData = (array)CakeSession::read('Auth');
  275. }
  276. if ($userData !== null) {
  277. $this->user = $userData;
  278. }
  279. }
  280. /**
  281. * Used for logging custom actions that arent crud, like login or download.
  282. *
  283. * @example $this->Boat->customLog('ship', 66, array('title' => 'Titanic heads out'));
  284. * @param Object $Model
  285. * @param string $action name of action that is taking place (dont use the crud ones)
  286. * @param int $id id of the logged item (ie foreign_id in logs table)
  287. * @param array $values optional other values for your logs table
  288. */
  289. public function customLog(Model $Model, $action, $id = null, $values = array()) {
  290. $logData['Log'] = $values;
  291. /**
  292. @todo clean up $logData */
  293. if ($id === null) {
  294. $id = $Model->id;
  295. }
  296. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey']) && is_numeric($id)) {
  297. $logData['Log'][$this->settings[$Model->alias]['foreignKey']] = $id;
  298. }
  299. $title = null;
  300. if (isset($values['title'])) {
  301. $title = $values['title'];
  302. unset($logData['Log']['title']);
  303. }
  304. $logData['Log']['action'] = $action;
  305. $this->_saveLog($Model, $logData, $title);
  306. }
  307. public function clearUserData(Model $Model) {
  308. $this->user = null;
  309. }
  310. public function setUserIp(Model $Model, $userIP = null) {
  311. if ($userIP === null) {
  312. //App::uses();
  313. $userIP = CakeRequest::clientIp();
  314. }
  315. $this->userIP = $userIP;
  316. }
  317. public function beforeDelete(Model $Model, $cascade = true) {
  318. $this->setUserData($Model);
  319. if (!$this->settings[$Model->alias]['enabled']) {
  320. return true;
  321. }
  322. if (isset($this->settings[$Model->alias]['skip']['delete']) && $this->settings[$Model->alias]['skip']['delete']) {
  323. return true;
  324. }
  325. $Model->recursive = -1;
  326. $Model->read();
  327. return true;
  328. }
  329. public function afterDelete(Model $Model) {
  330. if (!$this->settings[$Model->alias]['enabled']) {
  331. return true;
  332. }
  333. if (isset($this->settings[$Model->alias]['skip']['delete']) && $this->settings[$Model->alias]['skip']['delete']) {
  334. return true;
  335. }
  336. $logData = array();
  337. if ($this->Log->hasField('description')) {
  338. $logData['Log']['description'] = $Model->alias;
  339. if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
  340. $logData['Log']['description'] .= ' "' . $Model->data[$Model->alias][$Model->displayField] . '"';
  341. }
  342. if ($this->settings[$Model->alias]['descriptionIds']) {
  343. $logData['Log']['description'] .= ' (' . $Model->id . ') ';
  344. }
  345. $logData['Log']['description'] .= __('deleted');
  346. }
  347. $logData['Log']['action'] = 'delete';
  348. $this->_saveLog($Model, $logData);
  349. }
  350. public function beforeValidate(Model $Model) {
  351. if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] !== 'validate') {
  352. return true;
  353. }
  354. $this->_prepareLog($Model);
  355. return true;
  356. }
  357. public function beforeSave(Model $Model) {
  358. if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] !== 'save') {
  359. return true;
  360. }
  361. $this->_prepareLog($Model);
  362. return true;
  363. }
  364. protected function _prepareLog(Model $Model) {
  365. if ($this->user === null) {
  366. $this->setUserData($Model);
  367. }
  368. if ($Model->id && empty($this->old)) {
  369. $this->old = $Model->find('first', array('conditions' => array($Model->primaryKey => $Model->id), 'recursive' => -1));
  370. }
  371. }
  372. public function afterSave(Model $Model, $created) {
  373. if (!$this->settings[$Model->alias]['enabled']) {
  374. return true;
  375. }
  376. if (!empty($this->settings[$Model->alias]['skip']['add']) && $created) {
  377. return true;
  378. } elseif (!empty($this->settings[$Model->alias]['skip']['edit']) && !$created) {
  379. return true;
  380. }
  381. $keys = array_keys($Model->data[$Model->alias]);
  382. $diff = array_diff($keys, $this->settings[$Model->alias]['ignore']);
  383. if (count($diff) === 0 && empty($Model->logableAction)) {
  384. return false;
  385. }
  386. if ($Model->id) {
  387. $id = $Model->id;
  388. } elseif ($Model->insertId) {
  389. $id = $Model->insertId;
  390. }
  391. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey'])) {
  392. $logData['Log'][$this->settings[$Model->alias]['foreignKey']] = $id;
  393. }
  394. if ($this->Log->hasField('description')) {
  395. $logData['Log']['description'] = $Model->alias . ' ';
  396. if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
  397. $logData['Log']['description'] .= '"' . $Model->data[$Model->alias][$Model->displayField] . '" ';
  398. }
  399. if ($this->settings[$Model->alias]['descriptionIds']) {
  400. $logData['Log']['description'] .= '(' . $id . ') ';
  401. }
  402. if ($created) {
  403. $logData['Log']['description'] .= __('added');
  404. } else {
  405. $logData['Log']['description'] .= __('updated');
  406. }
  407. }
  408. if ($this->Log->hasField('action')) {
  409. if ($created) {
  410. $logData['Log']['action'] = 'add';
  411. } else {
  412. $logData['Log']['action'] = 'edit';
  413. }
  414. }
  415. if ($this->Log->hasField('change')) {
  416. $logData['Log']['change'] = '';
  417. $db_fields = array_keys($Model->schema());
  418. $changed_fields = array();
  419. foreach ($Model->data[$Model->alias] as $key => $value) {
  420. if (isset($Model->data[$Model->alias][$Model->primaryKey]) && !empty($this->old) && isset($this->old[$Model->alias][$key])) {
  421. $old = $this->old[$Model->alias][$key];
  422. } else {
  423. $old = '';
  424. }
  425. if ($key !== 'modified' && !in_array($key, $this->settings[$Model->alias]['ignore']) && $value != $old && in_array($key, $db_fields)) {
  426. if ($this->settings[$Model->alias]['change'] === 'full') {
  427. $changed_fields[] = $key . ' (' . $old . ') => (' . $value . ')';
  428. } elseif ($this->settings[$Model->alias]['change'] === 'serialize') {
  429. $changed_fields[$key] = array('old' => $old, 'value' => $value);
  430. } else {
  431. $changed_fields[] = $key;
  432. }
  433. }
  434. }
  435. $changes = count($changed_fields);
  436. if (!$changes) {
  437. return true;
  438. }
  439. if ($this->settings[$Model->alias]['change'] === 'serialize') {
  440. $logData['Log']['change'] = serialize($changed_fields);
  441. } else {
  442. $logData['Log']['change'] = implode(', ', $changed_fields);
  443. }
  444. $logData['Log']['changes'] = $changes;
  445. }
  446. if (empty($logData)) {
  447. return true;
  448. }
  449. return $this->_saveLog($Model, $logData);
  450. }
  451. /**
  452. * Does the actual saving of the Log model. Also adds the special field if possible.
  453. *
  454. * If model field in table, add the Model->alias
  455. * If action field is NOT in table, remove it from dataset
  456. * If the userKey field in table, add it to dataset
  457. * If userData is supplied to model, add it to the title
  458. *
  459. * @param Object $Model
  460. * @param array $logData
  461. * @return void
  462. */
  463. public function _saveLog(Model $Model, $logData, $title = null) {
  464. if ($title !== null) {
  465. $logData['Log']['title'] = $title;
  466. } elseif ($Model->displayField == $Model->primaryKey) {
  467. $logData['Log']['title'] = $Model->alias . ' (' . $Model->id . ')';
  468. } elseif (isset($Model->data[$Model->alias][$Model->displayField])) {
  469. $logData['Log']['title'] = $Model->data[$Model->alias][$Model->displayField];
  470. } else {
  471. $Model->recursive = -1;
  472. $Model->read(array($Model->displayField));
  473. $logData['Log']['title'] = $Model->data[$Model->alias][$Model->displayField];
  474. }
  475. if ($this->Log->hasField($this->settings[$Model->alias]['classField'])) {
  476. // by miha nahtigal
  477. $logData['Log'][$this->settings[$Model->alias]['classField']] = $Model->name;
  478. }
  479. if ($this->Log->hasField($this->settings[$Model->alias]['foreignKey']) && !isset($logData['Log'][$this->settings[$Model->alias]['foreignKey']])) {
  480. if ($Model->id) {
  481. $logData['Log'][$this->settings[$Model->alias]['foreignKey']] = $Model->id;
  482. } elseif ($Model->insertId) {
  483. $logData['Log'][$this->settings[$Model->alias]['foreignKey']] = $Model->insertId;
  484. }
  485. }
  486. if (!$this->Log->hasField('action')) {
  487. unset($logData['Log']['action']);
  488. } elseif (isset($Model->logableAction) && !empty($Model->logableAction)) {
  489. $logData['Log']['action'] = implode(',', $Model->logableAction); // . ' ' . $logData['Log']['action'];
  490. unset($Model->logableAction);
  491. }
  492. if ($this->Log->hasField('version_id') && isset($Model->version_id)) {
  493. $logData['Log']['version_id'] = $Model->version_id;
  494. unset($Model->version_id);
  495. }
  496. if ($this->Log->hasField('ip') && $this->userIP) {
  497. $logData['Log']['ip'] = $this->userIP;
  498. }
  499. if ($this->Log->hasField($this->settings[$Model->alias]['userKey']) && $this->user && isset($this->user[$this->UserModel->alias])) {
  500. $logData['Log'][$this->settings[$Model->alias]['userKey']] = $this->user[$this->UserModel->alias][$this->UserModel->primaryKey];
  501. }
  502. if ($this->Log->hasField('description')) {
  503. if (empty($logData['Log']['description'])) {
  504. $logData['Log']['description'] = __('Custom action');
  505. }
  506. if ($this->user && $this->UserModel && isset($this->user[$this->UserModel->alias])) {
  507. $logData['Log']['description'] .= ' ' . __('by') . ' ' . $this->settings[$Model->alias]['userModel'] . ' "' . $this->user[$this->UserModel->alias][$this->UserModel->displayField] . '"';
  508. if ($this->settings[$Model->alias]['descriptionIds']) {
  509. $logData['Log']['description'] .= ' (' . $this->user[$this->UserModel->alias][$this->UserModel->primaryKey] . ')';
  510. }
  511. } else {
  512. // UserModel is active, but the data hasnt been set. Assume system action.
  513. $logData['Log']['description'] .= __(' by System');
  514. }
  515. $logData['Log']['description'] .= '.';
  516. }
  517. $this->Log->create($logData);
  518. $this->Log->save(null, false);
  519. }
  520. }