LogableBehavior.php 21 KB

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