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:
38: Yii::import('application.components.JSONEmbeddedModelFieldsBehavior');
39:
40: 41: 42: 43:
44: class Admin extends X2ActiveRecord {
45:
46: protected $_oldAttributes = array();
47:
48: 49: 50: 51:
52: public static function model($className = __CLASS__){
53: return parent::model($className);
54: }
55:
56: public static function getDoNotEmailLinkDefaultText () {
57: return Yii::t('admin', 'I do not wish to receive these emails.');
58: }
59:
60: public static function getDoNotEmailDefaultPage () {
61: $message = Yii::t(
62: 'admin', 'You will no longer receive emails from this sender.');
63: return '<html><head><title>'.$message.
64: '</title></head><body>'.$message.'</body></html>';
65: }
66:
67: private $_googleIntegrationCredentials;
68: public function getGoogleIntegrationCredentials ($refresh=false) {
69: if (!isset ($this->_googleIntegrationCredentials) || $refresh) {
70: $credId = Yii::app()->settings->googleCredentialsId;
71: if ($credId && ($credentials = Credentials::model ()->findByPk ($credId))) {
72: $this->_googleIntegrationCredentials = array (
73:
74: 'clientId' => $credentials->auth->clientId,
75: 'clientSecret' => $credentials->auth->clientSecret,
76: );
77: }
78: }
79: return $this->_googleIntegrationCredentials;
80: }
81:
82: 83: 84:
85: public function tableName(){
86: return 'x2_admin';
87: }
88:
89: 90: 91:
92: public function afterFind() {
93: $this->_oldAttributes = $this->getAttributes();
94: parent::afterFind();
95: }
96:
97: public function behaviors(){
98: $behaviors = array(
99: 'JSONFieldsBehavior' => array (
100: 'class' => 'application.components.JSONFieldsBehavior',
101: 'transformAttributes' => array (
102: 'twitterRateLimits',
103: 'assetBaseUrls',
104: ),
105: ),
106: 'JSONFieldsDefaultValuesBehavior' => array(
107: 'class' => 'application.components.JSONFieldsDefaultValuesBehavior',
108: 'transformAttributes' => array(
109: 'actionPublisherTabs' => array(
110: 'PublisherCommentTab' => true,
111: 'PublisherActionTab' => true,
112: 'PublisherCallTab' => true,
113: 'PublisherTimeTab' => true,
114: 'PublisherEventTab' => false,
115: 'PublisherProductsTab' => false,
116: ),
117:
118: ),
119: 'maintainCurrentFieldsOrder' => true
120: ),
121: );
122:
123: return $behaviors;
124: }
125:
126: public function validateUniqueId ($attr) {
127: $value = $this->$attr;
128:
129: if (!isset ($this->_oldAttributes[$attr]) || $value !== $this->_oldAttributes[$attr]) {
130: Yii::app()->cache2->delete ($this->getLicenseKeyInfoCacheKey ());
131: }
132: }
133:
134: 135: 136:
137: public function validateUrlArray ($attr, $params) {
138: $values = $this->$attr;
139: $urlValidator = new CUrlValidator;
140: $allowEmpty = array_key_exists ('allowEmpty', $params) && $params['allowEmpty'];
141:
142: if (is_array ($values)) {
143: foreach ($values as $url) {
144: if ($urlValidator->validateValue ($url) || ($allowEmpty && empty($url)))
145: continue;
146: $this->addError (
147: $attr,
148: Yii::t('admin', 'The specified URL "{url}" is not in the correct format.', array(
149: '{url}' => CHtml::encode($url),
150: ))
151: );
152: }
153: }
154: }
155:
156: 157: 158:
159: public function rules(){
160:
161:
162: return array(
163: array('unique_id', 'validateUniqueId'),
164: array('emailType,emailFromName, emailFromAddr', 'requiredIfSysDefault', 'field' => 'emailBulkAccount'),
165: array('serviceCaseFromEmailName, serviceCaseFromEmailAddress', 'requiredIfSysDefault', 'field' => 'serviceCaseEmailAccount'),
166: array('serviceCaseEmailSubject, serviceCaseEmailMessage', 'required'),
167: array('timeout, webTrackerCooldown, chatPollTime, ignoreUpdates, rrId, onlineOnly, emailBatchSize, emailInterval, emailPort, installDate, updateDate, updateInterval, workflowBackdateWindow, workflowBackdateRange', 'numerical', 'integerOnly' => true),
168:
169: array('chatPollTime', 'numerical', 'max' => 100000, 'min' => 100),
170: array('currency', 'length', 'max' => 3),
171: array('emailUseAuth, emailUseSignature', 'length', 'max' => 10),
172: array('emailType, emailSecurity,gaTracking_internal,gaTracking_public', 'length', 'max' => 20),
173: array('webLeadEmail, leadDistribution, emailFromName, emailFromAddr, emailHost, emailUser, emailPass,externalBaseUrl,externalBaseUri', 'length', 'max' => 255),
174:
175: array('batchTimeout', 'numerical', 'integerOnly' => true),
176: array(
177: 'massActionsBatchSize',
178: 'numerical',
179: 'integerOnly' => true,
180: 'min' => 5,
181: 'max' => 100,
182: ),
183: array('emailBulkAccount,serviceCaseEmailAccount', 'safe'),
184:
185: array('emailBulkAccount', 'setDefaultEmailAccount', 'alias' => 'bulkEmail'),
186: array('serviceCaseEmailAccount', 'setDefaultEmailAccount', 'alias' => 'serviceCaseEmail'),
187: array('webLeadEmailAccount','setDefaultEmailAccount','alias' => 'systemResponseEmail'),
188: array('emailNotificationAccount','setDefaultEmailAccount','alias'=>'systemNotificationEmail'),
189: array('emailSignature', 'length', 'max' => 4096),
190: array('externalBaseUrl','url','allowEmpty'=>true),
191: array('assetBaseUrls','validateUrlArray','allowEmpty'=>false),
192: array('externalBaseUrl','match','pattern'=>':/$:','not'=>true,'allowEmpty'=>true,'message'=>Yii::t('admin','Value must not include a trailing slash.')),
193: array('enableWebTracker, quoteStrictLock, workflowBackdateReassignment,disableAutomaticRecordTagging,enableAssetDomains, enableUnsubscribeHeader', 'boolean'),
194: array('gaTracking_internal,gaTracking_public', 'match', 'pattern' => "/'/", 'not' => true, 'message' => Yii::t('admin', 'Invalid property ID')),
195: array ('appDescription', 'length', 'max' => 255),
196: array (
197: 'appName,x2FlowRespectsDoNotEmail,doNotEmailPage,doNotEmailLinkText',
198: 'safe'
199: ),
200:
201:
202:
203:
204:
205: );
206: }
207:
208: 209: 210:
211: public function attributeLabels(){
212: return array(
213: 'id' => Yii::t('admin', 'ID'),
214:
215:
216: 'timeout' => Yii::t('admin', 'Session Timeout'),
217: 'webLeadEmail' => Yii::t('admin', 'Web Lead Email'),
218: 'enableWebTracker' => Yii::t('admin', 'Enable Web Tracker'),
219: 'webTrackerCooldown' => Yii::t('admin', 'Web Tracker Cooldown'),
220: 'currency' => Yii::t('admin', 'Currency'),
221: 'chatPollTime' => Yii::t('admin', 'Notification Poll Time'),
222: 'ignoreUpdates' => Yii::t('admin', 'Ignore Updates'),
223: 'rrId' => Yii::t('admin', 'Round Robin ID'),
224: 'leadDistribution' => Yii::t('admin', 'Lead Distribution'),
225: 'onlineOnly' => Yii::t('admin', 'Online Only'),
226: 'disableAutomaticRecordTagging' =>
227: Yii::t('profile', 'Disable automatic record tagging?'),
228: 'emailBulkAccount' => Yii::t('admin', 'Send As (when sending bulk email)'),
229: 'emailFromName' => Yii::t('admin', 'Sender Name'),
230: 'emailFromAddr' => Yii::t('admin', 'Sender Email Address'),
231: 'emailBatchSize' => Yii::t('admin', 'Batch Size'),
232: 'emailInterval' => Yii::t('admin', 'Interval (Minutes)'),
233: 'emailUseSignature' => Yii::t('admin', 'Email Signatures'),
234: 'emailSignature' => Yii::t('admin', 'Default Signature'),
235: 'emailType' => Yii::t('admin', 'Method'),
236: 'emailHost' => Yii::t('admin', 'Hostname'),
237: 'emailPort' => Yii::t('admin', 'Port'),
238: 'emailUseAuth' => Yii::t('admin', 'Authentication'),
239: 'emailUser' => Yii::t('admin', 'Username'),
240: 'emailPass' => Yii::t('admin', 'Password'),
241: 'emailSecurity' => Yii::t('admin', 'Security'),
242: 'enableColorDropdownLegend' => Yii::t('admin', 'Colorize Dropdown Options?'),
243: 'installDate' => Yii::t('admin', 'Installed'),
244: 'updateDate' => Yii::t('admin', 'Last Update'),
245: 'updateInterval' => Yii::t('admin', 'Version Check Interval'),
246: 'googleIntegration' => Yii::t('admin', 'Activate Google Integration'),
247: 'inviteKey' => Yii::t('admin', 'Invite Key'),
248: 'workflowBackdateWindow' => Yii::t('admin', 'Process Backdate Window'),
249: 'workflowBackdateRange' => Yii::t('admin', 'Process Backdate Range'),
250: 'workflowBackdateReassignment' => Yii::t('admin', 'Process Backdate Reassignment'),
251: 'serviceCaseEmailAccount' => Yii::t('admin', 'Send As (to service requesters)'),
252: 'serviceCaseFromEmailName' => Yii::t('admin', 'Sender Name'),
253: 'serviceCaseFromEmailAddress' => Yii::t('admin', 'Sender Email Address'),
254: 'serviceCaseEmailSubject' => Yii::t('admin', 'Subject'),
255: 'serviceCaseEmailMessage' => Yii::t('admin', 'Email Message'),
256: 'gaTracking_public' => Yii::t('admin', 'Google Analytics Property ID (public)'),
257: 'gaTracking_internal' => Yii::t('admin', 'Google Analytics Property ID (internal)'),
258: 'serviceDistribution' => Yii::t('admin', 'Service Distribution'),
259: 'serviceOnlineOnly' => Yii::t('admin', 'Service Online Only'),
260: 'eventDeletionTime' => Yii::t('admin', 'Event Deletion Time'),
261: 'eventDeletionTypes' => Yii::t('admin', 'Event Deletion Types'),
262: 'properCaseNames' => Yii::t('admin', 'Proper Case Names'),
263: 'contactNameFormat' => Yii::t('admin', 'Contact Name Format'),
264: 'webLeadEmailAccount' => Yii::t('admin','Send As (to web leads)'),
265: 'emailNotificationAccount' => Yii::t('admin','Send As (when notifying users)'),
266: 'batchTimeout' => Yii::t('admin','Time limit on batch actions'),
267: 'massActionsBatchSize' => Yii::t('admin','Batch size for grid view mass actions'),
268: 'externalBaseUrl' => Yii::t('admin','External / Public Base URL'),
269: 'externalBaseUri' => Yii::t('admin','External / Public Base URI'),
270: 'appName' => Yii::t('admin','Application Name'),
271: 'x2FlowRespectsDoNotEmail' => Yii::t(
272: 'app','Respect contacts\' "Do not email" settings?'),
273: 'doNotEmailLinkText' => Yii::t('admin','"Do not email" Link Text'),
274: 'doNotEmailLinkPage' => Yii::t('admin','"Do not email" Page'),
275: 'doNotEmailPage' => Yii::t('admin','Do Not Email Page'),
276: 'enableAssetDomains' => Yii::t('admin','Enable Asset Domains'),
277:
278:
279: );
280: }
281:
282: public function requiredIfSysDefault($attribute, $params){
283: if(empty($this->$attribute) && $this->{$params['field']} == Credentials::LEGACY_ID)
284: $this->addError($attribute, Yii::t('yii', '{attribute} cannot be blank.', array('{attribute}' => $this->getAttributeLabel($attribute))));
285: }
286:
287: public function setDefaultEmailAccount($attribute, $params){
288: if($this->$attribute != Credentials::LEGACY_ID){
289: $cred = Credentials::model()->findByPk($this->$attribute);
290: if($cred)
291: $cred->makeDefault(Credentials::$sysUseId[$params['alias']], 'email', false);
292: } else{
293: Yii::app()->db->createCommand()->delete('x2_credentials_default', 'userId=:uid AND serviceType=:st', array(':uid' => Credentials::$sysUseId[$params['alias']], ':st' => 'email'));
294: }
295: }
296:
297: 298: 299: 300: 301: 302:
303: public function countEmail($nEmail = 1) {
304: $now = time();
305: if(empty($this->emailStartTime))
306: $this->emailStartTime = $now;
307: if($now-$this->emailStartTime > $this->emailInterval) {
308:
309: $this->emailStartTime = $now;
310: $this->emailCount = 0;
311: }
312: $this->emailCount += $nEmail;
313: $this->update(array('emailCount','emailStartTime'));
314: return $this->emailCount;
315: }
316:
317: 318: 319: 320: 321: 322:
323: public function emailCountWillExceedLimit($nEmail=1) {
324: $now = time();
325: if($now-$this->emailStartTime > $this->emailInterval) {
326: $this->emailStartTime = $now;
327: $this->emailCount = 0;
328: }
329: return $this->emailCount + $nEmail > $this->emailBatchSize;
330: }
331:
332: 333: 334: 335:
336: public function setActionPublisherTabs ($value) {
337: $this->actionPublisherTabs = $value;
338: $this->save ();
339: }
340:
341:
342:
343: public function getDoNotEmailLinkText(){
344: if(!empty($this->doNotEmailLinkText)){
345: return $this->doNotEmailLinkText;
346: }
347: return self::getDoNotEmailLinkDefaultText ();
348: }
349:
350:
351:
352: private function getLicenseKeyInfoCacheKey () {
353: return 'Admin::getLicenseKeyInfoCacheKey';
354: }
355: }
356:
357: