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 contact@x2engine.com.
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 NewListFromSelection extends MassAction {
38:
39: protected $_label;
40:
41: private $listId;
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>".
52: Yii::t('app', 'What should the list be named?')."
53: </span>
54: <br/>
55: <input class='left new-list-name'></input>
56: </div>";
57: }
58:
59: /**
60: * @return string label to display in the dropdown list
61: */
62: public function getLabel () {
63: if (!isset ($this->_label)) {
64: $this->_label = Yii::t('app', 'New list from selected');
65: }
66: return $this->_label;
67: }
68:
69: public function getPackages () {
70: return array_merge (parent::getPackages (), array (
71: 'X2NewListFromSelection' => array(
72: 'baseUrl' => Yii::app()->request->baseUrl,
73: 'js' => array(
74: 'js/X2GridView/NewListFromSelection.js',
75: ),
76: 'depends' => array ('X2MassAction'),
77: ),
78: ));
79: }
80:
81: public function execute (array $gvSelection) {
82: if (Yii::app()->controller->modelClass !== 'Contacts' ||
83: !isset ($_POST['listName']) || $_POST['listName'] === '') {
84:
85: throw new CHttpException (400, Yii::t('app', 'Bad Request'));
86: }
87: if (!Yii::app()->params->isAdmin &&
88: !Yii::app()->user->checkAccess ('ContactsCreateListFromSelection')) {
89:
90: return -1;
91: }
92:
93: $listName = $_POST['listName'];
94: foreach($gvSelection as &$contactId){
95: if(!ctype_digit((string) $contactId))
96: throw new CHttpException(400, Yii::t('app', 'Invalid selection.'));
97: }
98:
99: $list = new X2List;
100: $list->name = $_POST['listName'];
101: $list->modelName = 'Contacts';
102: $list->type = 'static';
103: $list->assignedTo = Yii::app()->user->getName();
104: $list->visibility = 1;
105: $list->createDate = time();
106: $list->lastUpdated = time();
107:
108: $itemModel = X2Model::model('Contacts');
109: $success = true;
110: if($list->save()){ // if the list is valid save it so we can get the ID
111: $count = 0;
112: foreach($gvSelection as &$itemId){
113:
114: if($itemModel->exists('id="'.$itemId.'"')){ // check if contact exists
115: $item = new X2ListItem;
116: $item->contactId = $itemId;
117: $item->listId = $list->id;
118: if($item->save()) // add all the things!
119: $count++;
120: }
121: }
122: $list->count = $count;
123: $this->listId = $list->id;
124: if($list->save()) {
125: self::$successFlashes[] = Yii::t(
126: 'app', '{count} record'.($count === 1 ? '' : 's').
127: ' added to new list "{list}"', array (
128: '{count}' => $count,
129: '{list}' => $list->name,
130: )
131: );
132: } else {
133: self::$errorFlashes[] = Yii::t(
134: 'app', 'List created but records could not be added to it');
135: }
136: } else {
137: $success = false;
138: self::$errorFlashes[] = Yii::t(
139: 'app', 'List could not be created');
140: }
141: return $success ? $count : -1;
142:
143: }
144:
145: /**
146: * Add list id to response data so that subsequent client requests can be for add to list
147: * mass action
148: */
149: protected function generateSuperMassActionResponse ($successes, $selectedRecords, $uid) {
150: $response = parent::generateSuperMassActionResponse ($successes, $selectedRecords, $uid);
151: $response['listId'] = $this->listId;
152: return $response;
153: }
154:
155: }
156: