1: <?php
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: class MassMoveFileSysObjToFolder extends BaseDocsMassAction {
38:
39: public $hasButton = true;
40:
41: 42: 43: 44:
45: public function renderDialog ($gridId, $modelName) {
46: echo "
47: <div class='mass-action-dialog'
48: id='".$this->getDialogId ($gridId)."' style='display: none;'>
49: <span>".
50: Yii::t('app', 'Move to')."
51: </span>
52: <span style='display: none;'>".
53: Yii::t('app', 'Move to')."
54: </span>
55: <span class='target-folder'>".CHtml::encode (Yii::t('docs', 'Docs'))."</span>
56: <input name='targetFolderId' type='hidden' val='' />
57: <br/>
58: <div class='folder-selector'>
59: </div>
60: </div>";
61: }
62:
63: 64: 65:
66: public function renderButton () {
67: if (!$this->hasButton) return;
68:
69: echo "
70: <a href='#' title='".CHtml::encode ($this->getLabel ())."'
71: data-mass-action='".get_class ($this)."'
72: data-allow-multiple='".($this->allowMultiple ? 'true' : 'false')."'
73: class='fa fa-folder fa-lg mass-action-button x2-button mass-action-button-".
74: get_class ($this)."'>
75: </a>";
76: }
77:
78:
79: 80: 81:
82: public function getLabel () {
83: if (!isset ($this->_label)) {
84: $this->_label = Yii::t('app', 'Move to');
85: }
86: return $this->_label;
87: }
88:
89: public function getPackages () {
90: return array_merge (parent::getPackages (), array (
91: 'X2MassMoveFileSysObjToFolder' => array(
92: 'baseUrl' => Yii::app()->request->baseUrl,
93: 'js' => array(
94: 'js/X2GridView/MassMoveFileSysObjToFolder.js',
95: ),
96: 'depends' => array ('X2MassAction', 'BaseDocsMassActionJS'),
97: ),
98: ));
99: }
100:
101: 102: 103:
104: public function execute (array $gvSelection) {
105: if (Yii::app()->controller->modelClass !== 'Docs' ||
106: !isset ($_POST['selectedObjs']) || !is_array ($_POST['selectedObjs']) ||
107: count ($_POST['selectedObjs']) !== count ($gvSelection) ||
108: !isset ($_POST['selectedObjTypes']) || !is_array ($_POST['selectedObjTypes']) ||
109: count ($_POST['selectedObjTypes']) !== count ($gvSelection)) {
110:
111: throw new CHttpException (400, Yii::t('app', 'Bad Request'));
112: }
113: $selectedObjs = $_POST['selectedObjs'];
114: $selectedObjTypes = $_POST['selectedObjTypes'];
115:
116: if (!isset ($_POST['targetFolder']) || $_POST['targetFolder'] === '') {
117: $destination = null;
118: } else {
119: $targetFolder = $_POST['targetFolder'];
120: $destination = DocFolders::model ()->findByPk ($targetFolder);
121: if (!$destination)
122: throw new CHttpException (400, Yii::t('app', 'Folder not found'));
123: if (!Yii::app()->controller->checkPermissions ($destination, 'edit')) {
124: self::$errorFlashes[] =
125: Yii::t('app', 'You do not have permission to edit this folder.');
126: return 0;
127: }
128: }
129:
130: $objCount = count ($gvSelection);
131: $successes = 0;
132: for ($i = 0; $i < $objCount; $i++) {
133: $id = $selectedObjs[$i];
134: if (((int) $id) === DocFolders::TEMPLATES_FOLDER_ID) {
135: continue;
136: }
137: $type = $selectedObjTypes[$i];
138: if ($type === 'doc') {
139: $obj = Docs::model ()->findByPk ($id);
140: } elseif ($type === 'folder') {
141: $obj = DocFolders::model ()->findByPk ($id);
142: } else {
143: self::$errorFlashes[] = Yii::t('app', 'Invalid object type.');
144: continue;
145: }
146: if (!$obj) {
147: self::$errorFlashes[] =
148: Yii::t('app', 'Selected {type} does not exist', array (
149: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
150: ));
151: continue;
152: }
153: if (!Yii::app()->controller->checkPermissions ($obj, 'edit')) {
154: self::$errorFlashes[] =
155: Yii::t('app', 'You do not have permission to edit this {type}.', array (
156: '{type}' => $type === 'doc' ? ucfirst ($type) : $type,
157: ));
158: continue;
159: }
160: if ($obj instanceof DocFolders && $destination && $obj->id === $destination->id) {
161: self::$errorFlashes[] =
162: Yii::t('app', 'Cannot move "{name}" to a folder inside itself.', array (
163: '{name}' => $obj->name
164: ));
165: continue;
166: }
167: if ($obj->moveTo ($destination)) {
168: $successes++;
169: } else {
170: self::$errorFlashes[] =
171: Yii::t('app', 'Failed to move "{name}"', array (
172: '{name}' => $obj->name
173: ));
174: }
175: }
176: if ($successes) {
177: self::$successFlashes[] = Yii::t(
178: 'app',
179: '{n} object moved to "{destination}"|{n} objects moved to "{destination}"', array(
180: $successes,
181: '{destination}' => $destination ?
182: $destination->name :
183: Yii::t('docs', 'Docs')
184: )
185: );
186: }
187: return $successes;
188: }
189:
190: }
191:
192: ?>
193: