Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_design_associated_files.inc
1 <?php
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
20 
21 
34 {
35 
36 
54  public static function execute($settings, &$state)
55  {
56  // check settings, state
57  if (empty($settings['file_path']) || !file_exists($settings['file_path'])) {
58  // if no settings, fail
59  return FALSE;
60  }
61 
62  // state, design asset
63  if (empty($state['asset'])) {
64  // grab the asset if assetid is given, but not the asset.
65  if (empty($state['assetid'])) {
66  return FALSE;
67  } else {
68  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
69  }
70  }
71 
72  if (is_null($state['asset'])) return FALSE;
73 
74  $asset = $state['asset'];
75  $edit_fns = $asset->getEditFns();
76 
77  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
78 
79  $changes = FALSE;
80 
81  // if this is a tar archive, extract it
82  if (preg_match('/\\.tar\\.gz$/', $settings['file_path'])
83  || preg_match('/\\.tgz$/', $settings['file_path'])
84  || preg_match('/\\.tar$/', $settings['file_path']) ) {
85 
86  require_once 'Archive/Tar.php';
87  $tar_ball = new Archive_Tar($settings['file_path']);
88  if (($contents = $tar_ball->listContent()) != 0) {
89 
90  // Basically we want to make sure that all the files in the
91  // tar ball end up in a single directory, so we have to mess about with the extracting a bit
92  $extract_list = Array();
93  foreach ($contents as $entry) {
94  if ($entry['typeflag'] != '0' && $entry['typeflag'] != '') {
95  continue;
96  }
97 
98  $k = dirname($entry['filename']);
99  if (!isset($extract_list[$k])) {
100  $extract_list[$k] = Array();
101  }
102  $extract_list[$k][] = $entry['filename'];
103 
104  }//end foreach
105 
106  foreach ($extract_list as $remove_path => $files) {
107  // extract the files from the tar archive to a temporary directory
108  $files_dir = $asset->data_path.'/.temp_files';
109  if (!create_directory($files_dir)) return FALSE;
110  if (!clear_directory($files_dir)) return FALSE;
111 
112  $result = $tar_ball->extractList($files, $files_dir, $remove_path);
113  if (!$result) {
114  trigger_error('Failed to extract tar archive', E_USER_WARNING);
115  break;
116  } else {
117  $GLOBALS['SQ_SYSTEM']->am->includeAsset('file');
118  $design_link = Array('asset' => &$asset, 'link_type' => SQ_LINK_TYPE_2, 'value' => '', 'sort_order' => 1, 'is_dependant' => 1);
119 
120  foreach ($files as $filename) {
121  $filename = preg_replace('|^'.$remove_path.'/|', '', $filename);
122  $temp_path = $files_dir.'/'.$filename;
123  $temp_info = Array('name' => $filename, 'tmp_name' => $temp_path);
124  $temp_info['non_uploaded_file'] = TRUE;
125  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
126  if ($edit_fns->_processUploadedFile($asset, $temp_info)) {
127  $changes = TRUE;
128  }
129  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
130  }
131  }
132  delete_directory($files_dir);
133 
134  }//end foreach
135 
136  }//end if
137 
138  // just create the file asset
139  } else {
140  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
141  if ($edit_fns->_processUploadedFile($asset, $info)) {
142  $changes = TRUE;
143  }
144  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
145 
146  }//end if tar archive
147 
148  return Array(
149  'assetid' => $state['asset']->id,
150  );
151 
152  }//end execute()
153 
154 
165  public static function getInterface($settings, $prefix, $write_access=FALSE)
166  {
167  $file_path = array_get_index($settings, 'file_path', '');
168  ob_start();
169  ?>
170  <table border="0">
171  <tr>
172  <td valign="top"><b><?php echo 'Path to the parse file'; ?></b></td>
173  <td valign="top" style="padding-bottom: 10px;"><?php
174  // level
175  if ($write_access) {
176  echo text_box($prefix.'[file_path]', $file_path, '50');
177  } else {
178  echo $file_path.'&nbsp;';
179  }
180  ?>
181  </td>
182  </tr>
183  </table>
184  <?php
185 
186  return ob_get_clean();
187 
188  }//end getInterface()
189 
190 
202  public static function processInterface(&$settings, $request_data)
203  {
204  $file_path = array_get_index($request_data, 'file_path', '');
205  $file_path = trim($file_path);
206  if ($file_path == '') {
207  return 'file path is not specified.';
208  }
209  $settings['file_path'] = $file_path;
210 
211  return FALSE;
212 
213  }//end processInterface()
214 
215 
225  public static function getLocks($settings, &$state)
226  {
227  return Array(
228  $state['assetid'] => Array(
229  'attributes',
230  ),
231  );
232 
233  }//end getLocks()
234 
235 
236 }//end class
237 
238 ?>