PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/application/protected/extensions/gtc/components/Relation.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 560 lines | 362 code | 0 blank | 198 comment | 81 complexity | 7c697d8d1bca3f45803d1aeec21aabf8 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. The Relation widget is used in forms, where the User can choose
  4. between a selection of model elements, that this models belongs to.
  5. It is able to handle BELONGS_TO, HAS_ONE and MANY_MANY Relations. The Relation
  6. type is detected automatically from the Model 'relations()' section.
  7. The Widget has different styles in which it can render the possible choices.
  8. Use the 'style' option to set the appropriate style.
  9. The following example shows how to use Relation with a minimal config,
  10. assuming we have a Model "Post" and "User", where one User belongs
  11. to a Post:
  12. <pre>
  13. $this->widget('application.components.Relation', array(
  14. 'model' => 'Post',
  15. 'relation' => 'user'
  16. 'fields' => 'username' // show the field "username" of the parent element
  17. ));
  18. </pre>
  19. Results in a drop down list in which the user can choose between
  20. all available Users in the Database. The shown field of the
  21. Table "User" is "username" in this example.
  22. You can choose the Style of your Widget in the 'style' option.
  23. Note that a Many_Many Relation always gets rendered as a Listbox,
  24. since you can select multiple Elements.
  25. 'fields' can be an array or an string.
  26. If you pass an array to 'fields', the Widget will display every field in
  27. this array. If you want to show further sub-relations, separate the values
  28. with '.', for example: 'fields' => 'array('parent.grandparent.description')
  29. Optional Parameters:
  30. You can use 'field' => 'post_userid' if the field in the model
  31. that represents the foreign model is called different than in the
  32. relation
  33. Use 'relatedPk' => 'id_of_user' if the primary Key of the Foreign
  34. Model differs from the one given in the relation.
  35. Normally you shouldn´t use this fields cause the Widget get the relations
  36. automatically from the relation.
  37. Use 'allowEmpty' to let the user be able to choose no parent. If you
  38. set this to a string, this string will be displayed with the available
  39. choices.
  40. With 'showAddButton' => 'false' you can disable the 'create new Foreignkey'
  41. Button generated beside the Selectbox.
  42. Define the AddButtonString with 'addButtonString' => 'Add...'. This string
  43. is set default to '+'
  44. When using the '+' button you most likely want to return to where you came.
  45. To accomplish this, we pass a 'returnTo' parameter by $_GET.
  46. The Controller can send the user back to where he came from this way:
  47. <pre>
  48. if($model->save())
  49. if(isset($_GET['returnTo']))
  50. $this->redirect(array(urldecode($_GET['returnTo'])));
  51. </pre>
  52. Using the 'style' option we can configure how our Widget gets rendered.
  53. The following styles are available:
  54. Selectbox (default), Listbox, Checkbox and in MANY_MANY relations 'twopane'
  55. The style is case insensitive so one can use dropdownlist or dropDownList.
  56. Use the option 'createAction' if the action to add additional foreign Model
  57. options differs from 'create'.
  58. With 'parentObjects' you can limit the Parent Elements that are being shown.
  59. It takes an array of elements that could be returned from an scope or
  60. an SQL Query.
  61. The parentObjects can be grouped, for example, with
  62. 'groupParentsBy' => 'city'
  63. Use the option 'htmlOptions' to pass any html Options to the
  64. Selectbox/Listbox form element.
  65. Full Example:
  66. <pre>
  67. $this->widget('application.components.Relation', array(
  68. 'model' => 'Post',
  69. 'field' => 'Userid',
  70. 'style' => 'ListBox',
  71. 'criteria' => new CDBCriteria(array('condition' => 'id_of_user IN (1,8,20)')),
  72. 'parentObjects' => Parentmodel::model()->findAll('userid = 17'),
  73. 'groupParentsBy' => 'city',
  74. 'relation' => 'user',
  75. 'relatedPk' => 'id_of_user',
  76. 'fields' => array( 'username', 'username.group.groupid' ),
  77. 'delimiter' => ' -> ', // default: ' | '
  78. 'returnTo' => 'model/create',
  79. 'addButtonUrl' => 'othercontroller/otheraction', // default: ''
  80. 'showAddButton' => 'click here to add a new User', // default: ''
  81. 'htmlOptions' => array('style' => 'width: 100px;')
  82. ));
  83. </pre>
  84. @author Herbert Maschke <thyseus@gmail.com>
  85. @version 0.97 (after 1.0rc5)
  86. @since 1.1
  87. */
  88. class Relation extends CWidget {
  89. // this Variable holds an instance of the Object
  90. protected $_model;
  91. // this Variable holds an instance of the related Object
  92. protected $_relatedModel;
  93. // draw the relation of which model?
  94. public $model;
  95. // which relation should be rendered?
  96. public $relation;
  97. public $field;
  98. // the Primary Key of the foreign Model
  99. public $relatedPk;
  100. // a field or an array of fields that determine which field values
  101. // should be rendered in the selection
  102. public $fields;
  103. // if this is set, the User is able to select no related model
  104. // if this is set to a string, this string will be presented
  105. public $allowEmpty = 0;
  106. // Preselect which items?
  107. public $preselect = false;
  108. // disable this to hide the Add Button
  109. // set this to a string to set the String to be displayed
  110. public $showAddButton = FALSE;
  111. public $addButtonUrl = '';
  112. // url for the action that refreshes the dropdownlist after related
  113. // object is created
  114. public $addButtonRefreshUrl = '';
  115. // criteria for filtering records
  116. public $criteria = false;
  117. // How the label of a row should be rendered. {id} will be replaced by the
  118. // id of the model. You can also insert every field that is available in the
  119. // parent object.
  120. // Use {fields} to display all fields delimited by $this->delimiter
  121. // Use {myFuncName} to evaluate a user-contributed function specified in the
  122. // $functions array as 'myFuncName'=>'code to be evaluated'. The code for
  123. // these functions are evaluated under the context of the controller
  124. // rendering the current Relation widget ($this refers to the controller).
  125. // Old way, not encouraged anymore: Use {func0} to {funcX} to evaluate user-
  126. // contributed functions specified in the $functions array as a keyless
  127. // string entry of 'code to be evaluated'.
  128. // Example of code:
  129. //
  130. // 'template' => '#{id} : {fields} ({title}) Allowed other Models: {func0} {func1} {preferredWay}',
  131. // 'functions' => array(
  132. // "CHtml::checkBoxList('parent{id}', '', CHtml::listData(Othermodel::model()->findAll(), 'id', 'title'));",
  133. // '$this->funcThatReturnsText();'
  134. // 'preferredWay' => '$this->instructMe();'
  135. // ),
  136. public $template = '{fields}';
  137. // User-contributed functions, see comment for $template.
  138. public $functions = array();
  139. // If true, all the user-contributed functions in $functions will be
  140. // substituted in $htmlOptions['template'] as well.
  141. // If an array of function names, all the listed functions will be
  142. // substituted in $htmlOptions['template'] as well.
  143. public $functionsInHtmlOptionsTemplate = false;
  144. // how should multiple fields be delimited
  145. public $delimiter = " | ";
  146. // style of the selection Widget
  147. public $style = "dropDownList";
  148. public $htmlOptions = array();
  149. public $parentObjects = 0;
  150. public $orderParentsBy = 0;
  151. public $groupParentsBy = 0;
  152. // override this for complicated MANY_MANY relations:
  153. public $manyManyTable = '';
  154. public $manyManyTableLeft = '';
  155. public $manyManyTableRight = '';
  156. public $num = 1;
  157. public function init() {
  158. if (!is_object($this->model)) {
  159. if (!$this->_model = new $this->model) throw new CException(Yii::t('yii', 'Relation widget is not able to instantiate the given Model'));
  160. } else {
  161. $this->_model = $this->model;
  162. }
  163. // Instantiate Model and related Model
  164. foreach ($this->_model->relations() as $key => $value) {
  165. if (strcmp($this->relation, $key) == 0) {
  166. // $key = Name of the Relation
  167. // $value[0] = Type of the Relation
  168. // $value[1] = Related Model
  169. // $value[2] = Related Field or Many_Many Table
  170. switch ($value[0]) {
  171. case 'CBelongsToRelation':
  172. case 'CHasOneRelation':
  173. $this->_relatedModel = new $value[1];
  174. if (!isset($this->field)) {
  175. $this->field = $value[2];
  176. }
  177. break;
  178. case 'CManyManyRelation':
  179. preg_match_all('/^.*\(/', $value[2], $matches);
  180. $this->manyManyTable = substr($matches[0][0], 0, strlen($matches[0][0]) - 1);
  181. preg_match_all('/\(.*,/', $value[2], $matches);
  182. $this->manyManyTableLeft = substr($matches[0][0], 1, strlen($matches[0][0]) - 2);
  183. preg_match_all('/,.*\)/', $value[2], $matches);
  184. $this->manyManyTableRight = substr($matches[0][0], 2, strlen($matches[0][0]) - 3);
  185. $this->_relatedModel = new $value[1];
  186. break;
  187. }
  188. }
  189. }
  190. if (!is_object($this->_relatedModel)) throw new CException(Yii::t('yii', 'Relation widget cannot find the given Relation(' . $this->relation . ')'));
  191. if (!isset($this->relatedPk) || $this->relatedPk == "") {
  192. $this->relatedPk = $this->_relatedModel->tableSchema->primaryKey;
  193. }
  194. if (!isset($this->fields) || $this->fields == "" || $this->fields == array()) throw new CException(Yii::t('yii', 'Widget "Relation" has been run without fields Option(string or array)'));
  195. }
  196. // Check if model-value contains '.' and generate -> directives:
  197. public function getModelData($model, $field) {
  198. if (strstr($field, '.')) {
  199. $data = explode('.', $field);
  200. $value = $model->getRelated($data[0])->$data[1];
  201. } else $value = $model->$field;
  202. return $value;
  203. }
  204. /**
  205. * This function fetches all needed data of the related Object and returns them
  206. * in an array that is prepared for use in ListData.
  207. */
  208. public function getRelatedData() {
  209. /* At first we determine, if we want to display all parent Objects, or
  210. * if the User supplied an list of Objects */
  211. if (is_object($this->parentObjects)) // a single Element
  212. {
  213. $parentobjects = array(
  214. $this->parentObjects
  215. );
  216. } else if (is_array($this->parentObjects)) // Only show this elements
  217. {
  218. $parentobjects = $this->parentObjects;
  219. } else
  220. // Show all Parent elements
  221. {
  222. if ($this->criteria === false) {
  223. $parentobjects = CActiveRecord::model(get_class($this->_relatedModel))->findAll(array(
  224. 'order' => GHelper::guessNameColumn($this->_relatedModel->tableSchema->columns)
  225. ));
  226. } else {
  227. $parentobjects = CActiveRecord::model(get_class($this->_relatedModel));
  228. $parentobjects->setDbCriteria($this->criteria);
  229. $parentobjects = $parentobjects->findAll(array(
  230. 'order' => GHelper::guessNameColumn($this->_relatedModel->tableSchema->columns)
  231. ));
  232. }
  233. }
  234. if ($this->allowEmpty) if (is_string($this->allowEmpty)) $dataArray[''] = $this->allowEmpty;
  235. else $dataArray[''] = Yii::t('app', 'None');
  236. foreach ($parentobjects as $obj) {
  237. if (!is_array($this->fields)) $this->fields = array(
  238. $this->fields
  239. );
  240. $fields = '';
  241. $i = 0;
  242. foreach ($this->fields as $field) {
  243. $rule = sprintf('{%s}', $field);
  244. $rules[$rule] = $obj->$field;
  245. if ($i++ > 0) $fields.= $this->delimiter;
  246. $fields.= $this->getModelData($obj, $field);
  247. }
  248. $defaultrules = array(
  249. '{fields}' => $fields,
  250. '{id}' => $obj->{$obj->tableSchema->primaryKey}
  251. );
  252. // Look for user-contributed functions and evaluate them
  253. if ($this->functions != array()) {
  254. foreach ($this->functions as $key => $function) {
  255. // If the key is of type string, it's assumed to be a named function,
  256. // used like {myFuncName}.
  257. // If the key is an integer, it's assumed to be an unnamed function used
  258. // the old way, {funcX} where X is its index in the functions array.
  259. // We keep the integer support mostly for backwards compatibility, the
  260. // new way is encouraged.
  261. if (is_string($key)) {
  262. $funcrules[sprintf('{%s}', $key) ] = $this->controller->evaluateExpression(strtr($function, $defaultrules));
  263. } else {
  264. $funcrules[sprintf('{func%d}', $key) ] = $this->controller->evaluateExpression(strtr($function, $defaultrules));
  265. }
  266. }
  267. }
  268. // Merge the evaluated rules, if exist
  269. if (isset($funcrules)) $rules = array_merge($rules, $funcrules);
  270. // Merge the default rules into our ruleset
  271. $rules = array_merge($rules, $defaultrules);
  272. // Apply the rules to the template
  273. $value = strtr($this->template, $rules);
  274. // Apply the user contributed functions to $htmlOptions's template, if requested.
  275. if (isset($this->htmlOptions['template']) && $this->functionsInHtmlOptionsTemplate !== false && isset($funcrules) && is_array($funcrules)) {
  276. if (is_array($this->functionsInHtmlOptionsTemplate)) {
  277. $funcrulesToUse = array();
  278. foreach ($this->functionsInHtmlOptionsTemplate as $functionName) {
  279. $functionName = sprintf('{%s}', $functionName);
  280. if (isset($funcrules[$functionName])) {
  281. $funcrulesToUse[$functionName] = $funcrules[$functionName];
  282. }
  283. }
  284. $this->htmlOptions['template'] = strtr($this->htmlOptions['template'], $funcrulesToUse);
  285. } else {
  286. $this->htmlOptions['template'] = strtr($this->htmlOptions['template'], $funcrules);
  287. }
  288. }
  289. if ($this->groupParentsBy != '') {
  290. $dataArray[$obj->{$this->groupParentsBy}][$obj->{$this->relatedPk}] = $value;
  291. } else {
  292. $dataArray[$obj->{$this->relatedPk}] = $value;
  293. }
  294. }
  295. if (!isset($dataArray) || !is_array($dataArray)) $dataArray = array();
  296. return $dataArray;
  297. }
  298. /**
  299. * Retrieves the Assigned Objects of the MANY_MANY related Table
  300. */
  301. public function getAssignedObjects() {
  302. if (!$this->_model->{$this->_model->tableSchema->primaryKey}) return array();
  303. $sql = sprintf("select * from %s where %s = %s", $this->manyManyTable, $this->manyManyTableLeft, $this->_model->{$this->_model->tableSchema->primaryKey});
  304. $result = $this->_model->getDbConnection()->createCommand($sql)->queryAll();
  305. foreach ($result as $foreignObject) {
  306. $id = $foreignObject[$this->manyManyTableRight];
  307. $objects[$id] = $this->_relatedModel->findByPk($id);
  308. }
  309. foreach ($this->_model->{$this->relation} as $relobj) $objects[$relobj->id] = $relobj;
  310. return isset($objects) ? $objects : array();
  311. }
  312. /**
  313. * Retrieves the not Assigned Objects of the MANY_MANY related Table
  314. * This is used in the two-pane style view.
  315. */
  316. public function getNotAssignedObjects() {
  317. foreach ($this->getRelatedData() as $key => $value) {
  318. if (!array_key_exists($key, $this->getAssignedObjects())) {
  319. $objects[$key] = $this->_relatedModel->findByPk($key);
  320. }
  321. }
  322. return $objects ? $objects : array();
  323. }
  324. /**
  325. * Gets the Values of the given Object or Objects depending on the
  326. * $this->fields the widget requests
  327. */
  328. public function getObjectValues($objects) {
  329. if (is_array($objects)) {
  330. foreach ($objects as $object) {
  331. $attributeValues[$object->primaryKey] = $object->{$this->fields};
  332. }
  333. } else if (is_object($objects)) {
  334. $attributeValues[$object->primaryKey] = $objects->{$this->fields};
  335. }
  336. return isset($attributeValues) ? $attributeValues : array();
  337. }
  338. /*
  339. * How will the Listbox of the MANY_MANY Assignment be called?
  340. */
  341. public function getListBoxName($ajax = false) {
  342. if ($ajax) {
  343. return sprintf('%s_%s', get_class($this->_model) , get_class($this->_relatedModel));
  344. } else {
  345. return sprintf('%s[%s]', get_class($this->_model) , get_class($this->_relatedModel));
  346. }
  347. }
  348. public function renderBelongsToSelection() {
  349. CWidget::render(strtolower($this->style) , array(
  350. 'id' => $this->relation . '_options',
  351. 'model' => $this->_model,
  352. 'field' => $this->field,
  353. 'data' => $this->getRelatedData() ,
  354. 'htmlOptions' => $this->htmlOptions
  355. ));
  356. }
  357. public function renderManyManySelection() {
  358. if (strcasecmp($this->style, 'twopane') == 0) $this->renderTwoPaneSelection();
  359. else if (strcasecmp($this->style, 'checkbox') == 0) $this->renderCheckBoxListSelection();
  360. else if (strcasecmp($this->style, 'dropDownList') == 0) $this->renderManyManyDropDownListSelection();
  361. else if (strcasecmp($this->style, 'radiobutton') == 0) $this->renderManyManyRadioButtonListSelection();
  362. else $this->renderOnePaneSelection();
  363. }
  364. // when only one of the MANY_MANY related Objects should be choosable
  365. public function renderManyManyRadioButtonListSelection() {
  366. echo CHtml::RadiobuttonList(get_class($this->_model) . '[' . get_class($this->_relatedModel) . '][]', $this->_model{$this->relation}[0]->{$this->_model{$this->relation}[0]->tableSchema->primaryKey
  367. }
  368. , $this->getRelatedData() , $this->htmlOptions);
  369. }
  370. /*
  371. * Renders one dropDownList per selectable related Element.
  372. * The users can add additional entries with the + and remove entries
  373. * with the - Button .
  374. */
  375. public function renderManyManyDropDownListSelection() {
  376. $uniqueid = $this->_relatedModel->tableSchema->name;
  377. if ($this->parentObjects != 0) $relatedmodels = $this->parentObjects;
  378. else $relatedmodels = $this->_relatedModel->findAll();
  379. $addbutton = sprintf('i' . $this->num . ' = %d; maxi' . $this->num . ' = %d;', count($this->getAssignedObjects()) + 1, count($relatedmodels));
  380. Yii::app()->clientScript->registerScript('addbutton_' . $uniqueid . '_' . $this->num, $addbutton);
  381. $i = 0;
  382. foreach ($relatedmodels as $obj) {
  383. $i++;
  384. $isAssigned = $this->isAssigned($obj->{$this->relatedPk});
  385. echo CHtml::openTag('div', array(
  386. 'id' => sprintf('div_%s_%d', $uniqueid, $i) ,
  387. 'style' => $isAssigned ? '' : 'display:none;',
  388. 'class' => 'relation'
  389. ));
  390. echo CHtml::dropDownList(sprintf('%s[%d]', $this->getListBoxName() , $i) , $isAssigned ? $obj->id : 0, CHtml::listData(array_merge(array(
  391. '0' => $this->allowEmpty
  392. ) , $relatedmodels) , $this->relatedPk, $this->fields));
  393. echo CHtml::button('-', array(
  394. 'id' => sprintf('sub_%s_%d', $uniqueid, $i)
  395. ));
  396. echo CHtml::closeTag('div');
  397. $jsadd = '
  398. $(\'#add_' . $uniqueid . '\').click(function() {
  399. $(\'#div_' . $uniqueid . '_\' + i' . $this->num . ').show();
  400. if(i' . $this->num . ' <= maxi' . $this->num . ') i' . $this->num . '++;
  401. });
  402. ';
  403. $jssub = '
  404. $(\'#sub_' . $uniqueid . '_' . $i . '\').click(function() {
  405. $(\'#div_' . $uniqueid . '_' . $i . '\').hide();
  406. $("select[name=\'' . $this->getListBoxName() . '[' . $i . ']\']").val(\'\');
  407. if(i' . $this->num . ' >= 1) i--;
  408. });
  409. ';
  410. Yii::app()->clientScript->registerScript('addbutton_' . $uniqueid, $jsadd);
  411. Yii::app()->clientScript->registerScript('subbutton_' . $uniqueid, $jssub);
  412. }
  413. echo '&nbsp;';
  414. echo CHtml::button('+', array(
  415. 'id' => sprintf('add_%s', $uniqueid)
  416. ));
  417. }
  418. public function isAssigned($id) {
  419. return in_array($id, array_keys($this->getAssignedObjects()));
  420. }
  421. public static function retrieveValues($data) {
  422. $returnArray = array();
  423. $i = 0;
  424. foreach ($data as $value) {
  425. if ($value != 0) $returnArray[(int)$i] = (int)$value;
  426. $i++;
  427. }
  428. return $returnArray;
  429. }
  430. public function renderCheckBoxListSelection() {
  431. $keys = array_keys($this->getAssignedObjects());
  432. if (isset($this->preselect) && $this->preselect != false) $keys = $this->preselect;
  433. $id = $this->relation . '_options';
  434. echo CHtml::openTag('div', array(
  435. 'id' => $id,
  436. 'class' => 'relation'
  437. ));
  438. echo CHtml::CheckBoxList($this->getListBoxName() , $keys, $this->getRelatedData() , $this->htmlOptions);
  439. echo CHtml::closeTag('div');
  440. }
  441. public function renderOnePaneSelection() {
  442. $keys = array_keys($this->getAssignedObjects());
  443. echo CHtml::ListBox($this->getListBoxName() , $keys, $this->getRelatedData() , array(
  444. 'multiple' => 'multiple'
  445. ));
  446. }
  447. public function handleAjaxRequest($_POST) {
  448. print_r($_POST);
  449. }
  450. public function renderTwoPaneSelection() {
  451. echo CHtml::ListBox($this->getListBoxName() , array() , $this->getObjectValues($this->getAssignedObjects()) , array(
  452. 'multiple' => 'multiple'
  453. ));
  454. $ajax = array(
  455. 'type' => 'POST',
  456. 'data' => array(
  457. 'yeah'
  458. ) ,
  459. 'update' => '#' . $this->getListBoxName(true) ,
  460. );
  461. echo CHtml::ajaxSubmitButton('<<', array(
  462. 'assign'
  463. ) , $ajax);
  464. $ajax = array(
  465. 'type' => 'POST',
  466. 'update' => '#not_' . $this->getListBoxName(true)
  467. );
  468. echo CHtml::ajaxSubmitButton('>>', array(
  469. 'assign',
  470. 'revoke' => 1
  471. ) , $ajax); //,
  472. //$data['revoke']);
  473. echo CHtml::ListBox('not_' . $this->getListBoxName() , array() , $this->getObjectValues($this->getNotAssignedObjects()) , array(
  474. 'multiple' => 'multiple'
  475. ));
  476. }
  477. public function run() {
  478. if ($this->manyManyTable != '') $this->renderManyManySelection();
  479. else $this->renderBelongsToSelection();
  480. if ($this->showAddButton) $this->renderAddButton();
  481. }
  482. protected function renderAddButton() {
  483. $model = strtolower(get_class($this->_model));
  484. $relatedModel = strtolower(get_class($this->_relatedModel));
  485. $relations = $this->_model->relations();
  486. $controller = GHelper::resolveController($relations[$this->relation]);
  487. if ($this->addButtonUrl != '') $link = $this->addButtonUrl;
  488. else $link = $this->controller->createUrl($controller . '/create', array(
  489. 'relation' => $this->relation
  490. ));
  491. if ($this->addButtonRefreshUrl == '') $this->addButtonRefreshUrl = $this->controller->createUrl($model . '/getOptions', array(
  492. 'relation' => $this->relation,
  493. 'style' => $this->style,
  494. 'fields' => $this->fields
  495. ));
  496. $string = Yii::t('app', 'Add new {model}', array(
  497. '{model}' => $relatedModel
  498. ));
  499. $dialog = 'zii.widgets.jui.CJuiDialog';
  500. $this->beginWidget($dialog, array(
  501. 'id' => $this->relation . '_dialog',
  502. 'options' => array(
  503. 'autoOpen' => false,
  504. 'modal' => true,
  505. 'title' => $string,
  506. 'width' => 800,
  507. 'height' => 600
  508. )
  509. ));
  510. $this->endWidget($dialog);
  511. echo CHtml::AjaxButton(is_string($this->showAddButton) ? $this->showAddButton : $string, $link, array(
  512. 'success' => "function(html) {
  513. jQuery('#" . $this->relation . "_dialog').html(html);
  514. $('#" . $this->relation . "_dialog').dialog('open');
  515. }"
  516. ) , array(
  517. 'id' => $this->relation . '_create'
  518. ));
  519. // prepare the Submit button that is not loaded into the DOM yet
  520. Yii::app()->clientScript->registerScript($this->relation . '_submit', "jQuery('body').delegate('#submit_" . $this->relation . "','click',function(){
  521. jQuery.ajax({'url':'" . $link . "',
  522. 'cache':false,
  523. 'type':'POST',
  524. 'data':jQuery(this).parents('form').serialize(),
  525. 'success':function(html){
  526. jQuery('#" . $this->relation . "_dialog').html(html)}});
  527. return false;});");
  528. Yii::app()->clientScript->registerScript($this->relation . '_done', "jQuery('body').delegate('#" . $this->relation . "_done','click',function(){
  529. jQuery.ajax({'url':'" . $this->addButtonRefreshUrl . "',
  530. 'cache':false,
  531. 'success':function(html){
  532. jQuery('#" . $this->relation . "_options').html(html)}});
  533. $('#" . $this->relation . "_dialog').dialog('close');
  534. return false;});");
  535. }
  536. }