Esempio n. 1
0
 public function _action($action)
 {
     //$app = application::getInstance();
     $modelId = intval($action);
     $class = get_class($this);
     $cl = strlen($class);
     $lc = 'ListController';
     $l = strlen($lc);
     if ($this->_model === null) {
         if (substr($class, $cl - $l, $l) == $lc) {
             $this->_model = substr($class, 0, $cl - $l);
         }
         if ($this->_model === null || !class_exists($this->_model)) {
             throw new Exception('setup $_model in ' . get_class($this));
         }
     }
     $model = $this->_model;
     if ($this->_subController === null) {
         $this->_subController = $model . 'Controller';
         if (!class_exists($this->_subController)) {
             throw new Exception('setup $_subController in ' . get_class($this));
         }
     }
     $subController = $this->_subController;
     $items = modelCollection::getInstance($model);
     $id = $this->_id;
     $item = $items->select()->where("{$items->{$id}} = '{$modelId}'")->fetch();
     if ($item) {
         $this->onValidItem($modelId, $item);
         $this->runController($subController, array($model => $item));
     } else {
         $this->notFound();
     }
 }
Esempio n. 2
0
 protected function applyFilters()
 {
     foreach ($this->_joinedTables as $tableUid => $table) {
         $filters = $table->getFilters();
         if (count($filters)) {
             if ($this->_storageSource->getUniqueId() == $tableUid) {
                 foreach ($filters as $filter) {
                     $this->where($filter);
                 }
             } else {
                 foreach ($filters as $filter) {
                     if ($filter instanceof modelExpression) {
                         $left = $filter->getLeft();
                         $right = $filter->getRight();
                         if ($left instanceof modelField) {
                             $this->_joinWhere[$left->getCollectionId()][] = $filter;
                         } elseif ($right instanceof modelField) {
                             $this->_joinWhere[$right->getCollectionId()][] = $filter;
                         } else {
                             $this->where($filter);
                         }
                     }
                 }
             }
         }
     }
 }
 public function index($page = 1)
 {
     $this->_collection->setItemsByPage($this->_itemsByPage);
     $itemsCount = count($this->_collection->select());
     if ($itemsCount) {
         $pagesCount = ceil($itemsCount / $this->_itemsByPage);
         $this->viewPages($pagesCount, $page);
         echo '<table class="scaffold-list">';
         $first = true;
         $odd = true;
         foreach ($this->_collection->select()->page($page) as $item) {
             $odd = !$odd;
             $properties = $item->getPropertyNames();
             if ($first) {
                 echo '<tr>';
                 foreach ($properties as $propertyName) {
                     echo '<th>';
                     echo $propertyName;
                     echo '</th>';
                 }
                 echo '</tr>';
                 $first = false;
             }
             echo '<tr' . ($odd ? ' class="odd"' : '') . '>';
             foreach ($properties as $propertyName) {
                 echo '<td>';
                 $property = $item->{$propertyName};
                 echo $property->html();
                 echo '</td>';
             }
             echo '</tr>';
         }
         echo '<tr>';
         echo '<th colspan="' . count($properties) . '">';
         echo 'Найдено рядов: ' . $itemsCount;
         echo '</th>';
         echo '</tr>';
         echo '</table>';
         $this->viewPages($pagesCount, $page);
     }
 }
Esempio n. 4
0
 /**
  * @return boolean whether authentication succeeds
  */
 public function authenticate()
 {
     $users = user::getCollection();
     //modelCollection::getInstance('registeredUser');
     $emails = modelCollection::getInstance('userEmail');
     $result = $users->select($emails, $emails->email->is($this->_email))->fetch();
     if (!$result) {
         throw new authException('Email not registered', authException::ERROR_EMAIL_INVALID);
     }
     list($user, $userEmail) = $result;
     $this->_isRegistered = true;
     if (!$user->password->equals($this->_password)) {
         throw new authException('Invalid password ("' . $user->password->getInternalValue() . '" != "' . $user->password->getHash($this->_password) . '")', authException::ERROR_PASSWORD_INVALID);
     }
     $this->_user = $user;
     $this->_identityModels['email'][$this->_email] = $userEmail;
 }
Esempio n. 5
0
 /**
  * @return boolean whether authentication succeeds
  */
 public function authenticate()
 {
     $users = user::getCollection();
     //modelCollection::getInstance('registeredUser');
     $logins = modelCollection::getInstance('userLogin');
     $result = $users->select($logins, $logins->login->is($this->_login))->fetch();
     if (!$result) {
         throw new authException('Login not registered', authException::ERROR_USERNAME_INVALID);
     }
     list($user, $userLogin) = $result;
     $this->_isRegistered = true;
     if (!$user->password->equals($this->_password)) {
         throw new authException('Invalid password', authException::ERROR_PASSWORD_INVALID);
     }
     $this->_user = $user;
     $this->_identityModels['login'][$this->_login] = $userLogin;
 }
Esempio n. 6
0
 /**
  * @return boolean whether authentication succeeds
  */
 public function authenticate()
 {
     $openid = $this->_openidIdentity;
     if (!$this->authenticateOpenId($openid)) {
         throw new authException('Invalid OpenID');
     }
     $openid = $this->_openidIdentity;
     $users = user::getCollection();
     //modelCollection::getInstance('registeredUser');
     $openids = modelCollection::getInstance('userOpenid');
     $result = $users->select($openids, $openids->openid->is($this->_openidIdentity))->fetch();
     if (!$result) {
         // throw new authException('OpenID "'.$this->_openidIdentity.'" not registered', authException::ERROR_NOT_REGISTERED);
         // autocreate:
         $user = new registeredUser();
         //$user->save();
         $userOpenid = new userOpenid();
         $userOpenid->userId = $user->id;
         $userOpenid->openid = $this->_openidIdentity;
         //$userOpenid->save();
     } else {
         list($user, $userOpenid) = $result;
         $this->_isRegistered = true;
     }
     /* if (!$user->password->equals($this->_password)){
     	  throw new authException('Invalid password', authException::ERROR_PASSWORD_INVALID);
     	  } */
     $this->_user = $user;
     $this->_identityModels['openid'][$this->_openidIdentity] = $userOpenid;
     if (isset($this->_attributes['contact/email'])) {
         $email = $this->_attributes['contact/email'];
         if (!isset($this->_identityModels['email'][$email])) {
             $userEmail = new userEmail();
             $userEmail->email = $email;
             $userEmail->userId = $user->id;
             $this->_identityModels['email'][$email] = $userEmail;
         }
     }
     //var_dump($this->_attributes);
     return true;
 }
Esempio n. 7
0
 /**
  * Create collections if not exists
  */
 protected function _createCollection()
 {
     //if ($this->_autoRepair){
     /*throw new Exception(
     	 'Trying to _createCollection()'
     		);*/
     $created = false;
     //echo 'Create collection()'."\r\n";
     $models = $this->getStorage()->getModels();
     foreach ($models as $model) {
         $collection = modelCollection::getInstance($model);
         /** @var modelCollection $collection */
         if (!$collection->exists()) {
             //echo $model.' collection  not exists'."\r\n";
             //$this->disableAutoRepair();
             //$this->disableServiceMode();
             //echo $collection->getCreateSql();
             /*throw new Exception(
             				'Trying to create table '.$collection->getTableName().' SQL:'.$collection->getCreateSql()
             		);*/
             if ($collection->internalQuery($collection->getCreateSql())) {
                 $created = true;
             }
             //$this->enableServiceMode();
             //$this->enableAutoRepair();
         } else {
             //echo $model.' collection  exists'."\r\n";
         }
     }
     /*throw new Exception(
     	 'Trying to _createCollection() - end'
     	 );*/
     return $created;
     //}
     //return false;
 }
Esempio n. 8
0
 public static function getCollection()
 {
     return modelCollection::getInstance(self::$_model);
 }
Esempio n. 9
0
 public static function getIndirectTablesJoins($sourceTable, $targetTable, $joinTypes, $joinWhere)
 {
     $keys =& storageRegistry::getInstance()->foreignKeys;
     $sourceClass = self::getTableModel($sourceTable);
     $targetClass = self::getTableModel($targetTable);
     $joins = array();
     $joinedTables = array();
     //echo 'Connecting from '.$sourceClass.' to '.$targetClass.'<br />';
     foreach ($keys[$sourceClass] as $foreignClass => $options) {
         if ($foreignClass == $targetClass) {
             if (!is_array($options)) {
                 $viaClass = $options;
                 //echo 'Connecting via '.$viaClass.'<br />';
                 $viaTable = modelCollection::getInstance($viaClass);
                 $subJoins = self::getIndirectTablesJoins($sourceTable, $viaTable, $joinTypes, $joinWhere);
                 if ($subJoins !== false) {
                     foreach ($subJoins as $uid => $joinString) {
                         $joins[$uid] = $joinString;
                     }
                 }
                 $subJoins = self::getIndirectTablesJoins($viaTable, $targetTable, $joinTypes, $joinWhere);
                 if ($subJoins !== false) {
                     foreach ($subJoins as $uid => $joinString) {
                         $joins[$uid] = $joinString;
                     }
                 }
                 return $joins;
             } else {
                 // FIXED JOIN TYPE & ON() SELECTION
                 $joinType = isset($joinTypes[$targetTable->getUniqueId()]) ? $joinTypes[$targetTable->getUniqueId()] : 'INNER';
                 $joinOn = $sourceTable->getJoinOn($targetTable);
                 if ($joinOn === null) {
                     $joinOn = $targetTable->getJoinOn($sourceTable);
                     if ($joinOn === null) {
                         $joinOn = '';
                     }
                 }
                 list($sourcePropertyName, $targetPropertyName) = $options;
                 $joinString = " " . $joinType . " JOIN {$targetTable->getTableName()} AS {$targetTable} ON ({$sourceTable->{$sourcePropertyName}} = {$targetTable->{$targetPropertyName}}";
                 if (isset($joinWhere[$targetTable->getUniqueId()])) {
                     // apply filters
                     $joinString .= " AND " . implode(" AND ", $joinWhere[$targetTable->getUniqueId()]);
                 }
                 if (strlen($joinOn)) {
                     $joinString .= " AND " . $joinOn;
                 }
                 $joinString .= ")";
                 //$joins[$sourceTable->getUniqueId()] = true;
                 $joins[$targetTable->getUniqueId()] = $joinString;
                 //echo 'Connecting via DIRECT<br />';
                 if ($joinString !== false) {
                     return $joins;
                 }
                 //array($joinedTables, $joinString);
             }
         }
     }
     //var_dump($keys[$sourceClass]);
     //throw new Exception('Connecting via FALSE');
     return false;
 }
Esempio n. 10
0
 public static function getCollection($modelName)
 {
     return modelCollection::getInstance($modelName);
 }
Esempio n. 11
0
 public function getCommentsCollection()
 {
     return modelCollection::getInstance($this->_getCommentClass());
 }
Esempio n. 12
0
 public function getCollection()
 {
     return modelCollection::getInstanceById($this->getCollectionId());
 }
Esempio n. 13
0
 /**
  * @return modelCollection
  */
 public static function &getCollection()
 {
     if (!function_exists('get_called_class')) {
         require_once dirname(__FILE__) . '/../common/compat/get_called_class.php';
         // PHP 5 >= 5.2.4
     }
     //echo get_called_class();
     //var_dump(debug_backtrace());
     return modelCollection::getInstance(get_called_class());
     // PHP 5 >= 5.3.0
 }