aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporterRelations.class.php
blob: 956d2878a1d3a97e95d97954d2e85889de63083b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

/**
 * IsisImporterRelations: provides ISIS import methods for importing data
 * into model entities.
 */
class sfIsisImporterRelations extends sfIsisImporterEntities {
  /**
   * Import one to one data.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param string $relation Relation name
   */
  public function addOneToOne(&$model, array $field, $relation)
  {
    foreach (new IsisRowIterator($this->isis, $field) as $row)
    {
      $data = new $relation();

      $this->addMain($data, $field, $row);
      $this->addSubfields($data, $field, $row);
      $data->save();

      $key         = sfInflector::underscore($relation) .'_id';
      $model->$key = $data->id;
    }
  }

  /**
   * Import one to many data.
   *
   * @param  object $model    Model
   * @param  string $relation Relation name
   * @return object           Relation model object
   */  
  public function addOneToMany(&$model, $relation)
  {
    $model_id          = $this->getModelId($model);
    $data              = new $relation();
    $data->{$model_id} = $model->id;
    $data->save();
    return $data;
  }

  /**
   * Add simple entities data into the model.
   *
   * @param object $model Model
   * @param array  $field Field data from ISIS database schema
   */  
  public function addOneToManyMain(&$model, array $field, $entity, $key = 'name')
  {
    foreach (new IsisMainItemIterator($this->isis, $field) as $row => $value)
    {
      $this->log("Entity: $entity; Value: $value", 'debug');
      $data         = $this->addOneToMany($model, $entity);
      $data->{$key} = $value;
      $data->save();
    }    
  }

  /**
   * Import a single value to a many to many data model.
   *
   * @param object $model    Model
   * @param mixed  $values   Values to be added
   * @param string $relation Relation name
   * @param string $key      Value key
   */
  public function addManyToMany(&$model, $value, $relation, $key = 'name')
  {
    return $this->addToSharedModel($model, $value, $relation, $key, 'add');
  }

  /**
   * Import a single value to a shared data model.
   *
   * @param object $model    Model
   * @param mixed  $values   Values to be added
   * @param string $relation Relation name
   * @param string $key      Value key
   * @param string $strategy Strategy do add the content into the database
   */
  public function addToSharedModel(&$model, $value, $relation, $key = 'name', $strategy = 'add')
  {
    $method = $strategy . $relation;

    // Populate related data.
    if (is_callable(array($this, $method)))
    {
      $data = $this->{$method}($value);
    }
    else
    {
      $data = $this->{$strategy .'Entity'}($relation, $value, $key);
    }

    // Get model and relation names and id fields.
    $model_id                   = $this->getModelId($model);
    $relation_id                = $this->getRelationId($relation);
    $model_relation             = $this->getModelRelation($model, $relation);

    // Make the relation.
    $model_data                 = new $model_relation();
    $model_data->{$model_id}    = $model->id;
    $model_data->{$relation_id} = $data->id;
    $model_data->save();

    return $model_data;
  }

  /**
   * Import many to many data.
   *
   * @param object $model    Model
   * @param array  $values   Values to be added
   * @param string $relation Relation name
   * @param string $key      Value key
   */
  public function addManyToManyEntities(&$model, array $values, $relation, $key = 'name')
  {
    foreach ($values as $value)
    {
      $this->addManyToMany($model, $value, $relation, $key);
    }
  }

  /**
   * Add field values in a many-to-many relation.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param string $relation Relation name
   * @param string $key      Value key
   */
  public function addManyToManyMain(&$model, array $field, $relation, $key = 'name')
  {
    foreach (new IsisMainItemIterator($this->isis, $field) as $value)
    {
      $this->addManyToManyEntities($model, $this->isis->explodeBrackets($value), $relation, $key);
    }    
  }
}