Beispiel #1
0
 /**
  * Replace all ITEM:* placeholders contained in a URL (or a string).
  *
  * @param string   $url   The URL wherein will be replaced the placeholders.
  * @param F0FTable $table A table object instance from which get known fields to replace.
  *
  * @return string The replaced URL.
  */
 private function getReplacedPlaceholders($url, F0FTable &$table)
 {
     // Ask to table which are the fields to replace
     $fields = $table->getKnownFields();
     // Replace all placeholders in the URL
     foreach ($fields as $field) {
         $url = str_replace('[ITEM:' . strtoupper($field) . ']', $table->get($field), $url);
     }
     return $url;
 }
 /**
  * Create a relations object based on the provided F0FTable instance
  *
  * @param   F0FTable   $table  The table instance used to initialise the relations
  */
 public function __construct(F0FTable $table)
 {
     // Store the table
     $this->table = $table;
     // Get the table's type from its name
     $tableName = $table->getTableName();
     $tableName = str_replace('#__', '', $tableName);
     $type = explode("_", $tableName);
     if (count($type) == 1) {
         $this->tableType = array_pop($type);
     } else {
         $this->componentName = array_shift($type);
         $this->tableType = array_pop($type);
     }
     $this->tableType = F0FInflector::singularize($this->tableType);
     $tableKey = $table->getKeyName();
     unset($type);
     // Scan all table keys and look for foo_bar_id fields. These fields are used to populate parent relations.
     foreach ($table->getKnownFields() as $field) {
         // Skip the table key name
         if ($field == $tableKey) {
             continue;
         }
         if (substr($field, -3) != '_id') {
             continue;
         }
         $parts = explode('_', $field);
         // If the component type of the field is not set assume 'joomla'
         if (count($parts) == 2) {
             array_unshift($parts, 'joomla');
         }
         // Sanity check
         if (count($parts) != 3) {
             continue;
         }
         // Make sure we skip any references back to ourselves (should be redundant, due to key field check above)
         if ($parts[1] == $this->tableType) {
             continue;
         }
         // Default item name: the name of the table, singular
         $itemName = F0FInflector::singularize($parts[1]);
         // Prefix the item name with the component name if we refer to a different component
         if ($parts[0] != $this->componentName) {
             $itemName = $parts[0] . '_' . $itemName;
         }
         // Figure out the table class
         $tableClass = ucfirst($parts[0]) . 'Table' . ucfirst($parts[1]);
         $default = empty($this->relations['parent']);
         $this->addParentRelation($itemName, $tableClass, $field, $field, $default);
     }
     // Get the relations from the configuration provider
     $key = $table->getConfigProviderKey() . '.relations';
     $configRelations = $table->getConfigProvider()->get($key, array());
     if (!empty($configRelations)) {
         foreach ($configRelations as $relation) {
             if (empty($relation['type'])) {
                 continue;
             }
             if (isset($relation['pivotTable'])) {
                 $this->addMultipleRelation($relation['itemName'], $relation['tableClass'], $relation['localKey'], $relation['ourPivotKey'], $relation['theirPivotKey'], $relation['remoteKey'], $relation['pivotTable'], $relation['default']);
             } else {
                 $method = 'add' . ucfirst($relation['type']) . 'Relation';
                 if (!method_exists($this, $method)) {
                     continue;
                 }
                 $this->{$method}($relation['itemName'], $relation['tableClass'], $relation['localKey'], $relation['remoteKey'], $relation['default']);
             }
         }
     }
 }