Created
January 15, 2014 09:58
-
-
Save asika32764/8433670 to your computer and use it in GitHub Desktop.
Facility & Type mapping helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Part of crm project. | |
* | |
* @copyright Copyright (C) 2011 - 2014 SMS Taiwan, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
/** | |
* Class IcrmHelperType | |
* | |
* @since 1.0 | |
*/ | |
class IcrmHelperType | |
{ | |
const RELATION_TABLE = '#__icrm_facility_type'; | |
/** | |
* getFacilityTypes | |
* | |
* @param int $fid | |
* | |
* @return mixed | |
*/ | |
public static function getFacilityTypes($fid) | |
{ | |
if (!$fid) | |
{ | |
return array(); | |
} | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true); | |
$query->select('type_id') | |
->from(static::RELATION_TABLE) | |
->where('facility_id = ' . $fid); | |
$data = $db->setQuery($query)->loadColumn(); | |
if ($data) | |
{ | |
return $data; | |
} | |
else | |
{ | |
return array(); | |
} | |
} | |
/** | |
* updateFacilityTypes | |
* | |
* @param int $fid | |
* @param int[] $tids | |
* | |
* @return bool | |
*/ | |
public static function updateFacilityTypes($fid, $tids = array()) | |
{ | |
if (empty($tids)) | |
{ | |
return true; | |
} | |
$app = JFactory::getApplication(); | |
if (!static::deleteTypes($fid)) | |
{ | |
$app->enqueueMessage('更新類型時,刪除關連失敗', 'error'); | |
return false; | |
} | |
if (!static::addTypes($fid, $tids)) | |
{ | |
$app->enqueueMessage('更新類型時,加入關連失敗', 'error'); | |
return false; | |
} | |
return true; | |
} | |
/** | |
* deleteTypes | |
* | |
* @param int $fid | |
* | |
* @return bool | |
*/ | |
public static function deleteTypes($fid) | |
{ | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true); | |
$query->delete(static::RELATION_TABLE) | |
->where('facility_id = ' . $fid); | |
if (!$db->setQuery($query)->execute()) | |
{ | |
return false; | |
} | |
return true; | |
} | |
/** | |
* addTypes | |
* | |
* @param int $fid | |
* @param int[] $tids | |
* | |
* @return bool | |
*/ | |
public static function addTypes($fid, $tids) | |
{ | |
$tids = array_unique($tids); | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true); | |
$query->insert(static::RELATION_TABLE) | |
->columns(array('facility_id', 'type_id')); | |
array_map( | |
function($tid) use($query, $fid) | |
{ | |
$query->values($fid . ', ' . $tid); | |
}, | |
$tids | |
); | |
if (!$db->setQuery($query)->execute()) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment