Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
promotional_code.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Promotional_Code extends Asset
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _preCreateCheck(Array &$link)
60  {
61  if (!parent::_preCreateCheck($link)) return FALSE;
62 
63  $name = trim($this->attr('name'));
64  if ($name == '') {
65  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
66  return FALSE;
67  }
68 
69  return TRUE;
70 
71  }//end _preCreateCheck()
72 
73 
81  public function _getAllowedLinks()
82  {
83  $allowed = parent::_getAllowedLinks();
84  $allowed[SQ_LINK_NOTICE]['asset'] = Array('card' => 'M', 'exclusive' => FALSE);
85 
86  return $allowed;
87 
88  }//end _getAllowedLinks()
89 
90 
100  protected function _getName($short_name=FALSE)
101  {
102  return $this->attr('name');
103 
104  }//end _getName()
105 
106 
121  public function saveAttributes($dont_run_updated=FALSE)
122  {
123  // If the promotional code is being changed, update the link values of the links between this promotional code asset and the applied assets
124  if (isset($this->_tmp['vars_set']['code']['old_value'])) {
125  $old_link_value = $this->_tmp['vars_set']['code']['old_value'];
126  $new_link_value = $this->attr('code');
127  $am = $GLOBALS['SQ_SYSTEM']->am;
128  $links = $am->getLinks($this->id, SQ_LINK_NOTICE, '', FALSE, 'major', $old_link_value);
129 
130  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
131  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
132 
133  try {
134  foreach ($links as $link) {
135  if (!$am->updateLink($link['linkid'], $link['link_type'], $new_link_value)) {
136  throw new Exception("The value of the notice link #{$link['linkid']} could not be updated to $new_link_value");
137  }
138  }
139 
140  if (!parent::saveAttributes($dont_run_updated)) {
141  throw new Exception('The attributes could not be saved');
142  }
143 
144  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
145  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
146 
147  return TRUE;
148 
149  } catch (Exception $e) {
150  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
151  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
152  throw $e;
153  }
154 
155  } else {
156  return parent::saveAttributes($dont_run_updated);
157  }
158 
159  }//end saveAttributes()
160 
161 
172  public function getOldAttrValue($name)
173  {
174  if (isset($this->_tmp['vars_set'][$name]['old_value'])) {
175  $value = $this->_tmp['vars_set'][$name]['old_value'];
176  } else {
177  $value = $this->attr('code');
178  }
179 
180  return $value;
181 
182  }//end getOldAttrValue()
183 
184 
191  public function getRules()
192  {
193  return $this->attr('rules');
194 
195  }//end getRules()
196 
197 
206  public function process(&$asset_data)
207  {
208  $rules = $this->attr('rules');
209  $am = $GLOBALS['SQ_SYSTEM']->am;
210 
211  foreach ($rules as $rule) {
212  $rule_type_code = $rule['rule_type_code'];
213  $am->includeAsset($rule_type_code);
214  $rule_asset = new $rule_type_code();
215  $rule_asset->evaluate($asset_data, $rule['rule_data']);
216  }
217 
218  }//end process()
219 
220 
221 }//end class
222 
223 ?>