Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_create_design_customisation.inc
1 <?php
15 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
16 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
17 
30 {
31 
32 
51  public static function execute($settings, &$state)
52  {
53 
54  if (empty($state['asset'])) {
55  if (empty($state['assetid'])) {
56  return FALSE;
57  } else {
58  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
59  }
60  }
61 
62  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
63 
64  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
65  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
66 
67  // This clone will be morphed to customisation later, so it will NOT clone the parse file.
68  $asset->_tmp['custom'] = TRUE;
69  $clone = $GLOBALS['SQ_SYSTEM']->am->cloneAsset($asset, $create_link, $map, Array('attributes', 'permissions', 'roles'), FALSE);
70  if (is_null($clone)) {
71  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
72  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
73  return FALSE;
74  }
75 
76  $default_name = 'Customisation #'.$clone->id;
77  $new_name = array_get_index($settings, 'customisation_name', $default_name);
78 
79  $keywords_list = retrieve_keywords_replacements($new_name);
80  $keywords_list = array_unique($keywords_list);
81  $replacements = Array();
82  foreach ($keywords_list as $keyword) {
83  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
84  }
85  $new_name = replace_keywords($new_name, $replacements);
86 
87  // Acquire a lock on the new object
88  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($clone->id, 'all')) {
89  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
90  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
91  return FALSE;
92  }
93 
94 
95  // Now let's morph the dupe into a design customisation
96  // since 3.18, morph() returns the result asset instead of a boolean
97  $customisation = $clone->morph('design_customisation');
98 
99 
100 
101  if (!$customisation) {
102  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
103  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
104  return FALSE;
105  }
106 
107  // and link it back to ourselves
108  if (!$asset->createLink($customisation, SQ_LINK_TYPE_2, 'customisation', NULL, '1')) {
109  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
110  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
111  return FALSE;
112  }
113  if (!($customisation->setAttrValue('id_name', $new_name) && $customisation->saveAttributes())) {
114  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
115  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
116  return FALSE;
117  }
118 
119  $am = $GLOBALS['SQ_SYSTEM']->am;
120 
121  // Now create the links to our design areas
122  $da_links = $asset->getDesignAreaLink();
123  foreach ($da_links as $link) {
124  $da = $am->getAsset($link['minorid'], $link['minor_type_code'], TRUE);
125  if (is_null($da)) continue;
126  if (!$customisation->createLink($da, SQ_LINK_TYPE_3, $link['value'], NULL, '1')) {
127  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
128  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
129  return FALSE;
130  }
131  }//end foreach
132 
133  // set an initial web path
134  $initial_path = strtolower($new_name);
135  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
136  $valid_paths = make_valid_web_paths(Array($initial_path));
137  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($asset, $valid_paths, $customisation->id, TRUE);
138  if (!$customisation->saveWebPaths($good_paths)) {
139  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
140  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
141  return FALSE;
142  }
143 
144  // link up all the files from this design to the customisation
145  $file_link_ids = Array();
146  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
147  foreach ($file_links as $link) {
148  $minor = $am->getAsset($link['minorid'], $link['minor_type_code']);
149  if (!$customisation->createLink($minor, SQ_LINK_TYPE_3, '', NULL, 1)) {
150  trigger_localised_error('CORE0162', E_USER_WARNING);
151  $am->forgetAsset($minor);
152  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
153  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
154  return FALSE;
155  }
156  }
157  $am->forgetAsset($minor);
158 
159  // link up all the css files from this design to the customisation
160  $css_link_ids = Array();
161  $css_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'design_css', FALSE);
162  foreach ($css_links as $link) {
163  $minor = $am->getAsset($link['minorid'], $link['minor_type_code']);
164  if (!$customisation->createLink($minor, SQ_LINK_TYPE_3, '', NULL, 1)) {
165  trigger_localised_error('CORE0161', E_USER_WARNING);
166  $am->forgetAsset($minor);
167  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
168  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
169  return FALSE;
170  }
171  }
172  $am->forgetAsset($minor);
173 
174  // release the lock on the new object
175  if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($customisation->id, 'all')) {
176  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
177  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
178  return FALSE;
179  }
180 
181  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
182  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
183 
184  return Array(
185  'assetid' => $customisation->id,
186  'original_design' => $asset->id,
187  );
188 
189  }//end execute()
190 
191 
202  public static function getInterface($settings, $prefix, $write_access=FALSE)
203  {
204  // How to get a list of keyword if there is no asset to get it from?
205  $GLOBALS['SQ_SYSTEM']->am->includeAsset('design');
206  $tmp_design = new Design();
207  $keywords = $tmp_design->getAvailableKeywords();
208  ob_start();
209  text_box($prefix.'[customisation_name]', '%asset_name% - Customisation', strlen('%asset_name% - Customisation'));
210  $text_box = ob_get_contents();
211  ob_end_clean();
212 
213  ob_start();
214  combo_box($prefix.'[keywords]', $keywords, FALSE, '',0, 'onchange="document.getElementById(\''.$prefix.'[customisation_name]\').value += \' %\' + this.value + \'%\';"');
215  $combo_box = ob_get_contents();
216  ob_end_clean();
217 
218  $contents = translate('trigger_create_design_customisation_usage')
219  .'<br /><br />'.translate('trigger_create_design_customisation_select_keyword').$combo_box
220  .'<br /><br />'.
221  translate('trigger_create_design_customisation_specify_name').$text_box.'<br />'
222  ;
223 
224  return $contents;
225 
226  }//end getInterface()
227 
228 
240  public static function processInterface(&$settings, $request_data)
241  {
242  // make sure the assetid isn't blank
243  if (empty($request_data['customisation_name'])) {
244  return translate('trigger_create_design_customisation_name_missing');
245  }
246 
247  $settings['customisation_name'] = $request_data['customisation_name'];
248 
249  return FALSE;
250 
251  }//end processInterface()
252 
253 
263  public static function getLocks($settings, &$state)
264  {
265  return Array (
266  $state['assetid'] => Array('all'),
267  );
268 
269  }//end getLocks()
270 
271 
272 }//end class
273 
274 
275 ?>