Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bodycopy_table.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/bodycopy/bodycopy_container/bodycopy_container.inc';
18 require_once(dirname(__FILE__).'/bodycopy_table_cell.inc');
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _createAdditional(Array &$link)
60  {
61  // grab the bodycopy data that we stored in the create() function
62  $bc_data = $this->_tmp['bodycopy_create_data'];
63  unset($this->_tmp['bodycopy_create_data']);
64 
65  if (!parent::_createAdditional($link)) return FALSE;
66 
67  $num_rows = $bc_data['num_rows'];
68  $num_cols = $bc_data['num_cols'];
69 
70  for ($i = 0; $i < $num_rows; $i++) {
71  $rows[$i] = Array();
72  $rows[$i]['attributes'] = Array();
73  $rows[$i]['cells'] = Array();
74  for ($x = 0; $x < $num_cols; $x++) {
75  $rows[$i]['cells'][$x] = new Bodycopy_Table_Cell();
76  // short and long names are the same to begin with
77  $content_type = $GLOBALS['SQ_SYSTEM']->getUserPrefs('bodycopy_container', 'SQ_DEFAULT_CONTENT_TYPE');
78  if (!$rows[$i]['cells'][$x]->setContentType($this, $content_type, $i, $x)) {
79  return FALSE;
80  }
81  }
82  }
83 
84  // set row information
85  $this->setAttrValue('rows', $rows);
86  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'attributes') || !$this->saveAttributes()) {
87  return FALSE;
88  }
89 
90  return TRUE;
91 
92  }//end _createAdditional()
93 
94 
101  public function getContentType()
102  {
103  if (SQ_IN_BACKEND || SQ_IN_LIMBO || !isset($this->_tmp['content_type'])) {
104  unset($this->_tmp['content_type']);
105  $link = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'content_type', FALSE, 'major', '', TRUE, TRUE);
106  if (is_array($link) && !empty($link)) {
107  foreach ($link as $id => $type_link) {
108  $this->_tmp['content_type'][$id] = $GLOBALS['SQ_SYSTEM']->am->getAsset($type_link['minorid'], $type_link['minor_type_code']);
109  if (is_null($this->_tmp['content_type'][$id])) {
110  echo translate('content_type_not_found', $type_link['minorid']);
111  }
112  }//end foreach
113  } else {
114  $null = NULL;
115  $this->_tmp['content_type'] = $null;
116  }
117  }
118  return $this->_tmp['content_type'];
119 
120  }//end getContentType()
121 
122 
139  public function setAttrValue($name, $value)
140  {
141  if ($name == 'rows') {
142  parent::setAttrValue('rows', Array());
143  }
144  return parent::setAttrValue($name, $value);
145 
146  }//end setAttrValue()
147 
148 
157  public function remapAssetids(Array $map)
158  {
159  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
160  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
161  if (!parent::remapAssetids($map)) {
162  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
163  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
164  return FALSE;
165  }
166 
167  $old_assetid = array_search($this->id, $map);
168 
169  // get our clone and make it update its tables array
170  if (isset($map[$this->id])) {
171  $child_links = $GLOBALS['SQ_SYSTEM']->am->getAllChildLinks($this->id, SQ_LINK_TYPE_2);
172  $new_clone = $GLOBALS['SQ_SYSTEM']->am->getAsset($map[$this->id]);
173  $GLOBALS['SQ_SYSTEM']->am->acquireLock($new_clone->id, 'attributes');
174 
175  if (!$new_clone->remapTableLinks($map, $child_links)) {
176  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
177  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
178  return FALSE;
179  }
180 
181  $GLOBALS['SQ_SYSTEM']->am->releaseLock($new_clone->id, 'attributes');
182  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($new_clone);
183  }
184 
185  if ($old_assetid === FALSE) {
186  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
187  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
188  return TRUE;
189  }
190 
191  $old_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($old_assetid, $this->type());
192  $our_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_SC_LINK_SIGNIFICANT, 'content_type', FALSE);
193 
194  if (is_null($old_asset) || empty($our_links)) {
195  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
196  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
197  return TRUE;
198  }
199  $rows = $this->attr('rows');
200 
201  for ($i = 0; $i < count($rows); $i++) {
202  // now print the cell contents
203  for ($j = 0; $j < count($rows[$i]['cells']); $j++) {
204  $old_linkid = $rows[$i]['cells'][$j]->attributes['linkid'];
205  $old_link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($old_linkid, $old_asset->id);
206  if (empty($old_link)) continue;
207 
208  if (isset($map[$old_link['minorid']])) {
209  $new_assetid = $map[$old_link['minorid']];
210 
211  foreach ($our_links as $our_link) {
212  if ($our_link['minorid'] == $new_assetid) {
213  $rows[$i]['cells'][$j]->attributes['linkid'] = $our_link['linkid'];
214  break;
215  }
216  }// end for
217 
218  }// end if
219  }// end for
220  }// end for
221 
222  if ($this->setAttrValue('rows', $rows)) {
223  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
224  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
225  return TRUE;
226  } else {
227  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
228  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
229  return FALSE;
230  }
231 
232  }//end remapAssetids()
233 
234 
244  public function remapTableLinks(Array &$map, Array &$child_links)
245  {
246  $table_map = $this->attr('rows');
247  $child_map = Array();
248 
249  foreach ($child_links as $linkval) {
250  $child_map[$linkval['linkid']] = $linkval['assetid'];
251  }
252 
253  // iterate our table map, find old links and update
254  foreach ($table_map as $rowid => $row) {
255  foreach ($row['cells'] as $cellid => $cell) {
256  if (in_array($cell->attributes['linkid'], array_keys($child_map))) {
257  // get correct link and update table
258  $new_link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($this->id, $map[$child_map[$cell->attributes['linkid']]]);
259  $table_map[$rowid]['cells'][$cellid]->attributes['linkid'] = $new_link['linkid'];
260  }
261  }
262  }
263 
264  $this->setAttrValue('rows', $table_map);
265  $this->saveAttributes();
266 
267  return TRUE;
268 
269  }//end remapTableLinks()
270 
271 
272 }//end class
273 
274 ?>