Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_decrement_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 decrement hit count due to database error: '.$e->getMessage());
77  }
78 
79  // if there is already a entry in the DB we will just decrement the hit count
80  if (!empty($asset_hit_count)) {
81  if (isset($asset_hit_count[0]['hitcount'])) {
82  if ((int) $asset_hit_count[0]['hitcount'] == 1) {
83  $deleterow = TRUE;
84  } else {
85  $deleterow = FALSE;
86  $new_hit_count = (int) $asset_hit_count[0]['hitcount'];
87  $new_hit_count--;
88  // we update the entry in the DB
89  try {
90  $bind_vars = Array(
91  'assetid' => (string) $state['assetid'],
92  'hitcount' => $new_hit_count,
93  );
94  $result = MatrixDAL::executeQuery('data_package', 'updateHitCount', $bind_vars);
95  } catch (Exception $e) {
96  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
97  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
98  throw new Exception('Unable to decrement hit count due to database error: '.$e->getMessage());
99  }
100  }
101  } else {
102  // error
103  }
104  }
105 
106  if (isset($deleterow) && $deleterow) {
107  // if the hit count is equal to 1, delete the row
108  try {
109  $bind_vars = Array('assetid' => (string) $state['assetid']);
110  $result = MatrixDAL::executeQuery('data_package', 'deleteHitCount', $bind_vars);
111  } catch (Exception $e) {
112  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
113  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
114  throw new Exception('Unable to decrement hit count due to database error: '.$e->getMessage());
115  }
116  }
117 
118  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
119  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
120 
121  }//end execute()
122 
123 
134  public static function getInterface($settings, $prefix, $write_access=FALSE)
135  {
136  // This trigger does not require any customisation from the details screen.
137  return FALSE;
138 
139  }//end getInterface()
140 
141 
153  public static function processInterface(&$settings, $request_data)
154  {
155  // This trigger does not use any settings, so we do not need to save.
156  return FALSE;
157 
158  }//end processInterface()
159 
160 
170  public static function getLocks($settings, &$state)
171  {
172  return Array($state['assetid'] => Array('lookups'));
173 
174  }//end getLocks()
175 
176 
177 }//end class
178 
179 ?>