Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
    function afterSave(&$model, $created) {
        if (!isset($this->runtime[$model->alias]['beforeSave'])) {
            return true;
        }
        $locale = $this->_getLocale($model);
        $tempData = $this->runtime[$model->alias]['beforeSave'];
        unset($this->runtime[$model->alias]['beforeSave']);
        $conditions = array('locale' => $locale, 'model' => $model->alias, 'foreign_key' => $model->id);
        $RuntimeModel =& $this->translateModel($model);
        if (empty($created)) {
            $translations = $RuntimeModel->find('list', array('conditions' => array_merge($conditions, array($RuntimeModel->displayField => array_keys($tempData)))));
            if ($translations) {
                foreach ($translations as $id => $field) {
                    $RuntimeModel->create();
                    $RuntimeModel->save(array($RuntimeModel->alias => array('id' => $id, 'content' => $tempData[$field])));
                    unset($tempData[$field]);
                }
            }
        }
       
        if (!empty($tempData)) {
            foreach ($tempData as $field => $value) {
                if (is_array($value)) {
                    foreach ($value as $loc=>$val) {
                        $tmploc = array('locale'=>$loc);
                        $RuntimeModel->create(array_unique(array_merge($conditions,$tmploc, array($RuntimeModel->displayField => $field, 'content' => $val))));
                        $RuntimeModel->save();
                    }
                }
                else {
                    $RuntimeModel->create(array_merge($conditions, array($RuntimeModel->displayField => $field, 'content' => $value)));
                    $RuntimeModel->save();
                }
            }
        }
    }