Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
RelatedRecord.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
62 
70  var $secureDisplay = true;
71 
75  var $vetoSecurity;
80  var $_record;
81 
87 
93 
99  var $_values;
100 
107  var $_metaDataValues;
108 
117  var $_absoluteColumnNames;
118 
122  var $_lockedFields=array();
127  var $_records;
131  var $_dirtyFlags=array();
132 
136  var $cache=array();
137 
138 
146  function Dataface_RelatedRecord($record, $relationshipName, $values=null){
147 
148  if ( !is_a($record, 'Dataface_Record') ){
149  throw new Exception("Error in Dataface_RelatedRecord constructor. Expected first argument to be of type 'Dataface_Record' but received '".get_class($record)."'.", E_USER_ERROR);
150  }
151  $this->_record =& $record;
152  $this->_relationshipName = $relationshipName;
153  $this->_relationship =& $record->_table->getRelationship($relationshipName);
154  if ( is_array($values) ){
155  $this->setValues($values);
156  $this->clearFlags();
157  }
158  }
159 
160 
165  function _initValues(){
166  if ( !isset( $this->_values ) ){
167  $fkeys = $this->_relationship->getForeignKeyValues();
168  $this->_values = array();
169  $this->_absoluteColumnNames = array();
170  //$cols = $this->_relationship->_schema['columns'];
171  $cols = $this->_relationship->fields(true); // we will get all fields - even grafted ones.
172  foreach ($cols as $col){
173  list($table, $field) = explode('.', $col);
174  $this->_values[$field] = null;
175  if ( isset($this->_absoluteColumnNames[$field]) and
176  Dataface_Table::loadTable($this->_relationship->getDomainTable())->hasField($field) ){
177  $this->_absoluteColumnNames[$field] = $this->_relationship->getDomainTable().'.'.$field;
178  } else {
179  $this->_absoluteColumnNames[$field] = $col;
180  }
181 
182 
183  /*
184  * We want to check for locked fields. Locked fields are fields that *must* have a particular
185  * value for the relationship to remain valid.
186  */
187  if ( isset( $fkeys[$table][$field]) and is_scalar($fkeys[$table][$field]) and strpos($fkeys[$table][$field],'$') ===0 ){
188  $this->_lockedFields[$field] = $fkeys[$table][$field];
189  $this->_values[$field] = $this->_record->parseString($fkeys[$table][$field]);
190  }
191  }
192  }
193  }
194 
195  //---------------------------------------------------------------------------------
196  //{@
197 
210  function clearCache(){
211  $this->cache = array();
212  return $this;
213  }
214 
215 
224  function &toRecord($tablename=null){
225  if ( isset($this->cache[__FUNCTION__][$tablename]) ){
226  return $this->cache[__FUNCTION__][$tablename];
227  }
228  if ( !isset($tablename) ){
229  $tablename = $this->_relationship->getDomainTable();
230 
231 
232  }
233 
234  $table =& Dataface_Table::loadTable($tablename);
235 
236 
237  $values = array();
238 
239 
240  $absVals = $this->getAbsoluteValues();
241  $fieldnames = $this->_relationship->fields(true);
242  //foreach ( array_keys($absVals) as $key ){
243  foreach ( $fieldnames as $key ){
244  list($currTablename, $columnName) = explode('.', $key);
245  if ( ($currTablename == $tablename or $table->hasField($columnName)) and array_key_exists($key, $absVals)){
246 
247  $values[$columnName] = $absVals[$key];
248 
249  } else if ( isset($this->_relationship->_schema['aliases'][$columnName]) /*and
250  /*$table->hasField($this->_relationship->_schema['aliases'][$columnName])*/ ){
251  $values[$this->_relationship->_schema['aliases'][$columnName]] = $absVals[$key];
252  }
253  }
254 
255  foreach ( $this->_values as $key=>$val ){
256  if ( !isset($values[$key]) and $table->hasField($key) ) $values[$key] = $this->_values[$key];
257  }
258 
259  $record = new Dataface_Record($tablename, $values);
260  $record->secureDisplay = $this->secureDisplay;
261  foreach (array_keys($values) as $key){
262 
263  if ( $this->isDirty($key) ) $record->setFlag($key);
264 
265  }
266  $this->cache[__FUNCTION__][$tablename] =& $record;
267 
268  return $record;
269  }
270 
271 
272 
281  function toRecords(){
282  $tables =& $this->_relationship->getDestinationTables();
283  $out = array();
284  foreach ( array_keys($tables) as $index ){
285  $out[] =& $this->toRecord($tables[$index]->tablename);
286  }
287  return $out;
288  }
289 
290 
316  function &getParent(){
317  return $this->_record;
318  }
319 
320 
337  function testCondition($condition){
338  extract($this->strvals());
339  return eval('return ('.$condition.');');
340  }
341 
342 
353  function getActions($params=array()){
354  $params['record'] =& $this;
355  return $this->_record->_table->tablename->getActions($params);
356  }
357 
358 
359  // @}
360  // END OF Utility Methods
361  //-------------------------------------------------------------------------------------
362 
363 
364  //-------------------------------------------------------------------------------------
365  // @{
379  function clearFlags(){
380  $this->_dirtyFlags = array();
381  return $this;
382  }
383 
389  function setFlag($fieldname){
390  $this->_dirtyFlags[$fieldname] = true;
391  return $this;
392  }
393 
400  unset($this->_dirtyFlags[$fieldname]);
401  return $this;
402  }
403 
409  function isDirty($fieldname){
410  return ( isset($this->_dirtyFlags[$fieldname]) );
411  }
412 
413  // @}
414  // End Transactions
415  //---------------------------------------------------------------------------------------
416 
417  //----------------------------------------------------------------------------------------
418  // @{
419 
434  function setMetaDataValue($key, $value){
435  if ( !isset( $this->_metaDataValues ) ) $this->_metaDataValues = array();
436  $this->_metaDataValues[$key] = $value;
437  return $this;
438 
439  }
440 
441 
452  if ( strpos($fieldname, '.') !== false ){
453  list($tablename, $fieldname) = explode('.', $fieldname);
454  return $this->getLength($fieldname);
455  }
456  $key = '__'.$fieldname.'_length';
457  if ( isset( $this->_metaDataValues[$key] ) ){
458  return $this->_metaDataValues[$key];
459  } else {
460  return strlen($this->getValueAsString($fieldname));
461  }
462  }
463 
472  function setValue($fieldname, $value){
473  $this->_initValues();
474 
475 
476  if ( strpos($fieldname,'.') === false ){
477  if ( strpos($fieldname, '__') === 0 ){
478  return $this->setMetaDataValue($fieldname, $value);
479  }
480 
481  if ( isset( $this->_lockedFields[$fieldname]) ) return;
482  $val = $this->_record->_table->parse($this->_relationshipName.".".$fieldname, $value);
483  if ( $val != @$this->_values[$fieldname] ){
484  $this->_values[$fieldname] = $val;
485  $this->clearCache();
486  $this->setFlag($fieldname);
487  }
488 
489  } else {
490 
491  list ( $table, $field ) = explode('.', $fieldname);
492  if ( $table != $this->_relationship->getDomainTable() and Dataface_Table::loadTable($this->_relationship->getDomainTable())->hasField($field) ){
493  return PEAR::raiseError("Cannot set duplicate value in relationship. The field $fieldname is part of the domain table and not part of $table in this relationship.");
494  }
495  return $this->setValue($field, $value);
496  }
497 
498  }
499 
508  function setValues($values){
509  if ( !is_array($values) ){
510  throw new Exception( "setValues() expects 1st parameter to be an array but received a '".get_class($values)."' ",E_USER_WARNING);
511  }
512  foreach ( $values as $key=>$value){
513 
514  $this->setValue($key, $value);
515  }
516 
517  }
518 
519 
520 
521 
522 
531  function getValue($fieldname){
532  $this->_initValues();
533  if ( strpos($fieldname,'.') === false ){
534 
535  if ( !array_key_exists( $fieldname, $this->_values ) ){
536  // The key does not exist as a normal field -- so check if it is a calculated field.
537  $tables = $this->_relationship->getDestinationTables();
538  $dt =& Dataface_Table::loadTable($this->_relationship->getDomainTable());
539 
540  if ( $dt->hasField($fieldname) ){
541  $tables = array($dt);
542  } else {
543  //echo "Domain table doesn't have $fieldname";
544  throw new Exception("Domain table doesn't have field $fieldname");
545  }
546  foreach ( array_keys($tables) as $tkey){
547  if ( $tables[$tkey]->hasField($fieldname) ){
548  $tempRecord = new Dataface_Record($tables[$tkey]->tablename,$this->getValues());
549  return $tempRecord->getValue($fieldname);
550  }
551  }
552  throw new Exception("Attempt to get value for fieldname '$fieldname' that does not exist in related record of relationship '".$this->_relationshipName."'. Acceptable values include {".implode(', ', array_keys($this->_values))."}.\n<br>", E_USER_ERROR);
553  }
554 
555  return $this->_values[$fieldname];
556 
557  } else {
558  list ( $table, $field ) = explode('.', $fieldname);
559  return $this->getValue($field);
560  }
561  }
562 
574  $value = $this->getValue($fieldname);
575 
576  $parent =& $this->getParent();
577  $table =& $parent->_table->getTableTableForField($this->_relationshipName.'.'.$fieldname);
578  if ( PEAR::isError($table) ){
579  throw new Exception($table->toString(), E_USER_ERROR);
580  }
581  $delegate =& $table->getDelegate();
582  $rel_fieldname = $fieldname; //$table->relativeFieldName($fieldname);
583  if ( $delegate !== null and method_exists( $delegate, $rel_fieldname.'__toString') ){
584  $value = call_user_func( array(&$delegate, $rel_fieldname.'__toString'), $value);
585  } else
586 
587 
588  if ( is_array($value) ){
589  if ( method_exists( $table, $table->getType($fieldname)."_to_string") ){
590  $value = call_user_func( array( &$table, $table->getType($fieldname)."_to_string"), $value );
591  } else {
592  $value = implode(', ', $value);
593  }
594  }
595 
596 
597  $evt = new stdClass;
598  $evt->table = $table;
599  $evt->field =& $table->getField($rel_fieldname);
600  $evt->value = $value;
601  $evt->type = $table->getType($rel_fieldname);
602  $table->app->fireEvent('after_getValueAsString', $evt);
603  $value = $evt->value;
604 
605  return $value;
606 
607  }
608 
617  $value = $this->getValue($fieldname);
618  $parent =& $this->getParent();
619  $table =& $parent->_table->getTableTableForField($this->_relationshipName.'.'.$fieldname);
620  if ( PEAR::isError($table) ){
621  throw new Exception($table->toString(), E_USER_ERROR);
622  }
623  $record =& $this->toRecord($table->tablename);
624 
625  if ( $this->secureDisplay and !$this->checkPermission('view') ){
626  $val = $this->display($fieldname);
627  $field = $record->table()->getField($fieldname);
628  if ( !@$field['passthru'] and $record->escapeOutput) $val = nl2br(htmlspecialchars($val));
629 
630  $del =& $record->table()->getDelegate();
631  if ( $del and method_exists($del, 'no_access_link') ){
632  $link = $del->no_access_link($record, array('field'=>$fieldname));
633  return '<a href="'.htmlspecialchars($link).'">'.$val.'</a>';
634  } else {
635  return $val;
636  }
637 
638  } else {
639  $oldSecure = $record->secureDisplay;
640  $record->secureDisplay = false;
641  $htmlval = $record->htmlValue($fieldname);
642  $record->secureDisplay = $oldSecure;
643  return $htmlval;
644  }
645  }
646 
658  $keys = is_array($fields) ? $fields : array_keys($this->_values);
659  $values = array();
660  foreach ($keys as $key){
661  $values[$key] = $this->getValueAsString($key);
662  }
663  return $values;
664  }
665 
666 
671  function strvals($fields=''){
672  return $this->getValuesAsStrings($fields);
673  }
674 
675 
680  function strval($fieldname, $index=0){
681  return $this->getValueAsString($fieldname, $index);
682  }
683 
684 
690  return $this->getValueAsString($fieldname);
691  }
692 
703  function &getValues($columns=null, $excludeNulls=false){
704  $this->_initValues();
705  return $this->_values;
706  }
707 
712  function &values($fields = null){
713  return $this->getValues($fields);
714  }
715 
720  function &vals($fields = null){
721  return $this->getValues($fields);
722  }
723 
724 
729  function val($fieldname){
730  return $this->getValue($fieldname);
731  }
732 
733 
734 
742  function getAbsoluteValues($excludeNulls=false, $includeAll=false){
743  $absVals = array();
744  foreach ( $this->getValues() as $key=>$value){
745  if ( !isset($this->_absoluteColumnNames[$key]) ){
746  // Tough call here. In this case the most likely scenario
747  // is that the value is a transient field.
748  // In the past (due to a bug or not), transient fields have not been
749  // included in the output of this method. For consistency, we'll
750  // keep it that way by just skipping this field.
751  continue;
752  $tablename = $this->_relationship->getTable($key)->tablename;
753  $this->_absoluteColumnNames[$key] = $tablename.'.'.$key;
754  }
755  $absVals[ $this->_absoluteColumnNames[$key] ] = $value;
756  }
757  return $absVals;
758 
759 
760  }
761 
772  function getForeignKeyValues($sql = null){
773  if ( !isset($sql) ) $sql_index = 0;
774  else $sql_index = $sql;
775  if ( isset($this->cache[__FUNCTION__][$sql_index]) ){
776  return $this->cache[__FUNCTION__][$sql_index];
777  }
778  $fkeys = $this->_relationship->getForeignKeyValues();
779  $absVals = $this->getAbsoluteValues(true);
780 
781  $out = $this->_relationship->getForeignKeyValues($absVals, $sql, $this->getParent());
782  $this->cache[__FUNCTION__][$sql_index] = $out;
783  return $out;
784 
785  }
786 
794  function getUnconstrainedFields($sql = null){
795 
796  //$fkCols = $this->getForeignKeyValues($sql);
797  $tmp = new Dataface_RelatedRecord($this->_record, $this->_relationshipName, array());
798  $fkCols = $tmp->getForeignKeyValues($sql);
799 
800  if ( PEAR::isError($fkCols) ){
801  throw new Exception($fkCols->getMessage(), $fkCols->getCode());
802 
803  }
804 
805  $unconstrainedFields = array();
806  $cols = $this->_relationship->fields();
807 
808  foreach ($cols as $col){
809  $field = $this->_relationship->getField($col);
810  //print_r($field);
811  $tablename = $field['tablename'];
812  $fieldname = $field['name'];
813  //echo $absFieldname;
814  if ( array_key_exists($tablename, $fkCols) and array_key_exists($fieldname, $fkCols[$tablename]) ){
815  // This column is already specified by the foreign key relationship so we don't need to pass
816  // this information using the form.
817  // Actually - this isn't entirely true. If there is no auto-incrementing field
818  // associated with this foreign key, then
819  if ( $this->_relationship->isNullForeignKey($fkCols[$tablename][$fieldname]) ){
820  $furthestField = $fkCols[$tablename][$fieldname]->getFurthestField();
821  if ( $furthestField != $absFieldname ){
822  // We only display this field if it is the furthest field of the key
823  continue;
824  }
825 
826  } else {
827  continue;
828  }
829  }
830 
831  if ( @$field['grafted'] && !@$field['transient'] ) continue;
832  $unconstrainedFields[] = $col;
833 
834  }
835 
836  return $unconstrainedFields;
837 
838 
839  }
840 
848  function getConstrainedFields($sql=null){
849 
850  return array_diff($this->_relationship->fields(), $this->getUnconstrainedFields($sql));
851 
852  }
853 
861  function getUnconstrainedTables($sql=null){
862 
863  $tables = array();
864  $cols = $this->getUnconstrainedFields($sql);
865  foreach ($cols as $col){
866  $field = $this->_relationship->getField($col);
867  $tables[$field['tablename']] = 1;
868  }
869  return array_keys($tables);
870  }
871 
872 
873 
874 
899  function display($fieldname){
900  if ( isset($this->cache[__FUNCTION__][$fieldname]) ){
901  return $this->cache[__FUNCTION__][$fieldname];
902  }
903  $parent =& $this->getParent();
904  $table =& $parent->_table->getTableTableForField($this->_relationshipName.'.'.$fieldname);
905  if (PEAR::isError($table) ){
906  throw new Exception("Error loading table while displaying $fieldname because ".$table->getMessage(), E_USER_ERROR);
907 
908  }
909 
910 
911  if ( !$table->isBlob($fieldname) ){
912  $record =& $this->toRecord($table->tablename);
913  if ( $this->secureDisplay and $this->checkPermission('view', array('field'=>$fieldname)) ){
914  //echo "HERE";
915  $oldSecure = $record->secureDisplay;
916  $record->secureDisplay = false;
917  $out = $record->display($fieldname);
918  $record->secureDisplay = $oldSecure;
919  } else {
920  $out = $record->display($fieldname);
921  }
922 
923  $this->cache[__FUNCTION__][$fieldname] = $out;
924  return $out;
925 
926  } else {
927  $keys = array_keys($table->keys());
928  $qstr = '';
929  foreach ($keys as $key){
930  $qstr .= "&$key"."=".$this->strval($key);
931  }
932  $out = DATAFACE_SITE_HREF."?-action=getBlob&-table=".$table->tablename."&-field=$fieldname$qstr";
933  $this->cache[__FUNCTION__][$fieldname] = $out;
934  return $out;
935  }
936 
937 
938  }
939 
951  function preview($fieldname, $index=0, $maxlength=255){
952  if ( isset($this->cache[__FUNCTION__][$fieldname][$index][$maxlength]) ){
953  return $this->cache[__FUNCTION__][$fieldname][$index][$maxlength];
954  }
955  $strval = strip_tags($this->display($fieldname,$index));
956  $out = substr($strval, 0, $maxlength);
957  if ( strlen($strval)>$maxlength) {
958  $out .= '...';
959  }
960  $this->cache[__FUNCTION__][$fieldname][$index][$maxlength] = $out;
961  return $out;
962  }
963 
969  return $this->display($fieldname);
970  }
971 
976  function printval($fieldname){
977  return $this->display($fieldname);
978  }
979 
984  function q($fieldname){
985  return $this->display($fieldname);
986  }
987 
995  function qq($fieldname){
996  $parent =& $this->getParent();
997  $table =& $parent->_table->getTableTableForField($this->_relationshipName.'.'.$fieldname);
998  if ( PEAR::isError($table) ){
999  throw new Exception($table->toString(), E_USER_ERROR);
1000  }
1001  if ( !$table->isBlob($fieldname) ){
1002  return htmlspecialchars($this->q($fieldname, $index));
1003  } else {
1004  return $this->display($fieldname, $index);
1005  }
1006  }
1007 
1008 
1009  // @}
1010  // End Accessing Field Data
1011  //-------------------------------------------------------------------------------------------
1012 
1013 
1014  //-------------------------------------------------------------------------------------------
1015  // @{
1033  function getId(){
1034  if ( isset($this->cache[__FUNCTION__]) ){
1035  return $this->cache[__FUNCTION__];
1036  }
1037  $parentid = $this->_record->getId();
1038  list($tablename, $querystr) = explode('?',$parentid);
1039  $id = $tablename.'/'.$this->_relationshipName.'?'.$querystr;
1040  $keys = array_keys($this->_relationship->keys());
1041  $params = array();
1042  foreach ($keys as $key){
1043  $params[] = urlencode($this->_relationshipName.'::'.$key).'='.urlencode($this->strval($key));
1044  }
1045  $out = $id.'&'.implode('&',$params);
1046  $this->cache[__FUNCTION__] = $out;
1047  return $out;
1048  }
1049 
1050 
1059  function getTitle(){
1060  $method = 'rel_'.$this->_relationshipName.'__getTitle';
1061  $del = $this->_record->table()->getDelegate();
1062 
1063  if ( isset($del) and method_exists($del, $method) ){
1064  return $del->$method($this);
1065  }
1066 
1067 
1068  $record =& $this->toRecord();
1069  if ( $this->checkPermission('view') ){
1070  $oldSecureDisplay = $record->secureDisplay;
1071  $record->secureDisplay = false;
1072  $out = $record->getTitle();
1073  $record->secureDisplay = $oldSecureDisplay;
1074  return $out;
1075  } else {
1076  return $record->getTitle();
1077  }
1078  }
1079 
1080 
1081 
1082  // @}
1083  // END Metadata
1084  //--------------------------------------------------------------------------------------------
1085 
1086  // @{
1106  function validate( $fieldname, $value, &$params){
1107  if ( strpos($fieldname, '.') !== false ){
1108  list($relname, $fieldname) = explode('.', $fieldname);
1109  return $this->validate($fieldname, $value, $params);
1110  }
1111 
1112  if ( !is_array($params) ){
1113  $params = array('message'=> &$params);
1114  }
1115  $table =& $this->_relationship->getTable($fieldname);
1116  if (PEAR::isError($table) ){
1117  error_log($table->toString().implode("\n", $table->getBacktrace()));
1118  throw new Exception("Failed to get table for field $fieldname. See error log for details", E_USER_ERROR);
1119 
1120  } else if (!$table ){
1121  throw new Exception("Could not load table for field $fieldname .", E_USER_ERROR);
1122  }
1123  $field =& $table->getField($fieldname);
1124 
1125  if ( $field['widget']['type'] == 'file' and @$field['validators']['required'] and is_array($value) and $this->getLength($fieldname) == 0 and !is_uploaded_file(@$value['tmp_name'])){
1126  // This bit of validation operates on the upload values assuming the file was just uploaded as a form. It assumes
1127  // that $value is of the form
1129  $params['message'] = "$fieldname is a required field.";
1130  return false;
1131  }
1132 
1133  $res = $table->validate($fieldname, $value, $params);
1134  if ( $res ){
1135  $delegate =& $table->getDelegate();
1136  if ( $delegate !== null and method_exists($delegate, $fieldname."__validate") ){
1137  /*
1138  *
1139  * The delegate defines a custom validation method for this field. Use it.
1140  *
1141  */
1142  $methodname = $fieldname."__validate";
1143  $res = $delegate->$methodname($this,$value,$params);
1144  //$res = call_user_func(array(&$delegate, $fieldname."__validate"), $this, $value, $params);
1145  }
1146  }
1147  return $res;
1148 
1149  }
1150 
1151  // @}
1152  // END Form Validation
1153  //--------------------------------------------------------------------------------------------
1154 
1155  // @{
1162  function save($lang=null, $secure=false){
1163  $recs =& $this->toRecords();
1164 
1165  foreach (array_keys($recs) as $i){
1166 
1167  $res = $recs[$i]->save($lang, $secure);
1168  if ( PEAR::isError($res) ) return $res;
1169  }
1170  }
1171 
1172  // @}
1173  // End Saving
1174  //----------------------------------------------------------------------------------------------
1175 
1176  //----------------------------------------------------------------------------------------------
1177  // @{
1185  function getPermissions($params=array(), $table=null){
1186 
1187  // 1. Get the permissions for the particular field
1188  if ( isset($params['field']) ){
1189  if ( strpos($params['field'],'.') !== false ){
1190  list($junk,$fieldname) = explode('.', $params['field']);
1191  } else {
1192  $fieldname = $params['field'];
1193  }
1194  $t =& $this->_relationship->getTable($fieldname);
1195  $rec = $this->toRecord($t->tablename);
1196 
1197 
1198  $perms = $rec->getPermissions(array('field'=>$fieldname, 'nobubble'=>1));
1199  if ( !$perms ) $perms = array();
1200 
1201 
1202 
1203 
1204  $rfperms = $this->_record->getPermissions(array('relationship'=>$this->_relationshipName, 'field'=>$fieldname, 'nobubble'=>1));
1205  //echo "RFPerms: ";print_r($rfperms);
1206  if ( $rfperms ){
1207  foreach ($rfperms as $k=>$v){
1208  $perms[$k] = $v;
1209  }
1210  }
1211 
1212  unset($params['field']);
1213  $recPerms = $this->getPermissions($params, $t->tablename);
1214 
1215 
1216  foreach ($perms as $k=>$v){
1217  $recPerms[$k] = $v;
1218  }
1219 
1220  //print_r($perms);
1221  return $recPerms;
1222  } else {
1223  $domainTable = $this->_relationship->getDomainTable();
1224  $destinationTables = $this->_relationship->getDestinationTables();
1225  $isManyToMany = $this->_relationship->isManyToMany();
1226  $targetTable = $table;
1227  if ( !@$targetTable ){
1228  if ( $isManyToMany ){
1229  foreach ($destinationTables as $candidateTable){
1230  if ( strcmp($candidateTable->tablename, $domainTable) !== 0 ){
1231  $targetTable = $candidateTable->tablename;
1232  break;
1233  }
1234  }
1235  }
1236  }
1237  if ( !@$targetTable ){
1238  $targetTable = $domainTable;
1239  }
1240 
1241  $parentPerms = $this->_record->getPermissions(array('relationship'=>$this->_relationshipName));
1242  $domainRecord = $this->toRecord($targetTable);
1243 
1244 
1245  $isDomainTable = (strcmp($domainTable, $targetTable) === 0 );
1246 
1247 
1248  $perms = $domainRecord->getPermissions();
1249  if ( $isManyToMany ){
1250  if ( @$parentPerms['add new related record'] ){
1251  $perms['new'] = 1;
1252  } else if ( @$parentPerms['add existing related record'] and !$isDomainTable ){
1253  $perms['new'] = 1;
1254  } else if ( $isDomainTable and isset($parentPerms['add new related record']) and !@$parentPerms['add new related record'] ){
1255  $perms['new'] = 0;
1256  } else if ( isset($parentPerms['add existing related record']) and !@$parentPerms['add existing related record'] ){
1257  $perms['new'] = 0;
1258  }
1259 
1260  if ( @$parentPerms['delete related record'] ){
1261  $perms['delete'] = 1;
1262  } else if ( $isDomainTable and isset($parentPerms['delete related record']) and !@$parentPerms['delete related record'] ){
1263  $perms['delete'] = 0;
1264  } else if ( !$isDomainTable and @$parentPerms['remove related record'] ){
1265  $perms['delete'] = 1;
1266  } else if ( !$isDomainTable and isset($parentPerms['remove related record']) and !@$parentPerms['remove related record'] ){
1267  $perms['delete'] = 0;
1268  }
1269 
1270  if ( !$isDomainTable ){
1271  if ( @$parentPerms['edit related records'] ){
1272  $perms['edit'] = 1;
1273  } else if ( isset($parentPerms['edit related records']) and !@$parentPerms['edit related records'] ){
1274  $perms['edit'] = 0;
1275  }
1276 
1277  if ( @$parentPerms['link related records'] ){
1278  $perms['link'] = 1;
1279  } else if ( isset($parentPerms['link related records']) and !@$parentPerms['link related records'] ){
1280  $perms['link'] = 0;
1281  }
1282  }
1283 
1284 
1285  } else {
1286  if ( @$parentPerms['add new related record'] ){
1287  $perms['new'] = 1;
1288  } else if ( isset($parentPerms['add new related record']) and !@$parentPerms['add new related record'] ){
1289  $perms['new'] = 0;
1290  }
1291 
1292  if ( @$parentPerms['delete related record'] ){
1293  $perms['delete'] = 1;
1294  } else if ( isset($parentPerms['delete related record']) and !@$parentPerms['delete related record'] ){
1295  $perms['delete'] = 0;
1296  }
1297  if ( @$parentPerms['edit related records'] ){
1298  $perms['edit'] = 1;
1299  } else if ( isset($parentPerms['edit related records']) and !@$parentPerms['edit related records'] ){
1300  $perms['edit'] = 0;
1301  }
1302  if ( @$parentPerms['link related records'] ){
1303  $perms['link'] = 1;
1304  } else if ( isset($parentPerms['link related records']) and !@$parentPerms['link related records'] ){
1305  $perms['link'] = 0;
1306  }
1307  }
1308 
1309 
1310 
1311  if ( @$parentPerms['view related records'] ){
1312  $perms['view'] = 1;
1313  } else if ( isset($parentPerms['view related records']) and !@$parentPerms['view related records'] ){
1314  $perms['view'] = 0;
1315  }
1316  if ( @$parentPerms['find related records'] ){
1317  $perms['find'] = 1;
1318  } else if ( isset($parentPerms['find related records']) and !@$parentPerms['find related records'] ){
1319  $perms['find'] = 0;
1320  }
1321 
1322 
1323  /*
1324  foreach ( $this->toRecords() as $record){
1325  $rperms = $record->getPermissions(array());
1326  if ( $perms ){
1327  $perms = array_intersect_assoc($perms, $rperms);
1328 
1329  } else {
1330  $perms = $rperms;
1331  }
1332 
1333  }
1334  */
1335  return $perms;
1336 
1337  }
1338  }
1339 
1356  function checkPermission($perm, $params=array()){
1357  $perms = $this->getPermissions($params);
1358  return @$perms[$perm]?1:0;
1359 
1360  }
1361 
1362 
1363  // @}
1364  // End Permissions
1365  //----------------------------------------------------------------------------------------------
1366 
1367 
1368 
1369 
1370 
1371 
1372 
1373 
1374 
1375 
1376 }