Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_increment_hit_count.inc
1 <?php
16 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 
32 {
33 
34 
54  public static function execute($settings, &$state)
55  {
56  if (empty($state['asset'])) {
57  // grab the asset if assetid is given, but not the asset.
58  if (empty($state['assetid'])) {
59  return FALSE;
60  } else {
61  $state['asset'] = &$GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
62  }
63  }
64  if (is_null($state['asset'])) return FALSE;
65 
66  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
67  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
68 
69  // we need to check if there is already an entry in the DB
70  try {
71  $bind_vars = Array('assetid' => (string) $state['assetid']);
72  $asset_hit_count = MatrixDAL::executeAll('data_package', 'getHitCount', $bind_vars);
73  } catch (Exception $e) {
74  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
75  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
76  throw new Exception('Unable to increment hit count due to database error: '.$e->getMessage());
77  }
78 
79  // if there is already a entry in the DB we will just increment the hit count
80  if (!empty($asset_hit_count)) {
81  if (isset($asset_hit_count[0]['hitcount'])) {
82  $new_hit_count = (int) $asset_hit_count[0]['hitcount'];
83  $new_hit_count++;
84  // we update the entry in the DB
85  try {
86  $bind_vars = Array(
87  'assetid' => (string) $state['assetid'],
88  'hitcount' => $new_hit_count,
89  );
90  $result = MatrixDAL::executeQuery('data_package', 'updateHitCount', $bind_vars);
91  } catch (Exception $e) {
92  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
93  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
94  throw new Exception('Unable to increment hit count due to database error: '.$e->getMessage());
95  }
96  } else {
97  // error
98  }
99  } else {
100  // we insert a new entry in the DB
101  try {
102  $bind_vars = Array(
103  'assetid' => (string) $state['assetid'],
104  'hitcount' => 1,
105  );
106  $result = MatrixDAL::executeQuery('data_package', 'insertHitCount', $bind_vars);
107  } catch (Exception $e) {
108  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
109  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
110  // Bug Fix #3995 race condition in hit count
111  // Fail silently if we cannot add a new entry to the database
112  return TRUE;
113  }
114  }
115 
116  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
117  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
118 
119  }//end execute()
120 
121 
132  public static function getInterface($settings, $prefix, $write_access=FALSE)
133  {
134  // This trigger does not require any customisation from the details screen.
135  return FALSE;
136 
137  }//end getInterface()
138 
139 
151  public static function processInterface(&$settings, $request_data)
152  {
153  // This trigger does not use any settings, so we do not need to save.
154  return FALSE;
155 
156  }//end processInterface()
157 
158 
168  public static function getLocks($settings, &$state)
169  {
170  return Array($state['assetid'] => Array('lookups'));
171 
172  }//end getLocks()
173 
174 
175 }//end class
176 
177 ?>