function test_basic_check_array() { $pt =& Dataface_PermissionsTool::getInstance(); $this->assertTrue($pt->checkPermission('view', array('view' => 'View'))); $this->assertTrue(Dataface_PermissionsTool::checkPermission('view', array('view' => 'View'))); $this->assertTrue(!$pt->checkPermission('view', array())); $this->assertTrue(!Dataface_PermissionsTool::checkPermission('view', array())); $this->assertTrue($pt->checkPermission('edit', array('view' => 'View', 'edit' => 'Edit'))); $perms = array('view' => 'View'); $this->assertTrue($pt->view($perms)); $perms = array('view' => 'View'); $this->assertTrue(Dataface_PermissionsTool::view($perms)); $perms = array('edit' => 'Edit'); $this->assertTrue(!$pt->view($perms)); $perms = array('edit' => 'Edit'); $this->assertTrue(!Dataface_PermissionsTool::view($perms)); $this->assertTrue($pt->edit($perms)); $this->assertTrue(Dataface_PermissionsTool::edit($perms)); $perms = array('delete' => 'Delete'); $this->assertTrue(!$pt->edit($perms)); $this->assertTrue(!Dataface_PermissionsTool::edit($perms)); $this->assertTrue($pt->delete($perms)); $this->assertTrue(Dataface_PermissionsTool::delete($perms)); }
function display() { if ($this->_resultSet->found() > 0 || $this->_new) { $res = $this->_build(); if (PEAR::isError($res)) { return $res; } else { //$this->displayTabs(); if (!$this->_new and !Dataface_PermissionsTool::edit($this->_record)) { $this->freeze(); } if ($this->_new and !Dataface_PermissionsTool::checkPermission('new', $this->_table)) { $this->freeze(); } $formTool =& Dataface_FormTool::getInstance(); if ($this->_new || Dataface_PermissionsTool::view($this->_record)) { //echo $this->_renderer->toHtml(); echo $formTool->display($this); } else { echo "<p>" . df_translate('scripts.GLOBAL.INSUFFICIENT_PERMISSIONS_TO_VIEW_RECORD', 'Sorry you have insufficient permissions to view this record.') . "</p>"; } //parent::display(); } } else { echo "<p>" . df_translate('scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST', 'No records matched your request.') . "</p>"; } }
function toHtml() { $context = array(); $context['relatedList'] = $this; $app =& Dataface_Application::getInstance(); $context['app'] =& $app; $query =& $app->getQuery(); $context['query'] =& $query; if (isset($query['-related:sort'])) { $sortcols = explode(',', trim($query['-related:sort'])); $sort_columns = array(); foreach ($sortcols as $sortcol) { $sortcol = trim($sortcol); if (strlen($sortcol) === 0) { continue; } $sortcol = explode(' ', $sortcol); if (count($sortcol) > 1) { $sort_columns[$sortcol[0]] = strtolower($sortcol[1]); } else { $sort_columns[$sortcol[0]] = 'asc'; } break; } unset($sortcols); // this was just a temp array so we get rid of it here } else { $sort_columns = array(); } $context['sort_columns'] =& $sort_columns; $sort_columns_arr = array(); foreach ($sort_columns as $colkey => $colorder) { $sort_columns_arr[] = '`' . $colkey . '`' . $colorder; } if (count($sort_columns_arr) > 0) { $sort_columns_str = implode(', ', $sort_columns_arr); } else { $sort_columns_str = 0; } unset($query); $skinTool =& Dataface_SkinTool::getInstance(); $context['skinTool'] =& $skinTool; $resultController =& $skinTool->getResultController(); $context['resultController'] =& $resultController; $s =& $this->_table; $r =& $this->_relationship->_schema; $fkeys = $this->_relationship->getForeignKeyValues(); $local_fkey_fields = array(); foreach ($fkeys as $fk_table_name => $fk_table_cols) { foreach ($fk_table_cols as $k => $v) { if (is_string($v) and $v and $v[0] === '$') { $local_fkey_fields[$k] = $v; } } } $default_order_column = $this->_relationship->getOrderColumn(); //echo "Def order col = $default_order_column"; ob_start(); df_display(array('redirectUrl' => $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']), 'Dataface_MoveUpForm.html'); $moveUpForm = ob_get_contents(); ob_end_clean(); $context['moveUpForm'] = $moveUpForm; $records =& $this->_record->getRelatedRecords($this->_relationship_name, true, $this->_start, $this->_limit, $this->_where); if (PEAR::isError($records)) { $records->addUserInfo("Error retrieving records from relationship " . $this->_relationship_name); return $records; } $context['records'] =& $records; //echo "<br/><b>Now Showing</b> ".($this->_start+1)." to ".(min($this->_start + $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name))); $perms = $this->_record->getPermissions(array('relationship' => $this->_relationship_name)); $context['perms'] = $perms; $context['record_editable'] = Dataface_PermissionsTool::edit($this->_record); $context['can_add_new_related_record'] = @$perms['add new related record']; $context['can_add_existing_related_record'] = @$perms['add existing related record']; if (!$this->hideActions and ($context['record_editable'] or @$perms['add new related record'] or @$perms['add existing related record'])) { $query = array('-action' => 'new_related_record'); $link = Dataface_LinkTool::buildLink($query); $context['new_related_record_query'] = $query; $context['new_related_record_link'] = $link; $domainTable = $this->_relationship->getDomainTable(); //$context['domainTable'] =& $domainTable; $importTablename = $domainTable; if (!PEAR::isError($domainTable)) { //This relationship is many-to-many so we can add existing records to it. $query2 = array('-action' => 'existing_related_record'); $context['existing_related_record_query'] = $query2; $link2 = Dataface_LinkTool::buildLink($query2); $context['existing_related_record_link'] = $link2; $destTables = $this->_relationship->getDestinationTables(); $context['destTables'] =& $destTables; $importTablename = $destTables[0]->tablename; $context['importTablename'] = $importTablename; } if (!PEAR::isError($importTablename)) { $importTable =& Dataface_Table::loadTable($importTablename); $context['importTable'] =& $importTable; $query3 = array('-action' => 'import'); $context['import_related_records_query'] =& $query3; $link3 = Dataface_LinkTool::buildLink($query3); $context['import_related_records_link'] = $link3; } } $imgIcon = DATAFACE_URL . '/images/search_icon.gif'; $searchSrc = DATAFACE_URL . '/js/Dataface/RelatedList/search.js'; $relname = $this->_relationship_name; $context['relationship_label'] = $this->_relationship->getLabel(); $context['relname'] = $relname; $context['relationship_name'] = $this->_relationship_name; $context['searchSrc'] = $searchSrc; $context['imgIcon'] = $imgIcon; if (!$this->hideActions) { $num_related_records = $this->_record->numRelatedRecords($this->_relationship_name, $this->_where); $now_showing_start = $this->_start + 1; $now_showing_finish = min($this->_start + $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where)); $stats_context = array('num_related_records' => $num_related_records, 'now_showing_start' => $now_showing_start, 'now_showing_finish' => $now_showing_finish, 'relationship_name' => $this->_relationship_name, 'limit_field' => $resultController->limitField('related:'), 'back_link' => $this->_backButtonHtml(), 'next_link' => $this->_forwardButtonHtml()); import('Dataface/ActionTool.php'); $at =& Dataface_ActionTool::getInstance(); $actions = $at->getActions(array('category' => 'related_list_actions')); $context['related_list_actions'] = $actions; foreach ($stats_context as $k => $v) { $context[$k] = $v; } } import('Dataface/ActionTool.php'); $at =& Dataface_ActionTool::getInstance(); $selected_actions = $at->getActions(array('category' => 'selected_related_result_actions')); $context['selected_actions'] = $selected_actions; if ($this->_relationship->_schema['list']['type'] == 'treetable') { import('Dataface/TreeTable.php'); $treetable = new Dataface_TreeTable($this->_record, $this->_relationship->getName()); $context['treetable'] = $treetable->toHtml(); } else { echo $moveUpForm; if (!$this->hideActions and $this->_where) { $filterQuery =& $app->getQuery(); $context['filterQuery'] =& $filterQuery; } if (count($records) > 0) { ob_start(); echo ' <table class="listing relatedList relatedList--' . $this->_tablename . ' relatedList--' . $this->_tablename . '--' . $this->_relationship_name . '" id="relatedList"> <thead> <tr>'; if (count($selected_actions) > 0) { echo '<th>'; if (!$this->hideActions) { echo '<input type="checkbox" onchange="toggleSelectedRows(this,\'relatedList\');">'; } echo '</th>'; } $cols = array_keys(current($records)); $col_tables = array(); $table_keys = array(); $localFields = $this->_record->table()->fields(); $usedColumns = array(); foreach ($cols as $key) { if ($key == $default_order_column) { continue; } if (is_int($key)) { continue; } if (isset($sort_columns[$key])) { $class = 'sorted-column-' . $sort_columns[$key]; $query = array(); $qs_columns = $sort_columns; unset($qs_columns[$key]); $sort_query = $key . ' ' . ($sort_columns[$key] == 'desc' ? 'asc' : 'desc'); foreach ($qs_columns as $qcolkey => $qcolvalue) { $sort_query .= ', ' . $qcolkey . ' ' . $qcolvalue; } } else { $class = 'unsorted-column'; $sort_query = $key . ' asc'; foreach ($sort_columns as $scolkey => $scolvalue) { $sort_query .= ', ' . $scolkey . ' ' . $scolvalue; } } $sq = array('-related:sort' => $sort_query); $link = Dataface_LinkTool::buildLink($sq); $fullpath = $this->_relationship_name . '.' . $key; $field =& $this->_relationship->getField($key); if (isset($this->_relationship->_schema['visibility'][$key]) and $this->_relationship->_schema['visibility'][$key] == 'hidden') { continue; } if ($field['visibility']['list'] != 'visible') { continue; } if ($s->isBlob($fullpath) or $s->isPassword($fullpath)) { continue; } if (isset($local_fkey_fields[$key]) and !isset($this->_relationship->_schema['visibility'][$key])) { continue; } if (PEAR::isError($field)) { $field->addUserInfo("Error getting field info for field {$key} in RelatedList::toHtml() "); return $field; } $usedColumns[] = $key; $label = $field['widget']['label']; if (isset($field['column']) and @$field['column']['label']) { $label = $field['column']['label']; } $legend = ''; if (@$field['column'] and @$field['column']['legend']) { $legend = '<span class="column-legend">' . df_escape($field['column']['legend']) . '</span>'; } if (!$this->noLinks) { echo '<th><a href="' . df_escape($link) . '">' . df_escape($field['widget']['label']) . "</a> {$legend}</th>\n"; } else { echo '<th>' . $field['widget']['label'] . '</th>'; } if (!isset($col_tables[$key])) { $col_tables[$key] = $field['tablename']; } if (!isset($table_keys[$col_tables[$key]])) { $table_table =& Dataface_Table::loadTable($field['tablename']); $table_keys[$col_tables[$key]] = array_keys($table_table->keys()); unset($table_table); } unset($field); } echo "</tr>\n\t\t\t\t\t</thead>\n\t\t\t\t\t<tbody id=\"relatedList-body\">\n\t\t\t\t\t"; $limit = min($this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where) - $this->_start); $relatedTable = $this->_relationship->getDomainTable(); if (PEAR::isError($relatedTable)) { $relatedTable = reset($r['selected_tables']); } $relatedTable = Dataface_Table::loadTable($relatedTable); $relatedKeys = array_keys($relatedTable->keys()); foreach (array_keys($relatedKeys) as $i) { $relatedKeys[$i] = $this->_relationship_name . "." . $relatedKeys[$i]; } $fullpaths = array(); $fields_index = array(); foreach ($usedColumns as $key) { $fullpaths[$key] = $this->_relationship_name . '.' . $key; $fields_index[$key] =& $this->_relationship->getField($key); } $evenRow = false; for ($i = $this->_start; $i < $this->_start + $limit; $i++) { $rowClass = $evenRow ? 'even' : 'odd'; $evenRow = !$evenRow; if ($default_order_column and @$perms['reorder_related_records']) { $style = 'cursor:move'; // A variable that will be used below in javascript to decide // whether to make the table sortable or not $sortable_js = 'true'; } else { $style = ''; $sortable_js = 'false'; } $context['sortable_js'] = $sortable_js; unset($rrec); $rrec = $this->_record->getRelatedRecord($this->_relationship_name, $i, $this->_where, $sort_columns_str); //new Dataface_RelatedRecord($this->_record, $this->_relationship_name, $this->_record->getValues($fullpaths, $i, 0, $sort_columns_str)); $rrecid = $rrec->getId(); $rowPerms = $rrec->getPermissions(); if (!@$rowPerms['view']) { continue; } echo "<tr class=\"listing {$rowClass}\" style=\"{$style}\" id=\"row_{$rrecid}\">"; if (count($selected_actions) > 0) { echo ' <td class="' . $rowClass . ' viewableColumn" nowrap>'; if (!$this->hideActions) { echo '<input xf-record-id="' . df_escape($rrecid) . '" class="rowSelectorCheckbox" id="rowSelectorCheckbox:' . df_escape($rrecid) . '" type="checkbox">'; } echo '</td>'; } $link_queries = array(); foreach ($usedColumns as $key) { if (is_int($key)) { continue; } $fullpath = $fullpaths[$key]; unset($field); $field =& $fields_index[$key]; //$s->getField($fullpath); $srcRecord =& $rrec->toRecord($field['tablename']); if (!@$app->_conf['legacy_compatibility_mode']) { $link = $this->_record->getURL('-action=view_related_record&-related-record-id=' . urlencode($rrecid)); } else { //$link = $srcRecord->getURL('-action=browse&-portal-context=' . urlencode($rrecid)); $link = $rrec->getURL('-action=browse', $field['tablename']); } $srcRecordId = $srcRecord->getId(); //$val = $this->_record->preview($fullpath, $i,255, $this->_where, $sort_columns_str); if ($srcRecord->table()->isContainer($field['name']) or $srcRecord->table()->isBlob($field['name'])) { $val = $rrec->htmlValue($key, array('class' => 'blob-preview')); //$rrec->htmlValue($key); } else { $val = strip_tags($rrec->display($key)); } $title = ""; if ($key == $default_order_column) { unset($field); unset($srcRecord); continue; } else { if ($val != 'NO ACCESS') { $accessClass = 'viewableColumn'; } else { $accessClass = ''; } $cellClass = 'resultListCell resultListCell--' . $key; $cellClass .= ' ' . $srcRecord->table()->getType($key); $renderVal = $this->renderCell($srcRecord, $field['Field']); if (isset($renderVal)) { $val = $renderVal; } if ($link and !@$field['noLinkFromListView'] and !$this->noLinks and $rrec->checkPermission('link', array('field' => $key))) { $val = "<a href=\"" . df_escape($link) . "\" title=\"" . df_escape($title) . "\" data-xf-related-record-id=\"" . df_escape($srcRecordId) . "\" class=\"xf-related-record-link\">" . $val . "</a>"; } echo "<td class=\"{$cellClass} {$rowClass} {$accessClass}\">{$val}</td>\n"; unset($srcRecord); } } echo "</tr>\n"; } echo "</tbody>\n\t\t\t\t\t</table>"; $related_table_html = ob_get_contents(); $context['related_table_html'] = $related_table_html; ob_end_clean(); if (!$this->hideActions) { ob_start(); echo '<form id="result_list_selected_items_form" method="post">'; $app =& Dataface_Application::getInstance(); $q =& $app->getQuery(); foreach ($q as $key => $val) { if (strlen($key) > 1 and $key[0] == '-' and $key[1] == '-') { continue; } echo '<input type="hidden" name="' . $key . '" value="' . df_escape($val) . '">'; } echo '<input type="hidden" name="--selected-ids" id="--selected-ids">'; echo '<input type="hidden" name="-from" id="-from" value="' . $q['-action'] . '">'; echo '</form>'; $selected_actions_form = ob_get_contents(); $context['selected_actions_form'] = $selected_actions_form; ob_end_clean(); // This bit of javascript goes through all of the columns and removes all columns that // don't have any accessible information for this query. (i.e. any columns for which // each row's value is 'NO ACCESS' is removed $prototype_url = DATAFACE_URL . '/js/scriptaculous/lib/prototype.js'; $context['prototype_url'] = $prototype_url; $scriptaculous_url = DATAFACE_URL . '/js/scriptaculous/src/scriptaculous.js'; $context['scriptaculous_url'] = $scriptaculous_url; $effects_url = DATAFACE_URL . '/js/scriptaculous/src/effects.js'; $context['effects_url'] = $effects_url; $dragdrop_url = DATAFACE_URL . '/js/scriptaculous/src/dragdrop.js'; $context['dragdrop_url'] = $dragdrop_url; $thisRecordID = $this->_record->getId(); $context['thisRecordID'] = $thisRecordID; } } } Dataface_JavascriptTool::getInstance()->import('xataface/actions/related_list.js'); ob_start(); $context['filters'] = $this->filters; df_display($context, 'xataface/RelatedList/list.html'); $out = ob_get_contents(); ob_end_clean(); return $out; }
function toHtml() { $app =& Dataface_Application::getInstance(); $query =& $app->getQuery(); if (isset($query['-related:sort'])) { $sortcols = explode(',', trim($query['-related:sort'])); $sort_columns = array(); foreach ($sortcols as $sortcol) { $sortcol = trim($sortcol); if (strlen($sortcol) === 0) { continue; } $sortcol = explode(' ', $sortcol); if (count($sortcol) > 1) { $sort_columns[$sortcol[0]] = strtolower($sortcol[1]); } else { $sort_columns[$sortcol[0]] = 'asc'; } break; } unset($sortcols); // this was just a temp array so we get rid of it here } else { $sort_columns = array(); } $sort_columns_arr = array(); foreach ($sort_columns as $colkey => $colorder) { $sort_columns_arr[] = '`' . $colkey . '`' . $colorder; } if (count($sort_columns_arr) > 0) { $sort_columns_str = implode(', ', $sort_columns_arr); } else { $sort_columns_str = 0; } //echo $sort_columns_str;exit; unset($query); $skinTool =& Dataface_SkinTool::getInstance(); $resultController =& $skinTool->getResultController(); $s =& $this->_table; $r =& $this->_relationship->_schema; $fkeys = $this->_relationship->getForeignKeyValues(); $default_order_column = $this->_relationship->getOrderColumn(); //echo "Def order col = $default_order_column"; ob_start(); df_display(array('redirectUrl' => $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']), 'Dataface_MoveUpForm.html'); $moveUpForm = ob_get_contents(); ob_end_clean(); $records =& $this->_record->getRelatedRecords($this->_relationship_name, true, $this->_start, $this->_limit, $this->_where); if (PEAR::isError($records)) { $records->addUserInfo("Error retrieving records from relationship " . $this->_relationship_name . " on line " . __LINE__ . " of file " . __FILE__); return $records; } ob_start(); //echo "<br/><b>Now Showing</b> ".($this->_start+1)." to ".(min($this->_start + $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name))); $perms = $this->_record->getPermissions(array('relationship' => $this->_relationship_name)); if (Dataface_PermissionsTool::edit($this->_record) or @$perms['add new related record'] or @$perms['add existing related record']) { $query = array('-action' => 'new_related_record'); $link = Dataface_LinkTool::buildLink($query); $domainTable = $this->_relationship->getDomainTable(); $importTablename = $domainTable; if (!PEAR::isError($domainTable)) { //This relationship is many-to-many so we can add existing records to it. $query2 = array('-action' => 'existing_related_record'); $link2 = Dataface_LinkTool::buildLink($query2); $destTables = $this->_relationship->getDestinationTables(); $importTablename = $destTables[0]->tablename; } if (!PEAR::isError($importTablename)) { $importTable =& Dataface_Table::loadTable($importTablename); $query3 = array('-action' => 'import'); $link3 = Dataface_LinkTool::buildLink($query3); } echo "<div id=\"relatedActionsWrapper\" class=\"contentActions\"><ul id=\"relatedActions\">"; if ($this->_relationship->supportsAddNew() and @$perms['add new related record']) { echo "<li id=\"addNew\"><a id=\"add_new_related_record\" href=\"{$link}\">" . df_translate('scripts.Dataface.RelatedList.toHtml.LABEL_ADD_NEW_RELATED_RECORD', "Add New " . ucfirst($this->_relationship_name) . " Record", array('relationship' => ucfirst($this->_relationship_name))) . "</a></li>"; } if ($this->_relationship->supportsAddExisting() and isset($query2) and @$perms['add existing related record']) { echo "<li id=\"addExisting\"><a id=\"add_existing_related_record\" href=\"{$link2}\">" . df_translate('scripts.Dataface.RelatedList.toHtml.LABEL_ADD_EXISTING_RELATED_RECORD', "Add Existing " . ucfirst($this->_relationship_name) . " Record", array('relationship' => ucfirst($this->_relationship_name))) . "</a></li>"; } if (isset($query3) and count($importTable->getImportFilters()) > 0) { echo "<li id=\"import\"><a id=\"import_related_records\" href=\"{$link3}\">" . df_translate('scripts.Dataface.RelatedList.toHtml.LABEL_IMPORT_RELATED_RECORDS', "Import " . ucfirst($this->_relationship_name) . " Records", array('relationship' => ucfirst($this->_relationship_name))) . "</a></li>"; } echo "</ul></div>"; } $out = ob_get_contents(); ob_end_clean(); ob_start(); $imgIcon = DATAFACE_URL . '/images/search_icon.gif'; $searchSrc = DATAFACE_URL . '/js/Dataface/RelatedList/search.js'; $relname = $this->_relationship_name; echo <<<END \t\t<div class="result-tools" style="float:left"> \t\t\t<script language="javascript" type="text/javascript" src="{$searchSrc}"></script> \t\t\t<a href="#" onclick="Dataface.RelatedList.showSearch('{$relname}', document.getElementById('related_find_wrapper')); return false;" title="Filter these results"><img src="{$imgIcon}" alt="Filter" /></a> \t\t\t \t\t</div> END; echo '<div class="result-stats">'; $num_related_records = $this->_record->numRelatedRecords($this->_relationship_name, $this->_where); $now_showing_start = $this->_start + 1; $now_showing_finish = min($this->_start + $this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where)); echo df_translate('scripts.Dataface.RelatedList.toHtml.MESSAGE_FOUND', "<b>Found</b> " . $num_related_records . " Records in relationship <i>" . $this->_relationship_name . "</i>", array('num' => $num_related_records, 'relationship' => $this->_relationship_name)) . "<br/>" . df_translate('scripts.Dataface.RelatedList.toHtml.MESSAGE_NOW_SHOWING', "<b>Now Showing</b> " . $now_showing_start . " to " . $now_showing_finish, array('start' => $now_showing_start, 'finish' => $now_showing_finish)) . "</div>\n\t\t\t<div class=\"limit-field\">\n\t\t\t"; echo $resultController->limitField('related:'); echo "</div>\n\t\t\t<div class=\"prev-link\">" . $this->_backButtonHtml() . "</div>\n\t\t\t<div class=\"next-link\">" . $this->_forwardButtonHtml() . "</div>\n\t\t"; import('Dataface/ActionTool.php'); $at =& Dataface_ActionTool::getInstance(); $actions = $at->getActions(array('category' => 'related_list_actions')); echo <<<END \t\t<div class="result-list-actions"> \t\t<ul class="icon-only" id="result-list-actions"> END; foreach ($actions as $action) { if (@$action['onclick']) { $onclick = 'onclick="' . htmlspecialchars($action['onclick']) . '"'; } else { $onclick = ''; } echo <<<END \t\t\t <li id="result-list-actions-{$action['id']}" class="plain"> \t\t\t \t\t\t<a id="result-list-actions-{$action['id']}-link"href="{$action['url']}" {$onclick} \t\t\t accesskey="e" title="{$action['description']}"> \t\t\t <img id="result-list-actions-{$action['id']}-icon"src="{$action['icon']}" alt="{$action['label']}"/> \t\t\t\t<span class="action-label">{$action['label']}</span> \t\t\t</a> \t\t </li> END; } echo <<<END \t\t</ul> \t\t \t\t</div> END; $relatedResultController = ob_get_contents(); ob_end_clean(); ob_start(); //echo '<div style="clear: both"/>'; echo '<div class="resultlist-controller">'; echo $relatedResultController; echo "</div>"; import('Dataface/ActionTool.php'); $at =& Dataface_ActionTool::getInstance(); $selected_actions = $at->getActions(array('category' => 'selected_related_result_actions')); if ($this->_relationship->_schema['list']['type'] == 'treetable') { import('Dataface/TreeTable.php'); $treetable = new Dataface_TreeTable($this->_record, $this->_relationship->getName()); echo $treetable->toHtml(); } else { echo $moveUpForm; if ($this->_where) { $filterQuery =& $app->getQuery(); echo '<div>Showing matches for query <em>"' . htmlspecialchars($filterQuery['-related:search']) . '"</em> <a href="' . $app->url('-related:search=') . '" title="Remove this filter to show all records in this relationship"> <img src="' . DATAFACE_URL . '/images/delete.gif" alt="Remove filter" /> </a> </div>'; } echo '<div style="display:none" id="related_find_wrapper"></div>'; if (count($records) > 0) { echo ' <table class="listing relatedList relatedList--' . $this->_tablename . ' relatedList--' . $this->_tablename . '--' . $this->_relationship_name . '" id="relatedList"> <thead> <tr>'; if (count($selected_actions) > 0) { echo '<th><input type="checkbox" onchange="toggleSelectedRows(this,\'relatedList\');"></th> '; } $cols = array_keys(current($records)); $col_tables = array(); $table_keys = array(); $usedColumns = array(); foreach ($cols as $key) { if ($key == $default_order_column) { continue; } if (is_int($key)) { continue; } if (isset($sort_columns[$key])) { $class = 'sorted-column-' . $sort_columns[$key]; $query = array(); $qs_columns = $sort_columns; unset($qs_columns[$key]); $sort_query = $key . ' ' . ($sort_columns[$key] == 'desc' ? 'asc' : 'desc'); foreach ($qs_columns as $qcolkey => $qcolvalue) { $sort_query .= ', ' . $qcolkey . ' ' . $qcolvalue; } } else { $class = 'unsorted-column'; $sort_query = $key . ' asc'; foreach ($sort_columns as $scolkey => $scolvalue) { $sort_query .= ', ' . $scolkey . ' ' . $scolvalue; } } $sq = array('-related:sort' => $sort_query); $link = Dataface_LinkTool::buildLink($sq); $fullpath = $this->_relationship_name . '.' . $key; $field =& $s->getField($fullpath); if (isset($this->_relationship->_schema['visibility'][$key]) and $this->_relationship->_schema['visibility'][$key] == 'hidden') { continue; } if ($field['visibility']['list'] != 'visible') { continue; } if ($s->isBlob($fullpath) or $s->isPassword($fullpath)) { continue; } if (PEAR::isError($field)) { $field->addUserInfo("Error getting field info for field {$key} in RelatedList::toHtml() on line " . __LINE__ . " of file " . __FILE__); return $field; } $usedColumns[] = $key; $label = $field['widget']['label']; if (isset($field['column']) and @$field['column']['label']) { $label = $field['column']['label']; } $legend = ''; if (@$field['column'] and @$field['column']['legend']) { $legend = '<span class="column-legend">' . htmlspecialchars($field['column']['legend']) . '</span>'; } echo '<th><a href="' . $link . '">' . $field['widget']['label'] . "</a> {$legend}</th>\n"; if (!isset($col_tables[$key])) { $col_tables[$key] = $field['tablename']; } if (!isset($table_keys[$col_tables[$key]])) { $table_table =& Dataface_Table::loadTable($field['tablename']); $table_keys[$col_tables[$key]] = array_keys($table_table->keys()); unset($table_table); } unset($field); } echo "</tr>\n\t\t\t\t\t</thead>\n\t\t\t\t\t<tbody id=\"relatedList-body\">\n\t\t\t\t\t"; $limit = min($this->_limit, $this->_record->numRelatedRecords($this->_relationship_name, $this->_where) - $this->_start); $relatedTable = $this->_relationship->getDomainTable(); if (PEAR::isError($relatedTable)) { $relatedTable = reset($r['selected_tables']); } $relatedTable = Dataface_Table::loadTable($relatedTable); $relatedKeys = array_keys($relatedTable->keys()); foreach (array_keys($relatedKeys) as $i) { $relatedKeys[$i] = $this->_relationship_name . "." . $relatedKeys[$i]; } $fullpaths = array(); $fields_index = array(); foreach ($usedColumns as $key) { $fullpaths[$key] = $this->_relationship_name . '.' . $key; $fields_index[$key] =& $s->getField($fullpaths[$key]); } $evenRow = false; for ($i = $this->_start; $i < $this->_start + $limit; $i++) { $rowClass = $evenRow ? 'even' : 'odd'; $evenRow = !$evenRow; if ($default_order_column and @$perms['reorder_related_records']) { $style = 'cursor:move'; // A variable that will be used below in javascript to decide // whether to make the table sortable or not $sortable_js = 'true'; } else { $style = ''; $sortable_js = 'false'; } unset($rrec); $rrec = $this->_record->getRelatedRecord($this->_relationship_name, $i, $this->_where, $sort_columns_str); //new Dataface_RelatedRecord($this->_record, $this->_relationship_name, $this->_record->getValues($fullpaths, $i, 0, $sort_columns_str)); $rrecid = $rrec->getId(); echo "<tr class=\"listing {$rowClass}\" style=\"{$style}\" id=\"row_{$rrecid}\">"; if (count($selected_actions) > 0) { echo ' <td class="' . $rowClass . ' viewableColumn" nowrap> <input class="rowSelectorCheckbox" id="rowSelectorCheckbox:' . $rrecid . '" type="checkbox"> '; echo ' </td>'; } $link_queries = array(); foreach ($usedColumns as $key) { if (is_int($key)) { continue; } $fullpath = $fullpaths[$key]; unset($field); $field =& $fields_index[$key]; //$s->getField($fullpath); $srcRecord =& $rrec->toRecord($field['tablename']); $link = $srcRecord->getURL('-action=browse'); /* if ( isset($link_queries[$col_tables[$key]]) ){ $query = $link_queries[$col_tables[$key]]; $failed = false; } else { $query = array( "-action"=>"browse", "-relationship"=>null, "-cursor"=>0, "-table"=>$col_tables[$key]) ; $failed = false; // flag to indicate if we failed to generate appropriate link foreach ( $table_keys[$col_tables[$key]] as $table_key ){ $query[$table_key] = "=".$this->_record->getValueAsString($this->_relationship_name.'.'.$table_key, $i, $this->_where, $sort_columns_str); if ( $query[$table_key] == '=' ){ if ( isset( $fkeys[$col_tables[$key]][$table_key]) ){ $query[$table_key] = $this->_record->parseString($fkeys[$col_tables[$key]][$table_key]); } else { $failed = true; } } } $link_queries[$col_tables[$key]] = $query; } if ( $failed ){ $link = "#"; } else { $link = Dataface_LinkTool::buildLink($query, false); } */ //$val = ''; $val = $this->_record->preview($fullpath, $i, 255, $this->_where, $sort_columns_str); $title = ""; if ($key == $default_order_column) { unset($field); unset($srcRecord); continue; } else { if ($val != 'NO ACCESS') { $accessClass = 'viewableColumn'; } else { $accessClass = ''; } $cellClass = 'resultListCell resultListCell--' . $key; $renderVal = $this->renderCell($srcRecord, $field['Field']); if (isset($renderVal)) { $val = $renderVal; } else { if ($link and !@$field['noLinkFromListView']) { $val = "<a href=\"{$link}\" title=\"" . htmlspecialchars($title) . "\">" . $val . "</a>"; } } echo "<td class=\"{$cellClass} {$rowClass} {$accessClass}\">{$val}</td>\n"; unset($srcRecord); } } echo "</tr>\n"; } echo "</tbody>\n\t\t\t\t\t</table>"; echo '<form id="result_list_selected_items_form" method="post">'; $app =& Dataface_Application::getInstance(); $q =& $app->getQuery(); foreach ($q as $key => $val) { if (strlen($key) > 1 and $key[0] == '-' and $key[1] == '-') { continue; } echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($val) . '">'; } echo '<input type="hidden" name="--selected-ids" id="--selected-ids">'; echo '<input type="hidden" name="-from" id="-from" value="' . $query['-action'] . '">'; echo '</form>'; if (count($selected_actions) > 0) { echo '<div id="selected-actions">' . df_translate('scripts.GLOBAL.LABEL_WITH_SELECTED', 'With Selected') . ': <ul class="selectedActionsMenu" id="result_list-selectedActionsMenu">'; foreach ($selected_actions as $action) { echo <<<END \t\t\t\t\t\t<li id="action-{$action['id']}"><a href="{$action['url']}" title="{$action['description']}">{$action['label']}</a></li> END; } echo '</ul></div>'; } echo '<div class="resultlist-controller">'; echo $relatedResultController; echo '</div>'; // This bit of javascript goes through all of the columns and removes all columns that // don't have any accessible information for this query. (i.e. any columns for which // each row's value is 'NO ACCESS' is removed $prototype_url = DATAFACE_URL . '/js/scriptaculous/lib/prototype.js'; $scriptaculous_url = DATAFACE_URL . '/js/scriptaculous/src/scriptaculous.js'; $effects_url = DATAFACE_URL . '/js/scriptaculous/src/effects.js'; $dragdrop_url = DATAFACE_URL . '/js/scriptaculous/src/dragdrop.js'; $thisRecordID = $this->_record->getId(); echo <<<END \t\t\t\t<script language="javascript" src="{$prototype_url}"></script> \t\t\t\t<script language="javascript" src="{$scriptaculous_url}"></script> \t\t\t\t<script language="javascript" src="{$effects_url}"></script> \t\t\t\t<script language="javascript" src="{$dragdrop_url}"></script> \t\t\t\t<script language="javascript"><!-- \t\t\t\tfunction removeUnauthorizedColumns(){ \t\t\t\t\tvar relatedList = document.getElementById('relatedList'); \t\t\t\t\tvar trs = relatedList.getElementsByTagName('tr'); \t\t\t\t\tvar viewableColumns = []; \t\t\t\t\tvar numCols = 0; \t\t\t\t\tfor (var i=0; i<trs.length; i++){ \t\t\t\t\t\tvar tr = trs[i]; \t\t\t\t\t\tvar tds = tr.getElementsByTagName('td'); \t\t\t\t\t\tfor (var j=0; j<tds.length; j++){ \t\t\t\t\t\t\tvar td = tds[j]; \t\t\t\t\t\t\tif ( td.className.indexOf('viewableColumn') >= 0 ){ \t\t\t\t\t\t\t\tviewableColumns[j] = true; \t\t\t\t\t\t\t} \t\t\t\t\t\t\tnumCols = j; \t\t\t\t\t\t} \t\t\t\t\t} \t\t\t\t\tfor (var j=viewableColumns.length; j<=numCols; j++){ \t\t\t\t\t\tviewableColumns[j] = false; \t\t\t\t\t} \t\t\t\t\t \t\t\t\t\t \t\t\t\t\tfor (var i=0; i<trs.length; i++){ \t\t\t\t\t\tvar tds = trs[i].getElementsByTagName('td'); \t\t\t\t\t\tif ( tds.length <= 0 ){ \t\t\t\t\t\t\tvar tds = trs[i].getElementsByTagName('th'); \t\t\t\t\t\t} \t\t\t\t\t\t \t\t\t\t\t\tfor (var j=0; j<viewableColumns.length; j++){ \t\t\t\t\t\t\tif ( !viewableColumns[j] ){ \t\t\t\t\t\t\t\ttds[j].style.display = 'none'; \t\t\t\t\t\t\t} \t\t\t\t\t\t} \t\t\t\t\t\t \t\t\t\t\t} \t\t\t\t} \t\t\t\tremoveUnauthorizedColumns(); \t\t\t\t \t\t\t\t \t\t\t\tif ( {$sortable_js} ){ \t\t\t\t\tSortable.create("relatedList-body", \t\t\t\t\t\t\t{ \t\t\t\t\t\t\t\tdropOnEmpty:true, \t\t\t\t\t\t\t\tconstraint:false, \t\t\t\t\t\t\t\t//handle:'move-handle', \t\t\t\t\t\t\t\ttag:'tr', \t\t\t\t\t\t\t\tonUpdate: function(container){ \t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t\tvar params = Sortable.serialize('relatedList-body'); \t\t\t\t\t\t\t\t\tparams += '&'+window.location.search.substring(1); \t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t\tparams += '&-action=reorder_related_records';//&--recordid='+escape('{$thisRecordID}'); \t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t\tnew Ajax.Request( \t\t\t\t\t\t\t\t\t\tDATAFACE_SITE_HREF, { \t\t\t\t\t\t\t\t\t\t\tmethod: 'post', \t\t\t\t\t\t\t\t\t\t\tparameters: params, \t\t\t\t\t\t\t\t\t\t\tonSuccess: function(transport){ \t\t\t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t\t\t\t\t//document.getElementById('details-controller').innerHTML = transport.responseText; \t\t\t\t\t\t\t\t\t\t\t}, \t\t\t\t\t\t\t\t\t\t\tonFailure:function(){ \t\t\t\t\t\t\t\t\t\t\t\talert('Failed to sort records.'); \t\t\t\t\t\t\t\t\t\t\t} \t\t\t\t\t\t\t\t\t\t} \t\t\t\t\t\t\t\t\t); \t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t} \t\t\t\t\t\t\t\t//only:'movable' \t\t\t\t\t\t\t}); \t\t\t\t\t\t//Sortable.create("dataface-sections-main", \t\t\t\t\t\t//{dropOnEmpty:true,constraint:false, handle:'movable-handle',tag:'div',only:'movable', onUpdate:updateSections}); \t\t\t\t}\t \t\t\t\t \t\t\t\t//--></script> \t\t\t\t END; } else { echo "<p>" . df_translate('scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST', 'No records matched your request.') . "</p>"; } } $out .= ob_get_contents(); ob_end_clean(); return $out; }
/** * * Imports data into the supplied record's relationship. This makes use of this table's delegate * file to handle the importing. * * @param $record A Dataface_Record object whose relationship is to have records added to it. * @type Dataface_Record | null * * @param $data Either raw data that is to be imported, or the name of an Import table from which * data is to be imported. * @type Raw | string * * @param $importFilter The name of the import filter that should be used. * @type string * * @param $relationshipName The name of the relationship where these records should be added. * @type string * * @param $commit A boolean value indicating whether this import should be committed to the * database. If this is false, then the records will not actually be imported. They * will merely be stored in an import table. This must be explicitly set to true * for the import to succeed. * @type boolean * * @param defaultValues Array of default values of the form [Abs fieldname] -> [field value], where 'Abs fieldname' * is the absolute field name (ie: tablename.fieldname). All imported records will attain * these default values. * @type array([string] -> [mixed]) * * @return Case 1: The import succeeds. * Case 1.1: if commit = false return Import Table name where data is stored. * Case 1.2: If commit = true return array of Dataface_Record objects that were inserted. * Case 2: The import failed * return PEAR_Error object. * * * Usage: * ------- * $data = '<phonelist> * <listentry> * <name>John Smith</name><number>555-555-5555</number> * </listentry> * <listentry> * <name>Susan Moore</name><number>444-444-4444</number> * </listentry> * </phonelist>'; * * // assume that we have an import filter called 'XML_Filter' that can import the above data. * * $directory = new Dataface_Record('Directory', array('Name'=>'SFU Directory')); * // assume that the Directory table has a relationship called 'phonelist' and we want to * // import the above data into this relationship. * * $io = new Dataface_IO('Directory'); * $importTableName = $io->importData( $directory, // The record that owns the relationship where imported records will be added * $data, // The raw data to import * 'XML_Filter', // The name of the impot * 'phonelist' * ); * // Since we didn't set the $commit flag, the data has been imported into an import table * // whose name is stored now in $importTableName. * * // * // Now suppose we have confirmed that the import is what we want to do and we are ready to import * // the data into the database. * $records = $io->importData($directory, $importTableName, null, 'phonelist', true ); * * echo $records[0]->val('name'); // should output 'John Smith' * echo $records[0]->val('number'); // should output '555-555-5555' * echo $records[1]->val('name'); // should output 'Susan Moore' * echo $records[1]->val('number'); // should output '444-444-4444' * * // note that at this point the records in $records are already persisted to the database * */ function importData(&$record, $data, $importFilter = null, $relationshipName = null, $commit = false, $defaultValues = array()) { if ($relationshipName === null) { /* * No relationship is specified so our import table is just the current table. */ $table =& $this->_table; } else { /* * A relationship is specified so we are actually importing the records into the * domain table of the relationship. */ $relationship =& $this->_table->getRelationship($relationshipName); $tablename = $relationship->getDomainTable(); if (PEAR::isError($tablename)) { /* * This relationship does not have a domain table.. so we will just take the destination table. */ $destinationTables =& $relationship->getDestinationTables(); if (count($destinationTables) <= 0) { trigger_error(df_translate('scripts.Dataface.IO.importData.ERROR_NO_DESTINATION_TABLES', "Error occurred while attempting to parse import data into a table. The relationship '" . $relationship->getName() . "' of table '" . $this->_table->tablename . "' has not destination tables listed. It should have at least one.\n", array('relationship' => $relationship->getName(), 'table' => $this->_table->tablename)) . Dataface_Error::printStackTrace(), E_USER_ERROR); } $tablename = $destinationTables[0]->tablename; } if (PEAR::isError($tablename)) { trigger_error($tablename->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } $table =& Dataface_Table::loadTable($tablename); $rel_io = new Dataface_IO($tablename); $io =& $rel_io; } if (!$commit) { // If data is provided, we must parse it and prepare it for // import $records = $table->parseImportData($data, $importFilter, $defaultValues); if (PEAR::isError($records)) { /* * The import didn't work with the specified import filter, so we will * try the other filters. */ $records = $table->parseImportData($data, null, $defaultValues); } if (PEAR::isError($records)) { /* * Apparently we have failed to import the data, so let's just * return the errors. */ return $records; } // Now we will load the values of the records into an array // so that we can store it in the session $importData = array('table' => $table->tablename, 'relationship' => $relationshipName, 'defaults' => $defaultValues, 'importFilter' => $importFilter, 'record' => null, 'rows' => array()); if (isset($record)) { $importData['record'] = $record->getId(); } foreach ($records as $r) { if (is_a($r, 'Dataface_ImportRecord')) { // The current record is actually an ImportRecord $importData['rows'][] = $r->toArray(); } else { $importData['rows'][] = $r->vals(array_keys($r->_table->fields(false, true))); unset($r); } } $dumpFile = tempnam(sys_get_temp_dir(), 'dataface_import'); $handle = fopen($dumpFile, "w"); if (!$handle) { trigger_error("Could not write import data to dump file {$dumpFile}", E_USER_ERROR); } fwrite($handle, serialize($importData)); fclose($handle); $_SESSION['__dataface__import_data__'] = $dumpFile; return $dumpFile; } if (!@$_SESSION['__dataface__import_data__']) { trigger_error("No import data to import", E_USER_ERROR); } $dumpFile = $_SESSION['__dataface__import_data__']; $importData = unserialize(file_get_contents($dumpFile)); if ($importData['table'] != $table->tablename) { return PEAR::raiseError("Unexpected table name in import data. Expected " . $table->tablename . " but received " . $importData['table']); } $inserted = array(); $i = 0; foreach ($importData['rows'] as $row) { if (isset($row['__CLASS__']) and isset($row['__CLASSPATH__'])) { // This row is an import record - not merely a Dataface_Record // object so it provides its own logic to import the records. import($row['__CLASSPATH__']); $class = $row['__CLASS__']; $importRecord = new $class($row); $res = $importRecord->commit($record, $relationshipName); if (PEAR::isError($res)) { return $res; } } else { $values = array(); foreach (array_keys($row) as $key) { if (!is_int($key)) { $values[$key] = $row[$key]; } } if ($relationshipName === null) { /* * These records are not being added to a relationship. They are just being added directly * into the table. */ $defaults = array(); // for absolute field name keys for default values, we will strip out the table name. foreach (array_keys($defaultValues) as $key) { if (strpos($key, '.') !== false) { list($tablename, $fieldname) = explode('.', $key); if ($tablename == $this->_table->tablename) { $defaults[$fieldname] = $defaultValues[$key]; } else { continue; } } else { $defaults[$key] = $defaultValues[$key]; } } $values = array_merge($defaults, $values); $insrecord = new Dataface_Record($this->_table->tablename, $values); $inserted[] =& $insrecord; $this->write($insrecord); $insrecord->__destruct(); unset($insrecord); } else { /* * The records are being added to a relationship so we need to make sure that we add the appropriate * entries to the "join" tables as well. */ foreach (array_keys($values) as $key) { $values[$table->tablename . '.' . $key] = $values[$key]; unset($values[$key]); } $values = array_merge($defaultValues, $values); /* * Let's check if all of the keys are set. If they are then the record already exists.. we * just need to update the record. * */ $rvalues = array(); foreach ($values as $valkey => $valval) { if (strpos($valkey, '.') !== false) { list($tablename, $fieldname) = explode('.', $valkey); if ($tablename == $table->tablename) { $rvalues[$fieldname] = $valval; } } } $rrecord = new Dataface_Record($table->tablename, array()); $rrecord->setValues($rvalues); // we set the values in a separate call because we want to be able to do an update // and setting values in the constructer sets the snapshot (ie: it will think that // no values have changed. if ($io->recordExists($rrecord)) { /* * The record already exists, so we update it and then add it to the relationship. * */ if (Dataface_PermissionsTool::edit($rrecord)) { /* * We only edit the record if we have permission to do so. */ $result = $io->write($rrecord); if (PEAR::isError($result)) { trigger_error($result->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } } $relatedRecord = new Dataface_RelatedRecord($record, $relationshipName, $values); $inserted[] =& $relatedRecord; $qb = new Dataface_QueryBuilder($this->_table->tablename); $sql = $qb->addExistingRelatedRecord($relatedRecord); $res2 = $this->performSQL($sql); unset($relatedRecord); } else { $relatedRecord = new Dataface_RelatedRecord($record, $relationshipName, $values); $inserted[] =& $relatedRecord; $qb = new Dataface_QueryBuilder($this->_table->tablename); $sql = $qb->addRelatedRecord($relatedRecord); $res2 = $this->performSQL($sql); unset($relatedRecord); } unset($rrecord); } } unset($row); } @unlink($dumpFile); unset($_SESSION['__dataface__import_data__']); return $inserted; }
/** * Handles initialization and control for the import records form. */ function _import_init() { import('Dataface/ImportForm.php'); $form = new Dataface_ImportForm($this->_tablename); $record =& $form->_record; if (!Dataface_PermissionsTool::edit($record)) { $this->_vars['error'] = "<div class=\"error\">Error. Permission Denied.<!-- At line " . __LINE__ . " of file " . __FILE__ . " --></div>"; return; } $form->_build(); $this->_vars['form'] =& $form; if ($form->validate()) { //echo "validated"; $querystr = $form->exportValue('-query'); if (intval($form->_step) === 1) { if (preg_match('/-step=1/', $querystr)) { $querystr = preg_replace('/-step=1/', '-step=2', $querystr); } else { $querystr .= '&-step=2'; } $importTablename = $form->process(array(&$form, 'import')); //echo "Table: $importTablename"; //exit; //$link = 'Location: '.$_SERVER['PHP_SELF'].'?'.$querystr.'&--importTablename='.$importTablename; //echo $link; //exit; header('Location: ' . $_SERVER['PHP_SELF'] . '?' . $querystr . '&--importTablename=' . $importTablename); exit; } else { $records = $form->process(array(&$form, 'import')); $keys = $form->exportValue('__keys__'); $keys['-action'] = 'browse'; $keys['-step'] = null; $keys['-query'] = null; $link = Dataface_LinkTool::buildLink($keys); $response =& Dataface_Application::getResponse(); $msg = urlencode(trim("Records imported successfully.\n" . @$response['--msg'])); header('Location: ' . $link . '&--msg=' . $msg); exit; } } //echo "Not validated"; }