1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: class MassRenameFileSysObj extends BaseDocsMassAction {
38:
39: public $hasButton = false;
40:
41: public $allowMultiple = false;
42:
43: /**
44: * Renders the mass action dialog, if applicable
45: * @param string $gridId id of grid view
46: */
47: public function renderDialog ($gridId, $modelName) {
48: echo "
49: <div class='mass-action-dialog'
50: id='".$this->getDialogId ($gridId)."' style='display: none;'>
51: <span class='new-name'>".CHtml::encode (Yii::t('docs', 'New name:')).
52: "</span>
53: <input name='newName' type='text' val='' />
54: </div>";
55: }
56:
57: /**
58: * @return string label to display in the dropdown list
59: */
60: public function getLabel () {
61: if (!isset ($this->_label)) {
62: $this->_label = Yii::t('app', 'Rename');
63: }
64: return $this->_label;
65: }
66:
67: public function getPackages () {
68: return array_merge (parent::getPackages (), array (
69: 'MassRenameFileSysObjJS' => array(
70: 'baseUrl' => Yii::app()->request->baseUrl,
71: 'js' => array(
72: 'js/X2GridView/MassRenameFileSysObj.js',
73: ),
74: 'depends' => array ('BaseDocsMassActionJS'),
75: ),
76: ));
77: }
78:
79: /**
80: * @param array $gvSelection array of ids of records to perform mass action on
81: */
82: public function execute (array $gvSelection) {
83: if (Yii::app()->controller->modelClass !== 'Docs' ||
84: count ($gvSelection) > 1 ||
85: !isset ($_POST['selectedObjs']) || !is_array ($_POST['selectedObjs']) ||
86: count ($_POST['selectedObjs']) !== count ($gvSelection) ||
87: !isset ($_POST['selectedObjTypes']) || !is_array ($_POST['selectedObjTypes']) ||
88: count ($_POST['selectedObjTypes']) !== count ($gvSelection) ||
89: !in_array ($_POST['selectedObjTypes'][0], array ('doc', 'folder')) ||
90: !isset ($_POST['newName'])) {
91:
92: throw new CHttpException (400, Yii::t('app', 'Bad Request'));
93: }
94:
95: $selectedObjId = array_pop ($_POST['selectedObjs']);
96: $type = array_pop ($_POST['selectedObjTypes']);
97: $newName = $_POST['newName'];
98:
99: if ($type === 'doc') {
100: $obj = Docs::model ()->findByPk ($selectedObjId);
101: } else { // $type === 'folder'
102: $obj = DocFolders::model ()->findByPk ($selectedObjId);
103: }
104:
105: if (!$obj) {
106: self::$errorFlashes[] =
107: Yii::t('app', 'Selected {type} does not exist', array (
108: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
109: ));
110: return 0;
111: }
112:
113: if (!Yii::app()->controller->checkPermissions ($obj, 'edit')) {
114: self::$errorFlashes[] =
115: Yii::t('app', 'You do not have permission to edit this {type}.', array (
116: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
117: ));
118: return 0;
119: }
120:
121: if ($type === 'doc' &&
122: !Yii::app()->params->isAdmin &&
123: !in_array ('name', Docs::model ()->getEditableAttributeNames ())) {
124:
125: self::$errorFlashes[] =
126: Yii::t('app', 'You do not have permission to rename Docs.');
127: return 0;
128: }
129:
130: $obj->name = $newName;
131: $successes = 0;
132: if ($obj->save (true, array ('name'))) {
133: self::$successFlashes[] = Yii::t(
134: 'app',
135: 'Renamed {type}', array(
136: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
137: )
138: );
139: $successes = 1;
140: } else {
141: self::$errorFlashes[] = Yii::t(
142: 'app',
143: 'Failed to renamed {type}', array(
144: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
145: )
146: );
147: }
148: return $successes;
149: }
150:
151: }
152:
153: ?>
154: