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 RecordAliases extends CActiveRecord {
38:
39: 40: 41:
42: public static $types = array (
43: 'email', 'phone', 'skype', 'googlePlus', 'linkedIn', 'twitter', 'facebook', 'other');
44:
45: public static function model ($className=__CLASS__) {
46: return parent::model ($className);
47: }
48:
49: public static function getActions () {
50: return array (
51: 'createRecordAlias' =>
52: 'application.components.recordAliases.RecordAliasesCreateAction',
53: 'deleteRecordAlias' =>
54: 'application.components.recordAliases.RecordAliasesDeleteAction',
55: );
56: }
57:
58: public static function getAliases (X2Model $model, $aliasType = null) {
59: $params = array (
60: ':type' => get_class ($model),
61: ':recordId' => $model->id,
62: );
63: if ($aliasType) {
64: $params[':aliasType'] = $aliasType;
65: }
66: $aliases = RecordAliases::model ()->findAll (array (
67: 'condition' => 'recordType=:type AND recordId=:recordId'.
68: ($aliasType ? ' AND aliasType=:aliasType' : ''),
69: 'order' => 'aliasType ASC, alias ASC',
70: 'params' => $params,
71: ));
72: return $aliases;
73: }
74:
75: public function tableName () {
76: return 'x2_record_aliases';
77: }
78:
79: 80: 81:
82: public function rules () {
83: return array(
84: array('recordId, aliasType, alias', 'required'),
85: array('aliasType', 'validateAliasType'),
86: array('recordId', 'validateRecordId'),
87: array('alias', 'validateRecordAlias'),
88: array('alias', 'validateAliasUniqueness', 'on' => 'insert'),
89: array('label', 'safe'),
90: );
91: }
92:
93: public function getModel () {
94: $recordType = $this->recordType;
95: return $recordType::model ()->findByPk ($this->recordId);
96: }
97:
98: public function validateAliasType ($attribute) {
99: $value = $this->$attribute;
100: if (!in_array ($value, self::$types)) {
101: throw new CHttpException (400, Yii::t('app', 'Invalid alias type'));
102: }
103: }
104:
105: public function validateRecordId () {
106: if (!$this->getModel ()) {
107: throw new CHttpException (400, Yii::t('app', 'Invalid record id or type')) ;
108: }
109: }
110:
111: public function renderAlias ($makeLinks=true) {
112: if ($makeLinks) {
113: switch ($this->aliasType) {
114: case 'email':
115: return X2Html::renderEmailLink ($this->alias);
116: case 'phone':
117: return X2Html::renderPhoneLink ($this->alias);
118: case 'googlePlus':
119: return CHtml::encode ($this->label ? $this->label : $this->alias);
120: default:
121: return CHtml::encode ($this->alias);
122: }
123: } else {
124: switch ($this->aliasType) {
125: case 'googlePlus':
126: return CHtml::encode ($this->label ? $this->label : $this->alias);
127: default:
128: return CHtml::encode ($this->alias);
129: }
130: }
131: }
132:
133: public function validateAliasUniqueness () {
134: if (self::findByAttributes (array_diff_key ($this->getAttributes (), array (
135: 'id' => true)))) {
136:
137: $this->addError (
138: 'alias',
139: Yii::t('app', 'This record already has a {aliasType} alias with the '.
140: 'name "{alias}"', array (
141: '{aliasType}' => $this->renderAliasType (false),
142: '{alias}' => $this->renderAlias (false),
143: )));
144: }
145: }
146:
147: public function validateRecordAlias ($attribute) {
148: $value = $this->$attribute;
149: switch ($this->aliasType) {
150: case 'email':
151: $emailValidator = CValidator::createValidator ('email', $this, 'alias');
152: $emailValidator->validate ($this, 'email');
153: break;
154: }
155: }
156:
157: public function attributeLabels () {
158: return array (
159: 'aliasType' => Yii::t('app', 'Alias Type'),
160: 'alias' => Yii::t('app', 'Alias'),
161: );
162: }
163:
164: public function getAllIcons () {
165: $icons = array ();
166: foreach (self::$types as $type) {
167: $icons[$type] = $this->getIcon (false, false, $type);
168: }
169: return $icons;
170: }
171:
172: public function getIcon ($includeTitle=false, $large=false, $aliasType=null) {
173: if ($aliasType === null) {
174: $aliasType = $this->aliasType;
175: }
176:
177: $class = '';
178: switch ($aliasType) {
179: case 'email':
180: $class = 'fa-at';
181: break;
182: case 'phone':
183: $class = 'fa-phone';
184: break;
185: case 'skype':
186: $class = 'fa-skype';
187: break;
188: case 'googlePlus':
189: $class = 'fa-google-plus';
190: break;
191: case 'linkedIn':
192: $class = 'fa-linkedin';
193: break;
194: case 'twitter':
195: $class = 'fa-twitter';
196: break;
197: case 'facebook':
198: $class = 'fa-facebook';
199: break;
200: }
201: if ($includeTitle) $aliasOptions = $this->getAliasTypeOptions ();
202: if ($large) $class .= ' fa-lg';
203: return
204: '<span '.($includeTitle ?
205: 'title="'.CHtml::encode ($aliasOptions[$aliasType]).'" ' : '')
206: .'class="fa '.$class.'"></span>';
207: }
208:
209: public function renderAliasType ($encode=true) {
210: $options = $this->getAliasTypeOptions ();
211: $html = isset ($options[$this->aliasType]) ? $options[$this->aliasType] : '';
212: if ($encode) $html = CHtml::encode ($html);
213: return $html;
214: }
215:
216: private $_aliasTypeOptions;
217: public function getAliasTypeOptions () {
218: if (!isset ($this->_aliasTypeOptions)) {
219: $this->_aliasTypeOptions = array (
220: 'email' => Yii::t('app', 'email'),
221: 'phone' => Yii::t('app', 'phone'),
222: 'skype' => 'Skype',
223: 'googlePlus' => 'Google+',
224: 'linkedIn' => 'LinkedIn',
225: 'twitter' => 'Twitter',
226: 'facebook' => 'Facebook',
227: 'other' => 'Other',
228: );
229: }
230: return $this->_aliasTypeOptions;
231: }
232:
233: }
234:
235: ?>
236: