/**
  * @brief Loads the values for a particular field in a particular table.
  *
  * This method is delegated to by handle() for cleanliness purposes.
  *
  * @param Dataface_Table $table The table where the depselect field resides.
  * @param array &$field The field definition of the depselect field.
  * @param array $query The current GET parameters.
  * 
  */
 private function loadFromTable(Dataface_Table $table, &$field, $query)
 {
     try {
         $perms = $table->getPermissions(array('field' => $field['name']));
         if (!@$perms['edit'] and !@$perms['new']) {
             // The user doesn't have permission to edit the column... so we
             // need to cut off access right now!!!
             error_log("Insufficient permissions to access field {$field['name']} by current user.");
             throw new Exception("Failed to get options for field {$filed['name']} because you don't have view permission for this field.", 401);
         }
         if (!@$field['widget']['table']) {
             error_log("widget:table not defined for field " . $field['name'] . " of table " . $table->tablename . ".");
             throw new Exception(self::errorMessage($fieldname), 500);
         }
         $targetTable = Dataface_Table::loadTable($field['widget']['table']);
         $filters = array();
         if (@$field['widget']['filters'] and is_array($field['widget']['filters'])) {
             foreach ($field['widget']['filters'] as $key => $val) {
                 if (isset($query[$key])) {
                     $filters[$key] = $query[$key];
                 } else {
                     if (strpos($val, '$') === 0) {
                         $filters[$key] = '=';
                     } else {
                         $filters[$key] = $val;
                     }
                 }
             }
         }
         $limit = 250;
         if (@$field['widget']['limit']) {
             $limit = intval($field['widget']['limit']);
         }
         $filters['-limit'] = $limit;
         $records = df_get_records_array($field['widget']['table'], $filters);
         //if ( count($filters) > 1 ){
         //	echo "Num records found: ".count($records);
         //	print_r($filters);
         //}
         $keyCol = null;
         $labelCol = null;
         $multiLabel = null;
         if (@$field['widget']['keycol']) {
             $keyCol = $field['widget']['keycol'];
         } else {
             foreach ($targetTable->keys() as $k => $v) {
                 $keyCol = $k;
                 break;
             }
         }
         if (@$field['widget']['labelcol']) {
             $labelCol = $field['widget']['labelcol'];
         } else {
             if (@$field['widget']['multilabelformat']) {
                 $multiLabel = $field['widget']['multilabelformat'];
                 $params = explode('"', $multiLabel);
                 // The first array elements are empty due to the first '"' and ','.
                 $format = $params[1];
                 $colums = explode(',', $params[2]);
                 array_shift($colums);
             }
         }
         $out = array();
         foreach ($records as $r) {
             if (@$field['widget']['ignore_permissions']) {
                 $r->secureDisplay = false;
             } else {
                 //if ( !$r->checkPermission('view') ) continue;
                 if (!$r->checkPermission('view', array('field' => $keyCol))) {
                     continue;
                 }
                 if ($labelCol and !$r->checkPermission('view', array('field' => $labelCol))) {
                     continue;
                 }
             }
             if ($labelCol) {
                 $temp = array($r->val($keyCol) => $r->display($labelCol));
             } else {
                 if ($multiLabel) {
                     $values = array();
                     foreach ($colums as $field) {
                         $values[] = $r->display(trim($field));
                     }
                     $temp = array($r->val($keyCol) => vsprintf($format, $values));
                 } else {
                     $temp = array($r->val($keyCol) => $r->getTitle());
                 }
             }
             $out[] = $temp;
         }
         $this->out(array('code' => 200, 'message' => 'Received default valuelist', 'values' => $out));
         exit;
     } catch (Exception $ex) {
         $this->out(array('code' => $ex->getCode(), 'message' => $ex->getMessage()));
         exit;
     }
 }