31 import(
'Dataface/Table.php');
32 import(
'Dataface/QueryBuilder.php');
33 import(
'Dataface/LinkTool.php');
52 if ( !is_a($record,
'Dataface_Record') ){
53 throw new Exception(
"In Dataface_RelatedList constructor, the first argument is expected to be an object of type 'Dataface_Record' but received '".get_class($record));
55 $this->_record =& $record;
56 $this->_tablename = $this->_record->_table->tablename;
58 $this->_relationship_name = $relname;
61 $this->_table =& $this->_record->_table;
62 $this->_relationship =& $this->_table->getRelationship($relname);
64 $this->_start = isset($_REQUEST[
'-related:start']) ? $_REQUEST[
'-related:start'] : 0;
65 $this->_limit = isset($_REQUEST[
'-related:limit']) ? $_REQUEST[
'-related:limit'] : 30;
68 $query =&
$app->getQuery();
69 if ( isset($query[
'-related:search']) ){
71 foreach ($this->_relationship->fields() as $rfield){
73 $rwhere[] =
'`'.str_replace(
'.',
'`.`',$rfield).
'` LIKE \'%'.addslashes($query[
'-related:search']).
'%\'';
75 $rwhere = implode(
' OR ', $rwhere);
79 $this->_where = $rwhere;
85 $numRecords = $this->_record->numRelatedRecords( $this->_relationship_name, $this->_where );
86 if ( $this->_start + $this->_limit >= $numRecords )
return '';
87 $query = array(
'-related:start'=> $this->_start+$this->_limit,
'-related:limit'=>$this->_limit);
89 $out =
'<a href="'.$link.
'" title="Next '.$this->_limit.
' Results"><img src="'.DATAFACE_URL.
'/images/go-next.png" alt="Next" /></a>';
90 if ( ($this->_start+(2*$this->_limit)) < $numRecords ){
91 $query[
'-related:start'] = $numRecords - ( ($numRecords -
$this->_start) % $this->_limit) -1;
93 $out .=
'<a href="'.$link.
'" title="Last"><img src="'.DATAFACE_URL.
'/images/go-last.png" alt="Last" /></a>';
99 if ( $this->_start <= 0 )
return '';
100 $query = array(
'-related:start'=> max( 0, $this->_start-$this->_limit),
'-related:limit'=>$this->_limit);
102 $out =
'<a href="'.$link.
'" title="Previous '.$this->_limit.
' Results"><img src="'.DATAFACE_URL.
'/images/go-previous.png" alt="Previous" /></a>';
104 if ( ($this->_start-$this->_limit) > 0 ){
105 $query[
'-related:start'] = 0;
106 $out =
'<a href="'.Dataface_LinkTool::buildLink($query).
'" title="First"><img src="'.DATAFACE_URL.
'/images/go-first.png" alt="First" /></a>'.
$out;
114 $del =& $record->_table->getDelegate();
115 if ( isset($del) and method_exists($del,
$fieldname.
'__renderCell') ){
117 return $del->$method($record);
127 $context[
'relatedList'] = $this;
129 $context[
'app'] =&
$app;
131 $query =&
$app->getQuery();
132 $context[
'query'] =& $query;
134 if ( isset( $query[
'-related:sort']) ){
135 $sortcols = explode(
',', trim($query[
'-related:sort']));
136 $sort_columns = array();
137 foreach ($sortcols as $sortcol){
138 $sortcol = trim($sortcol);
139 if (strlen($sortcol) === 0 )
continue;
140 $sortcol = explode(
' ', $sortcol);
141 if ( count($sortcol) > 1 ){
142 $sort_columns[$sortcol[0]] = strtolower($sortcol[1]);
144 $sort_columns[$sortcol[0]] =
'asc';
150 $sort_columns = array();
152 $context[
'sort_columns'] =& $sort_columns;
154 $sort_columns_arr = array();
155 foreach ( $sort_columns as $colkey=>$colorder) {
156 $sort_columns_arr[] =
'`'.$colkey.
'`'. $colorder;
158 if ( count($sort_columns_arr) > 0 ){
159 $sort_columns_str = implode(
', ',$sort_columns_arr);
161 $sort_columns_str = 0;
170 $context[
'skinTool'] =& $skinTool;
172 $resultController =& $skinTool->getResultController();
173 $context[
'resultController'] =& $resultController;
176 $r =& $this->_relationship->_schema;
177 $fkeys = $this->_relationship->getForeignKeyValues();
178 $default_order_column = $this->_relationship->getOrderColumn();
182 df_display(array(
'redirectUrl'=>
$_SERVER[
'PHP_SELF'].
'?'.
$_SERVER[
'QUERY_STRING']),
'Dataface_MoveUpForm.html');
183 $moveUpForm = ob_get_contents();
185 $context[
'moveUpForm'] = $moveUpForm;
191 $records =& $this->_record->getRelatedRecords($this->_relationship_name,
true, $this->_start, $this->_limit, $this->_where);
194 $records->addUserInfo(
"Error retrieving records from relationship ".$this->_relationship_name);
197 $context[
'records'] =& $records;
200 $perms = $this->_record->getPermissions(array(
'relationship'=>$this->_relationship_name));
201 $context[
'perms'] = $perms;
203 $context[
'can_add_new_related_record'] = @$perms[
'add new related record'];
204 $context[
'can_add_existing_related_record'] = @$perms[
'add existing related record'];
206 if ( !$this->hideActions and ($context[
'record_editable'] or @$perms[
'add new related record'] or @$perms[
'add existing related record']) ){
207 $query = array(
'-action'=>
'new_related_record');
209 $context[
'new_related_record_query'] = $query;
210 $context[
'new_related_record_link'] = $link;
212 $domainTable = $this->_relationship->getDomainTable();
214 $importTablename = $domainTable;
217 $query2 = array(
'-action'=>
'existing_related_record');
218 $context[
'existing_related_record_query'] = $query2;
220 $context[
'existing_related_record_link'] = $link2;
222 $destTables = $this->_relationship->getDestinationTables();
223 $context[
'destTables'] =& $destTables;
224 $importTablename = $destTables[0]->tablename;
225 $context[
'importTablename'] = $importTablename;
229 $context[
'importTable'] =& $importTable;
230 $query3 = array(
'-action'=>
'import');
231 $context[
'import_related_records_query'] =& $query3;
233 $context[
'import_related_records_link'] = $link3;
240 $imgIcon = DATAFACE_URL.
'/images/search_icon.gif';
241 $searchSrc = DATAFACE_URL.
'/js/Dataface/RelatedList/search.js';
243 $context[
'relationship_label'] = $this->_relationship->getLabel();
244 $context[
'relname'] = $relname;
246 $context[
'searchSrc'] = $searchSrc;
247 $context[
'imgIcon'] = $imgIcon;
250 if ( !$this->hideActions ){
251 $num_related_records = $this->_record->numRelatedRecords($this->_relationship_name, $this->_where);
252 $now_showing_start = $this->_start+1;
253 $now_showing_finish = min($this->_start + $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where));
255 $stats_context = array(
256 'num_related_records'=>$num_related_records,
257 'now_showing_start'=>$now_showing_start,
258 'now_showing_finish'=>$now_showing_finish,
259 'relationship_name'=>$this->_relationship_name,
260 'limit_field'=>$resultController->limitField(
'related:'),
266 import(
'Dataface/ActionTool.php');
268 $actions = $at->getActions(array(
269 'category'=>
'related_list_actions'
273 $context[
'related_list_actions'] = $actions;
274 foreach ($stats_context as $k=>$v) $context[$k] = $v;
282 import(
'Dataface/ActionTool.php');
284 $selected_actions = $at->getActions(array(
'category'=>
'selected_related_result_actions'));
285 $context[
'selected_actions'] = $selected_actions;
287 if ( $this->_relationship->_schema[
'list'][
'type'] ==
'treetable' ){
288 import(
'Dataface/TreeTable.php');
290 $context[
'treetable'] = $treetable->
toHtml();
293 if ( !$this->hideActions and $this->_where ){
295 $filterQuery =&
$app->getQuery();
296 $context[
'filterQuery'] =& $filterQuery;
299 if ( count($records) > 0 ){
303 <table class="listing relatedList relatedList--'.$this->_tablename.
' relatedList--'.$this->_tablename.
'--'.$this->_relationship_name.
'" id="relatedList">
307 if ( count($selected_actions)>0){
309 if ( !$this->hideActions ){
310 echo
'<input type="checkbox" onchange="toggleSelectedRows(this,\'relatedList\');">';
314 $cols = array_keys(current($records));
318 $col_tables = array();
319 $table_keys = array();
320 $localFields = $this->_record->table()->fields();
321 $usedColumns = array();
322 foreach ($cols as $key ){
323 if ( $key == $default_order_column )
continue;
324 if ( is_int($key) )
continue;
325 if ( isset($sort_columns[$key]) ){
326 $class =
'sorted-column-'.$sort_columns[$key];
328 $qs_columns = $sort_columns;
329 unset($qs_columns[$key]);
330 $sort_query = $key.
' '.($sort_columns[$key] ==
'desc' ?
'asc' :
'desc');
331 foreach ( $qs_columns as $qcolkey=> $qcolvalue){
332 $sort_query .=
', '.$qcolkey.
' '.$qcolvalue;
335 $class =
'unsorted-column';
336 $sort_query = $key.
' asc';
337 foreach ( $sort_columns as $scolkey=>$scolvalue){
338 $sort_query .=
', '.$scolkey.
' '.$scolvalue;
342 $sq = array(
'-related:sort'=>$sort_query);
345 $fullpath = $this->_relationship_name.
'.'.$key;
347 $field =& $s->getField($fullpath);
348 if ( isset( $this->_relationship->_schema[
'visibility'][$key]) and $this->_relationship->_schema[
'visibility'][$key] ==
'hidden' )
continue;
349 if (
$field[
'visibility'][
'list'] !=
'visible')
continue;
350 if ( $s->isBlob($fullpath) or $s->isPassword($fullpath) )
continue;
351 if ( isset($localFields[$key]) )
continue;
353 $field->addUserInfo(
"Error getting field info for field $key in RelatedList::toHtml() ");
356 $usedColumns[] = $key;
358 $label =
$field[
'widget'][
'label'];
359 if ( isset(
$field[
'column']) and @
$field[
'column'][
'label'] ){
360 $label = $field[
'column'][
'label'];
364 if ( @$field[
'column'] and @$field[
'column'][
'legend'] ){
365 $legend =
'<span class="column-legend">'.htmlspecialchars($field[
'column'][
'legend']).
'</span>';
368 if (!$this->noLinks ){
369 echo
'<th><a href="'.htmlspecialchars($link).
'">'.htmlspecialchars($field[
'widget'][
'label']).
"</a> $legend</th>\n";
371 echo
'<th>'.$field[
'widget'][
'label'].
'</th>';
373 if ( !isset($col_tables[$key]) ) $col_tables[$key] = $field[
'tablename'];
374 if (!isset($table_keys[$col_tables[$key]]) ){
376 $table_keys[$col_tables[$key]] = array_keys($table_table->keys());
385 <tbody id=\"relatedList-body\">
388 $limit = min( $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where)-
$this->_start);
389 $relatedTable = $this->_relationship->getDomainTable();
391 $relatedTable = reset($r[
'selected_tables']);
395 $relatedKeys = array_keys($relatedTable->keys());
396 foreach (array_keys($relatedKeys) as $i){
397 $relatedKeys[$i] = $this->_relationship_name.
".".$relatedKeys[$i];
400 $fullpaths = array();
401 $fields_index=array();
402 foreach( $usedColumns as $key ){
403 $fullpaths[$key] = $this->_relationship_name.
'.'.$key;
404 $fields_index[$key] =& $s->getField($fullpaths[$key]);
412 for ( $i=$this->_start; $i<($this->_start+$limit); $i++){
413 $rowClass = $evenRow ?
'even' :
'odd';
414 $evenRow = !$evenRow;
416 if ( $default_order_column and @$perms[
'reorder_related_records'] ){
417 $style =
'cursor:move';
420 $sortable_js =
'true';
423 $sortable_js =
'false';
425 $context[
'sortable_js'] = $sortable_js;
429 $rrec = $this->_record->getRelatedRecord($this->_relationship_name, $i,$this->_where, $sort_columns_str);
430 $rrecid = $rrec->getId();
432 echo
"<tr class=\"listing $rowClass\" style=\"$style\" id=\"row_$rrecid\">";
433 if ( count($selected_actions)>0){
435 <td class="'.$rowClass.
' viewableColumn" nowrap>';
436 if ( !$this->hideActions ){
437 echo
'<input xf-record-id="'.htmlspecialchars($rrecid).
'" class="rowSelectorCheckbox" id="rowSelectorCheckbox:'.htmlspecialchars($rrecid).
'" type="checkbox">';
443 $link_queries=array();
444 foreach ($usedColumns as $key){
445 if ( is_int($key) )
continue;
447 $fullpath = $fullpaths[$key];
449 $field =& $fields_index[$key];
450 $srcRecord =& $rrec->toRecord(
$field[
'tablename']);
451 $link = $srcRecord->getURL(
'-action=browse&-portal-context='.urlencode($rrecid));
452 $srcRecordId = $srcRecord->getId();
455 $val = strip_tags($rrec->display($key));
458 if ( $key == $default_order_column ){
464 if ($val !=
'NO ACCESS'){
465 $accessClass =
'viewableColumn';
470 $cellClass =
'resultListCell resultListCell--'.$key;
471 $cellClass .=
' '.$srcRecord->table()->getType($key);
473 if ( isset($renderVal) ) $val = $renderVal;
474 else if ( $link and !@
$field[
'noLinkFromListView'] and !$this->noLinks and $rrec->checkPermission(
'link', array(
'field'=>$key))) $val =
"<a href=\"".htmlspecialchars($link).
"\" title=\"". htmlspecialchars($title).
"\" data-xf-related-record-id=\"".htmlspecialchars($srcRecordId).
"\" class=\"xf-related-record-link\">".$val.
"</a>";
475 echo
"<td class=\"$cellClass $rowClass $accessClass\">$val</td>\n";
486 $related_table_html = ob_get_contents();
487 $context[
'related_table_html'] = $related_table_html;
490 if ( !$this->hideActions ){
493 echo
'<form id="result_list_selected_items_form" method="post">';
495 $q =&
$app->getQuery();
496 foreach ( $q as $key=>$val){
497 if ( strlen($key)>1 and $key{0} ==
'-' and $key{1} ==
'-' ){
500 echo
'<input type="hidden" name="'.$key.
'" value="'.htmlspecialchars($val).
'">';
502 echo
'<input type="hidden" name="--selected-ids" id="--selected-ids">';
503 echo
'<input type="hidden" name="-from" id="-from" value="'.$query[
'-action'].
'">';
505 $selected_actions_form = ob_get_contents();
506 $context[
'selected_actions_form'] = $selected_actions_form;
515 $prototype_url = DATAFACE_URL.
'/js/scriptaculous/lib/prototype.js';
516 $context[
'prototype_url'] = $prototype_url;
517 $scriptaculous_url =DATAFACE_URL.
'/js/scriptaculous/src/scriptaculous.js';
518 $context[
'scriptaculous_url'] = $scriptaculous_url;
519 $effects_url = DATAFACE_URL.
'/js/scriptaculous/src/effects.js';
520 $context[
'effects_url'] = $effects_url;
521 $dragdrop_url = DATAFACE_URL.
'/js/scriptaculous/src/dragdrop.js';
522 $context[
'dragdrop_url'] = $dragdrop_url;
523 $thisRecordID = $this->_record->getId();
524 $context[
'thisRecordID'] = $thisRecordID;
535 df_display($context,
'xataface/RelatedList/list.html');
536 $out = ob_get_contents();