1: <?php
2:
3: /*****************************************************************************************
4: * X2Engine Open Source Edition is a customer relationship management program developed by
5: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
6: *
7: * This program is free software; you can redistribute it and/or modify it under
8: * the terms of the GNU Affero General Public License version 3 as published by the
9: * Free Software Foundation with the addition of the following permission added
10: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
12: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13: *
14: * This program is distributed in the hope that it will be useful, but WITHOUT
15: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU Affero General Public License along with
20: * this program; if not, see http://www.gnu.org/licenses or write to the Free
21: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22: * 02110-1301 USA.
23: *
24: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
25: * California 95067, USA. or at email address contact@x2engine.com.
26: *
27: * The interactive user interfaces in modified source and object code versions
28: * of this program must display Appropriate Legal Notices, as required under
29: * Section 5 of the GNU Affero General Public License version 3.
30: *
31: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32: * these Appropriate Legal Notices must retain the display of the "Powered by
33: * X2Engine" logo. If the display of the logo is not reasonably feasible for
34: * technical reasons, the Appropriate Legal Notices must display the words
35: * "Powered by X2Engine".
36: *****************************************************************************************/
37:
38: // Yii::import('application.models.X2Model');
39:
40: /**
41: * This is the model class for table "x2_docs".
42: *
43: * @package application.modules.docs.models
44: */
45: class Docs extends X2Model {
46:
47: public $supportsWorkflow = false;
48:
49: /**
50: * Returns the static model of the specified AR class.
51: * @return Docs the static model class
52: */
53: public static function model($className = __CLASS__) {
54: return parent::model($className);
55: }
56:
57: /**
58: * @return string the associated database table name
59: */
60: public function tableName() {
61: return 'x2_docs';
62: }
63:
64: public function behaviors() {
65: return array_merge(parent::behaviors(), array(
66: 'X2LinkableBehavior' => array(
67: 'class' => 'X2LinkableBehavior',
68: 'module' => 'docs',
69: ),
70: 'ERememberFiltersBehavior' => array(
71: 'class' => 'application.components.ERememberFiltersBehavior',
72: 'defaults' => array(),
73: 'defaultStickOnClear' => false
74: ),
75: 'FileSystemObjectBehavior' => array(
76: 'class' => 'application.modules.docs.components.FileSystemObjectBehavior',
77: 'folderRefName' => 'folderId',
78: ),
79: ));
80: }
81:
82: /**
83: * @return array relational rules.
84: */
85: public function relations() {
86: // NOTE: you may need to adjust the relation name and the related
87: // class name for the relations automatically generated below.
88: return array(
89: 'parent' => array(self::BELONGS_TO, 'DocFolders', 'folderId'),
90: );
91: }
92:
93: public function rules() {
94:
95: return array_merge(
96: array(
97: array('name','menuCheck','on'=>'menu'),
98: array('subject', 'length', 'max' => 255),
99: ),
100: parent::rules()
101: );
102: }
103:
104: public function menuCheck($attr,$params=array()) {
105: $this->$attr;
106: $this->scenario = 'menu';
107:
108: if(sizeof(Modules::model()->findAllByAttributes(array('name'=>$this->name))) > 0)
109: {
110: $this->addError('name', 'That name is not available.');
111: }
112: }
113:
114: public function parseType() {
115: if (!isset($this->type))
116: $this->type = '';
117: switch ($this->type) {
118: case 'email':
119: case 'quote':
120: return Yii::t('docs', 'Template');
121: default:
122: return Yii::t('docs', 'Document');
123: }
124: }
125:
126: /**
127: * Retrieves a list of models based on the current search/filter conditions.
128: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
129: */
130: public function search($pageSize=null) {
131: $criteria = new CDbCriteria;
132: return $this->searchBase ($criteria, $pageSize, false);
133: }
134:
135: /**
136: * Replace tokens with model attribute values.
137: *
138: * @param type $str Input text
139: * @param X2Model $model Model to use for replacement
140: * @param array $vars List of extra variables to replace
141: * @param bool $encode Encode replacement values if true; use renderAttribute otherwise.
142: * @return string
143: */
144: public static function replaceVariables(
145: $str,$model,$vars = array(),$encode = false,$renderFlag=true) {
146:
147: if($encode) {
148: foreach(array_keys($vars) as $key)
149: $vars[$key] = CHtml::encode($vars[$key]);
150: }
151: $str = strtr($str,$vars); // replace any manually set variables
152: if ($model instanceof X2Model) {
153: if (get_class($model) === 'Quote') {
154: $quoteTitle = Modules::displayName(false, "Quotes");
155: $quoteParams = array(
156: '{lineItems}' => $model->productTable(true),
157: '{dateNow}' => date("F d, Y", time()),
158: '{quoteOrInvoice}' => Yii::t('quotes',
159: $model->type == 'invoice' ? 'Invoice' : $quoteTitle),
160: );
161: $str = strtr($str, $quoteParams);
162: }
163: $str = Formatter::replaceVariables($str, $model, '', $renderFlag,
164: false);
165: }
166: return $str;
167: }
168:
169: /**
170: * Returns a list of email available email templates.
171: *
172: * Email and quote are the only two types of supported templates;
173: * no design has yet been done to completely generalize templating to
174: * accomodate generic models. Part of the challenge will lie in how,
175: * for multiple associated contacts (i.e. an account) any reference
176: * to a contact is ambiguous unless it is distinguished (i.e.
177: * primary contact, secondary contact, etc.)
178: *
179: * Current solution to this problem: Templates only contain insertable attributes for one type
180: *
181: * @param type $type
182: * @param string associationType Type associated with template (used for attribute replacement).
183: * If the empty string is passed, templates of all association types will be retrieved.
184: * @return type
185: */
186: public static function getEmailTemplates($type = 'email', $associationType=''){
187: $templateLinks = array();
188: if(in_array($type, array('email', 'quote'))){
189: // $criteria = new CDbCriteria(array('order'=>'lastUpdated DESC'));
190: $condition = 'TRUE';
191: $params = array ();
192: if(!Yii::app()->params->isAdmin){
193: $params[':username'] = Yii::app()->user->getName();
194: $condition = 'visibility="1" OR createdBy="Anyone" OR createdBy=:username ';
195:
196: /* x2temp */
197: $uid = Yii::app()->getSuID();
198: if(empty($uid)){
199: if(Yii::app()->params->noSession)
200: $uid = 1;
201: else
202: $uid = Yii::app()->user->id;
203: }
204: $groupLinks = Yii::app()->db->createCommand()
205: ->select('groupId')
206: ->from('x2_group_to_user')
207: ->where('userId='.$uid)
208: ->queryColumn();
209:
210: if(!empty($groupLinks))
211: $condition .= ' OR createdBy IN ('.implode(',', $groupLinks).')';
212:
213: $condition .=
214: 'OR (visibility=2 AND createdBy IN
215: (SELECT username FROM x2_group_to_user WHERE groupId IN
216: (SELECT groupId FROM x2_group_to_user WHERE userId='.$uid.')))';
217:
218: // $criteria->addCondition($condition);
219: }
220:
221: // for email templates, retrieve only templates with given association type.
222: // if associationType is empty, get templates of all association types
223: if ($type === 'email' && $associationType !== '') {
224: $condition .= ' AND (associationtype=:associationType)';
225: $params[':associationType'] = $associationType;
226: }
227: // $templates =
228: //X2Model::model('Docs')->findAllByAttributes(array('type'=>'email'),$criteria);
229: $params[':type'] = $type;
230:
231: $templateData = Yii::app()->db->createCommand()
232: ->select('id,name')
233: ->from('x2_docs')
234: ->where('type=:type AND ('.$condition.')', $params)
235: ->order('name ASC')
236: // ->andWhere($condition)
237: ->queryAll(false);
238: foreach($templateData as &$row)
239: $templateLinks[$row[0]] = $row[1];
240: }
241: return $templateLinks;
242: }
243:
244: public static function getEmailTemplates2() {
245: return self::model()->findAllByAttributes(array(
246: 'type' => 'email',
247: ));
248: }
249:
250: /**
251: * @return array names of models which support email templates
252: */
253: public static function modelsWhichSupportEmailTemplates () {
254: // get all x2model types not in blacklist
255: return array_diff_key (X2Model::getModelNames (), array_flip (array (
256: 'Actions', 'Quote', 'Product', 'Opportunity','Campaign',
257: )));
258: }
259:
260: }
261: