Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EditorController.php
1 <?php
2 
4 {
5  public function filters()
6  {
7  return array(
8  'accessControl',
9  );
10  }
11  public function accessRules()
12  {
13  return array(
14  array('allow', // allow authenticated user to perform 'create' and 'update' actions
15  'users'=>array('*'),
16  ),
17  //array('deny', // deny all users
18  // 'users'=>array('*'),
19  //),
20  );
21  }
22 
23  public function actionInit()
24  {
25  $response = array();
26  $response['assetsPath'] = $this->module->assetsPath;
27  FGHelper::sendJSON($response);
28  }
29 
30  public function actionLoadLibrary()
31  {
32  $templatesModel = FGTemplates::model()->findAll('user=:uid OR global = 1', array(':uid'=>Yii::app()->user->id));
33  $templates = array(
34  'global'=>array(),
35  'user'=>array()
36  );
37  foreach($templatesModel as $tpl) {
38  $templates[ $tpl->global? 'global' : 'user' ][] = array(
39  'id' => $tpl->id,
40  'name' => $tpl->name
41  );
42  }
43  FGHelper::sendJSON(array(
44  'templates' => $templates
45  ));
46  }
47 
48  public function actionLoadForm()
49  {
50  if(isset($_POST['id']))
51  {
52  $model = FGForms::model()->findByPk((int)$_POST['id']);
53  // correct ID in JSON
54  $model->json = str_replace('"id": 0,', '"id": '.$model->id.',', $model->json);
55 
56  if($model) {
57  FGHelper::sendJSON(array(
58  'id'=>$model->id,
59  'name'=>$model->name,
60  'json'=>$model->json
61  ));
62  exit;
63  } else {
64  throw new CHttpException(404, "No Form found");
65  }
66  }
67  else
68  {
69  throw new CHttpException(500, "No Form id provided");
70  }
71  }
72 
73  public function actionLoadTemplate()
74  {
75  if(isset($_POST['id']))
76  {
77  $model = FGTemplates::model()->findByPk((int)$_POST['id'], 'user=:uid OR global = 1', array(':uid'=>Yii::app()->user->id));
78  if($model) {
79  FGHelper::sendJSON(array(
80  'name'=>$model->name,
81  'json'=>$model->json
82  ));
83  } else {
84  throw new CHttpException(404, "No template found");
85  }
86  }
87  else
88  {
89  throw new CHttpException(500, "No template id provided");
90  }
91  }
92 
93  /**
94  * @desc save template
95  */
96  public function actionSaveTemplate()
97  {
98  if(isset($_POST['json']))
99  {
100  $model = new FGTemplates();
101  $model->setAttributes($_POST);
102  $model->user = Yii::app()->user->id;
103  if($model->save())
104  {
105  FGHelper::sendJSON(array(
106  'id'=>$model->id,
107  'name'=>$model->name
108  ));
109  }
110  else
111  {
112  throw new CHttpException(500, CActiveForm::validate($model));
113  }
114  }
115  else
116  {
117  throw new CHttpException(500, "No JSON provided");
118  }
119  }
120 
121  /**
122  * @desc save form
123  */
124  public function actionSave()
125  {
126  if(count($_POST))
127  {
128  $response = array();
129  $model = new FGForms();
130  $model->setAttributes($_POST);
131  $model->user = Yii::app()->user->id;
132  $model->thankyoupage = $_POST['thankyoupage'];
133  $model->nextpage = $_POST['nextpage'];
134  $model->prevpage = $_POST['prevpage'];
135 
136  if (!empty($_POST['id']) && $_POST['id'] != "" && $_POST['id'] != "0"){
137  if($model->updateByPk((int)$_POST['id'],
138  array( "name" => $_POST['name'],
139  "json" => $_POST['json'],
140  "html" => $_POST['html'],
141  "thankyoupage" => $_POST['thankyoupage'],
142  "nextpage" => $_POST['nextpage'],
143  "prevpage" => $_POST['prevpage']
144  )
145  ))
146  {
147  $response['status'] = true;
148  $response['id'] = $_POST['id'];
149  }
150  else
151  {
152  $response['status'] = false;
153  $response['errors'] = $model->getErrors();
154  }
155 
156  } else {
157  if($model->save())
158  {
159  $response['status'] = true;
160  $response['id'] = $model->id;
161  }
162  else
163  {
164  $response['status'] = false;
165  $response['errors'] = $model->getErrors();
166  }
167  }
168  }
169  else
170  {
171  throw new CHttpException(500, "No JSON provided");
172  }
173  FGHelper::sendJSON($response);
174  }
175 }