Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
asset_listing_change_metadata_sort_option.php
1 <?php
21  require '../core/include/init.inc';
22 
23  $verbose = FALSE;
24 
25  $root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
26  $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
27  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
28 
29 
30  $am =& $GLOBALS['SQ_SYSTEM']->am;
31 
32  $db = NULL;
33  // PHP 5
34  if (version_compare(phpversion(), 5) >= 0) {
35  $db = MatrixDAL::getDb();
36  } else {
37  $db =& $GLOBALS['SQ_SYSTEM']->db;
38  }
39 
40 
41  // get the attribute id
42  $select_attribute = 'select attrid from sq_ast_attr where type_code = \'page_asset_listing\' and name = \'metadata_sort_type\'';
43 
44  $attribute_id = NULL;
45 
46  // PHP 5
47  if (version_compare(phpversion(), 5) >= 0) {
48  $query = $db->prepare($select_attribute);
49  try {
50  $attribute_id = MatrixDAL::executePdoOne($query);
51  } catch (Exception $e) {
52  throw new Exception($e.getMessage());
53  }//end
54  } else {
55  $attribute_id = $db->getOne($select_attribute);
56  assert_valid_db_result($attribute_id);
57  }
58 
59  // if the attribute is not found exit
60  if (empty($attribute_id)) {
61  echo 'The attribute "metadata_sort_type" for "page_asset_listing" does not exists';
62  exit();
63  }
64 
65 
66  // get all the page_asset_listings
67  $assets_id = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('page_asset_listing', TRUE);
68  // if no asset listings are found exit
69  if (empty($assets_id)) {
70  echo 'NO PAGE ASSET LISTINGS FOUND'."\n";
71  exit();
72  }
73  if ($verbose) {
74  bam('All the page_asset_listing ids');
75  bam($assets_id);
76  }
77 
78  // USER MANUAL
79  echo '*****************************************'."\n";
80  echo '* THE FOLLOWING ASSETS WILL BE AFFECTED *'."\n";
81  echo '*****************************************'."\n";
82  bam($assets_id);
83 
84  // create the SQL IN statement
85  $asset_listing_ids = '';
86  $_tmp_assets_id = Array();
87  if (count($assets_id) > 1) {
88  foreach ($assets_id as $key => $value) {
89  $_tmp_assets_id = array_merge($_tmp_assets_id, Array($key => $db->quote((string)$value)));
90  }
91  $asset_listing_ids = implode(',', $_tmp_assets_id);
92  } else {
93  $asset_listing_ids = "'".$assets_id[0]."'";
94  }
95 
96  // get all the page_asset_listings with a custom attribute
97  $select = "select atv.assetid from sq_ast_attr at, sq_ast_attr_val atv where at.name like 'metadata_sort_type' and at.attrid = atv.attrid and atv.assetid in (".$asset_listing_ids.");";
98  $with_custom_attr_assets_id = Array();
99  // PHP 5
100  if (version_compare(phpversion(), 5) >= 0) {
101  $result = NULL;
102  $query = $db->prepare($select);
103  try {
104  $result = MatrixDAL::executePdoAll($query);
105  } catch (Exception $e) {
106  throw new Exception('Could not get the '.$e->getMessage());
107  }//end
108  foreach ($result as $value) {
109  $with_custom_attr_assets_id = array_merge($with_custom_attr_assets_id, Array($value['assetid']));
110  }bam($result);
111  } else {
112  $result = $db->query($select);
113  assert_valid_db_result($result);
114  while (DB_OK === $result->fetchInto($row)) {
115  $with_custom_attr_assets_id = array_merge($with_custom_attr_assets_id, Array($row['assetid']));
116  }
117  $result->free();
118  }
119 
120  if ($verbose) {
121  bam('page_asset_listing ids of assets with custom attribute');
122  bam($with_custom_attr_assets_id);
123  }
124 
125 
126  // find the assets that we need to create a custom attribute
127  $create_custom_attr_id = array_diff($assets_id, $with_custom_attr_assets_id);
128  if ($verbose) {
129  bam('Assets that need to have a custom value');
130  bam($create_custom_attr_id);
131  }
132  // USER MANUAL
133  echo "\n\n";
134  echo "***************************************************************\n";
135  echo "* WARNING: DOUBLE CHECK THE SQL QUERIES BEFORE EXECUTING THEM *\n";
136  echo "***************************************************************\n\n";
137 
138 
139 
140  // USER MANUAL
141  echo "\n\n";
142  echo '*****************************************************************************'."\n";
143  echo '* TO SET THE METADATA SORTING OPTION TO "RAW" RUN THE FOLLOWING SQL QUERIES *'."\n";
144  echo '*****************************************************************************'."\n\n";
145 
146  // create a custom value for the assets
147  foreach($create_custom_attr_id as $id) {
148  echo 'INSERT INTO sq_ast_attr_val(assetid, attrid, custom_val) values (\''.$id.'\', \''.$attribute_id.'\', \'raw\');'."\n";
149  }
150 
151 
152  // create the SQL IN statement
153  $asset_listing_ids = '';
154  if (count($assets_id) > 1) {
155  foreach ($assets_id as $key => $value) {
156  $assets_id[$key] = $db->quote((string)$value);
157  }
158  $asset_listing_ids = implode(',', $assets_id);
159  } else {
160  $asset_listing_ids = $assets_id[0];
161  }
162 
163  // get all the assets that have a custom value for the attribute
164 
165  $where = '';
166  foreach ($assets_id as $id) {
167  if (!empty($where)) {
168  // we are NOT the first element
169  $where .= ' OR ';
170  }
171  $where .= '( assetid = '.$id.' AND attrid = \''.$attribute_id.'\')';
172  }
173 
174  $update = 'UPDATE sq_ast_attr_val set custom_val = \'raw\' where '.$where.';';
175  echo $update."\n";
176 
177  // USER MANUAL
178  echo "\n\n";
179  echo '**************************************************************************************'."\n";
180  echo '* TO SET THE METADATA SORTING OPTION TO "PRESENTATION" RUN THE FOLLOWING SQL QUERIES *'."\n";
181  echo '**************************************************************************************'."\n\n";
182 
183  // create a custom value for the assets
184  foreach($create_custom_attr_id as $id) {
185  echo 'INSERT INTO sq_ast_attr_val(assetid, attrid, custom_val) values (\''.$id.'\', \''.$attribute_id.'\', \'presentation\');'."\n";
186  }
187  $update = 'UPDATE sq_ast_attr_val set custom_val = \'presentation\' where '.$where.';';
188  echo $update."\n";
189 
190 
191  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
192 
193 ?>