Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_regenerate_design.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
33 {
34 
35 
41  function HIPO_Job_Regenerate_Design($code_name='')
42  {
43  $this->HIPO_Job($code_name);
44 
45  }//end constructor
46 
47 
57  public static function paintConfig(&$o, $class, $write_access)
58  {
59  // design regeneration threshhold HIPO config entry
60  $o->openField('Hipo Job Regenerate Design');
61 
62  if ($write_access) {
63  text_box($class.'[SQ_HIPO_REGENERATE_DESIGN_THRESHOLD]', SQ_HIPO_REGENERATE_DESIGN_THRESHOLD, 1);
64  } else {
65  echo SQ_HIPO_REGENERATE_DESIGN_THRESHOLD;
66  }
67  echo ' '.translate('assets');
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array(
83  'SQ_HIPO_REGENERATE_DESIGN_THRESHOLD' => Array('editable' => 1, 'default' => 1),
84  );
85 
86  }//end getConfigVars()
87 
88 
100  function getCodeName()
101  {
102  $code_name = parent::getCodeName();
103  return $code_name.'-'.md5($this->_running_vars['assetid']);
104 
105  }//end getCodeName()
106 
107 
114  function getHipoName()
115  {
116  return 'Hipo Job Regenerate Design';
117 
118  }//end getHipoName()
119 
120 
129  {
130  return Array(
131  Array(
132  'name' => 'Hipo Job Regenerate Design',
133  'function_call' => Array(
134  'process_function' => 'processCustomisation',
135  ),
136  'running_mode' => 'web',
137  'auto_step' => true,
138  'allow_cancel' => true,
139  'percent_done' => 0,
140  'complete' => false,
141  'message' => '',
142  ),
143  );
144 
145  }//end getInitialStepData()
146 
147 
148 
149 
150 
157  function freestyle()
158  {
159  while (!empty($this->_running_vars['todo_assetids'])) {
160  if (!$this->processCustomisation($this->_steps[0], get_class($this))) {
161  return false;
162  }
163  }
164  return true;
165 
166  }//end freestyle()
167 
168 
175  function prepare()
176  {
177  $parentid = isset($this->_running_vars['parentid']) ? $this->_running_vars['parentid'] : NULL;
178  $assetid = isset($this->_running_vars['assetid']) ? $this->_running_vars['assetid'] : NULL;
179 
180  if (empty($assetid)) {
181  trigger_error('Unable to run HIPO job; this job requires a design asset ID', E_USER_WARNING);
182  return FALSE;
183  }
184  $this->_running_vars['todo_assetids'] = Array();
185  $this->_running_vars['total_count'] = 0;
186  $all_customisations = Array();
187  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
188  $type = $asset->type();
189 
190  // first of first, generate parse file for itself
191  if ($type === 'design_customisation' || $type === 'design_css_customisation') {
192  if(empty ($parentid)) {
193  trigger_error('To regenerate a design customisation, parentid is required', E_USER_WARNING);
194  return FALSE;
195  }
196  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($parentid);
197  $asset->updateFromParent($parent, FALSE);
198  }
199  else {
200  $asset->generateDesignFile(FALSE);
201  }
202 
203 
204  // get all children customisation to do
205  $all_customisations = $this->_getCustomisations($assetid);
206  $this->_running_vars['total_count'] += count($all_customisations);
207  $this->_running_vars['todo_assetids'] = array_merge($this->_running_vars['todo_assetids'], $all_customisations);
208  $this->_running_vars['done_assetids'] = Array();
209 
210  return parent::prepare();
211 
212  }//end prepare()
213 
214 
224  function processCustomisation(&$step_data, $prefix)
225  {
226  if (!empty($this->_running_vars['todo_assetids'])) {
227  $current_customisation = array_shift($this->_running_vars['todo_assetids']);
228 
229  $customisation = $GLOBALS['SQ_SYSTEM']->am->getAsset($current_customisation['child']);
230  $design = $GLOBALS['SQ_SYSTEM']->am->getAsset($current_customisation['parent']);
231  if ($acquired = $GLOBALS['SQ_SYSTEM']->am->acquireLock($customisation->id, 'all')) {
232 
233  // update individual customisation, but don't pass on
234  $customisation->updateFromParent($design, FALSE);
235 
236  $this->_running_vars['done_assetids'][] = $current_customisation;
237  if ($acquired != 2) {
238  $GLOBALS['SQ_SYSTEM']->am->releaseLock($customisation->id, 'all');
239  }
240  }
241 
242 
243  }
244 
245  if (empty($this->_running_vars['todo_assetids'])) {
246  $step_data['percent_done'] = 100;
247  $step_data['complete'] = true;
248  } else {
249  $step_data['percent_done'] = (count($this->_running_vars['done_assetids']) / $this->_running_vars['total_count']) * 100;
250  $step_data['complete'] = false;
251  }
252 
253  return true;
254 
255  }//end processCustomisation()
256 
257 
266  private function _getCustomisations ($designid) {
267  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($designid, SQ_LINK_TYPE_2, 'design_customisation', TRUE, 'major', 'customisation');
268  $customisation_links = array_merge($customisation_links, $GLOBALS['SQ_SYSTEM']->am->getLinks($designid, SQ_LINK_TYPE_2, 'design_css_customisation', TRUE, 'major', 'customisation'));
269  if (empty($customisation_links)) return Array();
270 
271  $children_customisations = Array();
272  foreach ($customisation_links as $link) {
273  $children_customisations[] = Array('parent' => $designid, 'child' => $link['minorid']);
274  $children_customisations = array_merge($children_customisations, $this->_getCustomisations($link['minorid']));
275  }
276  return $children_customisations;
277  }
278 }//end class
279 
280 ?>