public function save(PropelPDO $con = null) { $query = DomainQuery::create()->filterByName($this->getName()); if ($this->getId()) { $query->add(DomainPeer::ID, $this->getId(), Criteria::NOT_EQUAL); } if (!$this->getAccountId()) { throw new Exception('A Domain must belong to an Account.'); } if ($query->findOneByAccountId($this->getAccountId())) { throw new Exception('A Domain must have a unique name.', self::ERROR_NOT_UNIQUE); } return parent::save($con); }
private function import() { session_write_close(); $SOURCE_DB = 'groupion_new'; $USERNAME = '******'; $PASSWORD = ''; set_time_limit(0); $status = new Status(); print '<br><br><br><br><br><div style="margin-left: 3em">'; print '<p>Starting database migration</p>'; flush(); try { $con = Propel::getConnection(); $status->start('Connecting to database'); $dbh = new PDO('mysql:host=localhost;dbname=' . $SOURCE_DB, $USERNAME, $PASSWORD); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->exec('SET NAMES utf8'); $status->done(); $debugUser = UserQuery::create()->findOneByFQN('cms/test', $con); if ($debugUser === null) { throw new Exception('Could not find debug user "cms/test".'); } $status->start('Clearing tables'); // Mark transactions created by old system as deleted TransactionQuery::create()->filterByCreatorId(null, Criteria::NOT_EQUAL)->filterById(5814, Criteria::LESS_EQUAL)->filterByCreationdate(1347446069, Criteria::LESS_EQUAL)->update(array('Deleted' => 1), $con); // Clockings created by old system => max. clocking ID = 365913 // Transactions created by new system with old clockings // SELECT distinct min(tc.clocking_id) FROM transaction t join transaction_clocking tc on t.id=tc.transaction_id where tc.clocking_id<=365913 and ; // => min clocking ID /* // Delete clockings without new system transactions ClockingQuery::create() ->joinTransactionClocking() ->join('TransactionClocking.Transaction') ->filterByCreatorId(null, Criteria::NOT_EQUAL) ->add(TransactionPeer::ID, 5814, Criteria::LESS_EQUAL) ->add(TransactionPeer::CREATOR_ID, null, Criteria::NOT_EQUAL) ->update(array('Deleted' => 1), $con); */ // Mark clockings with new system transactions as deleted ClockingQuery::create()->filterById(365913, Criteria::LESS_EQUAL)->filterByCreatorId(null)->update(array('Deleted' => 1), $con); /* TransactionClockingQuery::create()->deleteAll($con); TransactionQuery::create()->deleteAll($con); ClockingQuery::create() ->filterByUserRelatedByUserId($debugUser, Criteria::NOT_EQUAL) ->delete($con); HolidayQuery::create()->deleteAll($con); UserQuery::create() ->filterByName('test', Criteria::NOT_EQUAL) ->delete($con); */ /* TransactionClockingQuery::create()->deleteAll(); TransactionQuery::create()->deleteAll(); ClockingQuery::create()->deleteAll(); DomainQuery::create()->deleteAll(); AccountQuery::create()->deleteAll(); $status->done(); $status->start('Create default Account'); $account = new Account(); $account->setName('Default account'); $account->save(); $status->done(); $status->start('Create default domain'); $domain = new Domain(); $domain->setName('default'); $domain->setDescription('Default domain created while migrating to the new system.'); $domain->setAccount($account); $domain->save(); $status->done(); */ $account = AccountQuery::create()->findOneByIdentifier('cms', $con); $domain = DomainQuery::create(); /* $status->start('Create holidays'); $holidaysUrl = \Xily\Config::get('migration.holidays_url', 'string', 'http://10.10.10.5/groupion/data/holidays'); $filenames = array('Bayern2009', 'Bayern2010', 'Bayern2011', 'Bayern2012', 'Bayern2013', 'Bayern2014'); foreach ($filenames as $filename) { $file = fopen($holidaysUrl.'/'.$filename.'.csv', 'r'); if ( !is_resource($file) ) throw new Exception('Could not open file'); while ( is_array($row = fgetcsv($file, 1000, ';')) ) { $date = strtotime($row[0]); $name = $row[1]; $state = $row[2]; if ( $date ) { $holidayDomain = new HolidayDomain(); $holidayDomain->setDomain($domain); $holiday = new Holiday(); $holiday ->setAccount($account) ->setDate($date) ->setName(trim($name)) ->addHolidayDomain($holidayDomain) ->save(); } } } $status->done(); $status->start('Migrating Users'); $this->importUsers($status, $account, $domain, $dbh, $con); $status->done(); */ $usersByName = $this->getUsers($account, $con); $clockingTypeMap = $this->getClockingTypes($account, $con); $status->start('Migrating Clockings'); $clockingDataByOldID = $this->importClockings($status, $clockingTypeMap, $usersByName, $dbh, $con); $status->done(); $bookingTypesByIdentifier = $this->getBookingTypes($account, $con); $status->start('Migrating Transactions'); $this->importTransactions($status, $clockingTypeMap, $bookingTypesByIdentifier, $usersByName, $clockingDataByOldID, $dbh, $con); $status->done(); echo '#INCONSISTENCIES: ' . $this->inconsistencies; $dbh = null; } catch (Exception $e) { echo 'Error: ' . nl2br(htmlspecialchars($e->getMessage())) . '<br/>'; $status->showNotes($e->getMessage()); die; } print '<p>Finished migration!</p></div>'; $status->showNotes(); }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Account is new, it will return * an empty collection; or if this Account has previously * been saved, it will retrieve related Domains from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Account. * * @param Criteria $criteria optional Criteria object to narrow the query * @param PropelPDO $con optional connection object * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return PropelObjectCollection|Domain[] List of Domain objects */ public function getDomainsJoinAddress($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN) { $query = DomainQuery::create(null, $criteria); $query->joinWith('Address', $join_behavior); return $this->getDomains($query, $con); }
/** * Get the associated Domain object * * @param PropelPDO $con Optional Connection object. * @param $doQuery Executes a query to get the object if required * @return Domain The associated Domain object. * @throws PropelException */ public function getDomain(PropelPDO $con = null, $doQuery = true) { if ($this->aDomain === null && $this->domain_id !== null && $doQuery) { $this->aDomain = DomainQuery::create()->findPk($this->domain_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aDomain->addHolidayDomains($this); */ } return $this->aDomain; }
/** * Lists users of a domain. * * For non-administrative users, the list will contain the authenticated user only. * * @param string $strSearch Search query * @param string $intDomain Filter by domain * @param bool $showDeleted Show deleted users * @param string $strOrderby OrderBy Column * @param string $strOrderMode Order mode (asc or desc) * @return array */ public function do_list($strSearch = false, $intDomain = false, $showDeleted = false, $strOrderby = 'Name', $strOrderMode = 'asc') { $user = $this->requireUser(); $account = $user->getAccount(); $query = UserQuery::create()->joinWith('Domain')->joinWith('Domain.Account')->add(AccountPeer::ID, $account->getId()); if (!$user->getIsAdmin()) { $query->filterById($user->getId()); } Search::addSearchCriteria($query, $strSearch, array(UserPeer::NAME, UserPeer::FIRSTNAME, UserPeer::LASTNAME, UserPeer::PHONE, UserPeer::EMAIL)); if ($intDomain) { if ($domain = DomainQuery::create()->findOneById($intDomain)) { $query->filterByDomain($domain); } else { throw new Exception('Domain ID "' . $intDomain . '" not found!'); } } if ($strOrderMode != 'asc') { $strOrderMode = 'desc'; } switch ($strOrderby) { case 'Firstname': $query->orderByFirstname($strOrderMode); break; case 'Lastname': $query->orderByLastname($strOrderMode); break; case 'Domain': $query->orderBy(DomainPeer::NAME, $strOrderMode); break; case 'Email': $query->orderByEmail($strOrderMode); break; default: // Name $query->orderByName($strOrderMode); break; } if (!$showDeleted) { $query->filterByDeleted(0); } $res = array(); foreach ($query->find() as $user) { $domain = $user->getDomain(); $item = EntityArray::from($user) + array('Domain' => array('Name' => $domain->getName(), 'Id' => $domain->getId())) + array('Account' => array('Name' => $domain->getAccount()->getName(), 'Id' => $domain->getAccountId())); $res[] = $item; } return $res; }
/** * Updates a domain * * @param int $intId * @param array $arrData * @return int The domain ID */ public function do_update($intId, $arrData) { $domain = null; $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } try { $user = $this->requireUser(); $account = $user->getAccount($con); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($arrData, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { $con->rollBack(); return array('result' => false, 'warnings' => $warnings); } $query = DomainQuery::create()->filterByAccount($account); if ($intId !== null) { $domain = DomainQuery::create()->filterByAccount($account)->findOneById($intId, $con); if ($domain === null) { throw new Exception('Domain not found; ID: ' . $intId); } $query->filterById($intId, Criteria::NOT_EQUAL); } else { $domain = new Domain(); $domain->setAccount($account); } // Check for duplicates if ($query->findOneByName($arrData['Name'], $con)) { throw new Exception($locale->insert('error.taken', array('value' => '"' . $arrData['Name'] . '"'))); } $domain->fromArray(array_intersect_key($arrData, array('AddressId' => true, 'Name' => true, 'Description' => true, 'Number' => true))); $domain->save($con); if (!empty($arrData['Properties'])) { $domain->setProperties($arrData['Properties'], $con); } } catch (Exception $e) { $con->rollBack(); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return $domain->getId(); }
/** * Gets another domain from the same account. * * @param int $domainId * @param PropelPDO $con Optional. The database connection to use. * Default is NULL. * @return Domain * @throws Exception */ public function getOtherDomain($domainId, PropelPDO $con = null) { $domain = DomainQuery::create()->findOneById($domainId, $con); if ($domain === null) { throw new Exception('Domain with ID ' . $domainId . ' not found!'); } $account = $this->getAccount($con); if ($account === null or $domain->getAccountId() != $account->getId()) { throw new Exception('The domain does not belong to our account!'); } return $domain; }
/** * Updates a holiday * * @param int $intId The holiday ID * @param array $arrData * @return int The holiday ID */ public function do_update($intId, $arrData) { $user = $this->requireUser(); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($arrData, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } if ($intId) { if (!($holiday = HolidayQuery::create()->findOneById($intId))) { throw new Exception('Holiday with ID ' . $intId . ' not found!'); } } else { $holiday = new Holiday(); } $con = Propel::getConnection(HolidayPeer::DATABASE_NAME); $con->beginTransaction(); try { $holiday->setName($arrData['Name'])->setDate($arrData['Date'])->setAccount($user->getAccount())->save($con); // Assign the domains if (!(isset($arrData['Domains']) && is_array($arrData['Domains']))) { $arrData['Domains'] = array(); } $sub = array(); foreach (HolidayDomainQuery::create()->filterByHoliday($holiday)->find() as $link) { if (in_array($link->getDomainId(), $arrData['Domains'])) { $sub[] = $link->getDomainId(); } else { $link->delete($con); } } $diff = array_diff($arrData['Domains'], $sub); if (sizeof($diff) > 0) { // Get the account's domains $domainFilter = DomainQuery::create()->filterByAccount($user->getAccount())->add(DomainPeer::ID, $arrData['Domains'], Criteria::IN)->find(); if (sizeof($domainFilter) != sizeof($arrData['Domains'])) { // Obviously there are some domains the user does not belong to } foreach (array_diff($arrData['Domains'], $sub) as $domainId) { $link = new HolidayDomain(); $link->setHoliday($holiday)->setDomainId($domainId)->save($con); } } $con->commit(); } catch (Exception $e) { $con->rollBack(); throw $e; } return array('result' => $holiday->getId(), 'test' => $diff); // return $holiday->getId(); }
/** * Removes this object from datastore and sets delete attribute. * * @param PropelPDO $con * @return void * @throws PropelException * @throws Exception * @see BaseObject::setDeleted() * @see BaseObject::isDeleted() */ public function delete(PropelPDO $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getConnection(DomainPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } $con->beginTransaction(); try { $deleteQuery = DomainQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }