1<?php 2 3namespace GO\Email\Model; 4 5use GO; 6use GO\Base\Db\ActiveRecord; 7use go\core\util\StringUtil; 8 9/** 10 * Class Label 11 * 12 * @property int id 13 * @property string name 14 * @property string flag 15 * @property string color 16 * @property int account_id 17 * @property boolean default 18 */ 19class Label extends ActiveRecord 20{ 21 22 /** 23 * Returns a static model of itself 24 * 25 * @param String $className 26 * 27 * @return Label 28 */ 29 public static function model($className = __CLASS__) 30 { 31 return parent::model($className); 32 } 33 34 /** 35 * Returns the table name 36 */ 37 public function tableName() 38 { 39 return 'em_labels'; 40 } 41 42 /** 43 * Get count of account labels 44 * 45 * @param int $account_id Account ID 46 * 47 * @return int 48 */ 49 public function getLabelsCount($account_id) 50 { 51 $account_id = (int)$account_id; 52 53 if (!$account_id) { 54 return 0; 55 } 56 57 $sql = "SELECT count(*) FROM `{$this->tableName()}` WHERE account_id = " . $account_id; 58 $stmt = $this->getDbConnection()->query($sql); 59 return (int)($stmt->fetchColumn(0)); 60 } 61 62 /** 63 * Delete account labels 64 * 65 * @param int $account_id Account ID 66 * 67 * @return bool 68 */ 69 public function deleteAccountLabels($account_id) 70 { 71 $account_id = (int)$account_id; 72 73 if (!$account_id) { 74 return 0; 75 } 76 77 $sql = "DELETE FROM `{$this->tableName()}` WHERE account_id = " . $account_id; 78 $stmt = $this->getDbConnection()->query($sql); 79 return $stmt->execute(); 80 } 81 82 /** 83 * Create default account labels 84 * 85 * @param int $account_id Account ID 86 * 87 * @return bool 88 * @throws GO\Base\Exception\AccessDenied 89 */ 90 public function createDefaultLabels($account_id) 91 { 92 $labelsCount = $this->getLabelsCount($account_id); 93 94 if ($labelsCount >= 5) { 95 return false; 96 } 97 98 if ($labelsCount > 0 && $labelsCount < 5) { 99 $this->deleteAccountLabels($account_id); 100 } 101 102 $colors = array( 103 1 => '7A7AFF', 104 2 => '59BD59', 105 3 => 'FFBD59', 106 4 => 'FF5959', 107 5 => 'BD7ABD' 108 ); 109 110 for ($i = 1; $i < 6; $i++) { 111 $label = new Label; 112 $label->account_id = $account_id; 113 $label->name = 'Label ' . $i; 114 $label->flag = '$label' . $i; 115 $label->color = $colors[$i]; 116 $label->default = true; 117 $label->save(); 118 } 119 120 return true; 121 } 122 123 protected function init() 124 { 125 //$this->columns['name']['unique'] = true; 126 parent::init(); 127 } 128 129 protected function beforeSave() 130 { 131 $maxLabels = isset(\GO::config()->email_max_labels) ? (int)\GO::config()->email_max_labels : 10; 132 if ($this->isNew && $this->getLabelsCount($this->account_id) >= $maxLabels) { 133 throw new \Exception(sprintf(GO::t("Label's limit reached. The maximum number of labels is %d", "email"), $maxLabels)); 134 } 135 136 if (!$this->default && $this->isNew) { 137 $flag = preg_replace('~[^\\pL0-9_]+~u', '-', $this->name); 138 $flag = trim($flag, '-'); 139 $flag = StringUtil::toAscii($flag); 140 $flag = strtolower($flag); 141 $this->flag = preg_replace('~[^-a-z0-9_]+~', '', $flag); 142 } 143 return true; 144 } 145 146 /** 147 * @param $account_id 148 * @return array 149 */ 150 public function getAccountLabels($account_id) 151 { 152 $labels = array(); 153 154 $stmt = Label::model()->findByAttribute('account_id', (int)$account_id); 155 while ($label = $stmt->fetch()) { 156 $labels[$label->flag] = $label; 157 } 158 159 return $labels; 160 } 161} 162