Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_css_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design/design_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42  $this->static_screens['details']['force_unlock'] = FALSE;
43 
44  }//end constructor
45 
46 
56  public function _parseString(Asset $asset, $file_contents)
57  {
58  // Let's place an print operation at the top of the file
59  // to allow us to print the default header stuff like
60  // copyright and content-type information
61  $file_contents= '<'.$this->tag_name."_PRINT id_name='__global__' var='css_header' />\n"
62  .$file_contents;
63 
64  return parent::_parseString($asset, $file_contents);
65 
66  }//end _parseString()
67 
68 
78  public function _processUploadedFile(Asset $asset, Array $info=Array())
79  {
80  // if we already have a file with the same name TYPE_3 linked to us,
81  // delete this link so we link up this new file
82  $existing_ids = Array();
83  $existing = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_3, 'file', FALSE);
84  foreach ($existing as $link) {
85  $existing_ids[$link['minorid']] = $link['linkid'];
86  }
87 
88  $existing_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($existing_ids));
89  foreach ($existing_info as $assetid => $existing_info) {
90  // if the name is the same, delete the link
91  if ($existing_info['name'] == $info['name']) {
92  $linkid = $existing_ids[$assetid];
93  $asset->deleteExistingLink($linkid);
94  $asset->updateLookups();
95  }
96  }
97 
98  return parent::_processUploadedFile($asset, $info);
99 
100  }//end _processUploadedFile()
101 
102 
113  public function paintNewCustomisation(Design_CSS $asset, Backend_Outputter $o, $prefix)
114  {
115  if (!$asset->writeAccess('links')) return FALSE;
116  check_box($prefix.'_new_customisation');
117  text_box($prefix.'_new_customisation_name', $asset->name.'.css', 40);
118  return TRUE;
119 
120  }//end paintNewCustomisation()
121 
122 
133  public function processNewCustomisation(Design_CSS $asset, Backend_Outputter $o, $prefix)
134  {
135  if (empty($_POST[$prefix.'_new_customisation']) || !($new_name = trim($_POST[$prefix.'_new_customisation_name']))) {
136  return FALSE;
137  }
138 
139  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
140  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
141 
142  // First let's duplicate ourselves, ignoring dependants and the directory with the files
143  $map = Array();
144  $create_link = Array();
145 
146  // increase run level to allow setting of components without cloning permissions from parents
147  if ($asset->writeAccess('all')) {
148  $GLOBALS['SQ_SYSTEM']->setRunLevel($GLOBALS['SQ_SYSTEM']->getRunLevel() & SQ_RUN_LEVEL_FORCED);
149  // This clone will be morphed to customisation later, so it will NOT clone the parse file.
150  $asset->_tmp['custom'] = TRUE;
151  $clone = $GLOBALS['SQ_SYSTEM']->am->cloneAsset($asset, $create_link, $map, Array('attributes', 'permissions', 'roles'), FALSE);
152  unset ($asset->_tmp['custom']);
153  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
154  }
155 
156 
157 
158  if (is_null($clone)) {
159  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
160  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
161  return FALSE;
162  }
163 
164  $GLOBALS['SQ_SYSTEM']->am->rememberAsset($da);
165 
166  // Acquire a lock on the new object
167  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($clone->id, 'all')) {
168  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
169  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
170  return FALSE;
171  }
172 
173  // Now let's morph the dupe into a design customisation
174  $clone = $clone->morph('design_css_customisation');
175 
176  if (!$clone) {
177  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
178  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
179  return FALSE;
180  }
181 
182  // and link it back to ourselves
183  if (!$asset->createLink($clone, SQ_LINK_TYPE_2, 'customisation', NULL, '1')) {
184  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
185  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
186  return FALSE;
187  }
188  if (!($clone->setAttrValue('id_name', $new_name) && $clone->saveAttributes())) {
189  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
190  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
191  return FALSE;
192  }
193 
194  $am = $GLOBALS['SQ_SYSTEM']->am;
195 
196  // Now create the links to our design areas
197  $da_links = $asset->getDesignAreaLink();
198  foreach ($da_links as $link) {
199  $da = $am->getAsset($link['minorid'], $link['minor_type_code'], TRUE);
200  if (is_null($da)) continue;
201  if (!$clone->createLink($da, SQ_LINK_TYPE_3, $link['value'], NULL, '1')) {
202  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
203  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
204  return FALSE;
205  }
206  }// end foreach
207 
208  // set an initial web path
209  $initial_path = strtolower($new_name);
210  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
211  $valid_paths = make_valid_web_paths(Array($initial_path));
212  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($asset, $valid_paths, $clone->id, TRUE);
213  if (!$clone->saveWebPaths($good_paths)) {
214  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
215  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
216  return FALSE;
217  }
218 
219  // link up all the files from this design to the customisation
220  $file_link_ids = Array();
221  $file_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2 | SQ_LINK_TYPE_3, 'file', FALSE);
222  foreach ($file_links as $link) {
223  $minor = $am->getAsset($link['minorid'], $link['minor_type_code']);
224  if (!$clone->createLink($minor, SQ_LINK_TYPE_3, '', NULL, 1)) {
225  trigger_localised_error('CORE0162', E_USER_WARNING);
226  $am->forgetAsset($minor);
227  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
228  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
229  return FALSE;
230  }
231  }
232  $am->forgetAsset($minor);
233 
234  // release the lock on the new object
235  if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($clone->id, 'all')) {
236  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
237  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
238  return FALSE;
239  }
240 
241  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
242  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
243 
244  return TRUE;
245 
246  }//end processNewCustomisation()
247 
248 
259  public function paintCurrentCustomisations(Design_CSS $asset, Backend_Outputter $o, $prefix)
260  {
261  $customisation_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'design_css_customisation', TRUE, 'major', 'customisation');
262  if ($customisation_links) {
263  $am = $GLOBALS['SQ_SYSTEM']->am;
264  foreach ($customisation_links as $link) {
265  $customisation = $am->getAsset($link['minorid'], $link['minor_type_code']);
266  if (is_null($customisation)) continue;
267  echo '# ', $customisation->id, '&nbsp;&nbsp;<a href="', $customisation->getBackendHref(), '">', $customisation->name, '</a><br>';
268 
269  }
270 
271  } else {
272  echo translate('core_no_customisation_created');
273  }
274 
275  }//end paintCurrentCustomisations()
276 
277 
278 }//end class
279 ?>