mcramer
2013-08-09 81e1f83025cfbdaabdf3a8fbb6126b5a372ac3cc
interface/lib/classes/client_templates.inc.php
@@ -9,6 +9,61 @@
 
class client_templates {
    /**
     *  - check for old-style templates and change to new style
     *  - update assigned templates
     */
   function update_client_templates($clientId, $templates = array()) {
        global $app, $conf;
        if(!is_array($templates)) return false;
        $new_tpl = array();
        $used_assigned = array();
        foreach($templates as $item) {
            $item = trim($item);
            if($item == '') continue;
            $tpl_id = 0;
            $assigned_id = 0;
            if(strpos($item, ':') === false) {
                $tpl_id = $item;
            } else {
                list($assigned_id, $tpl_id) = explode(':', $item, 2);
                if(substr($assigned_id, 0, 1) === 'n') $assigned_id = 0; // newly inserted items
            }
            if($assigned_id > 0) {
                $used_assigned[] = $assigned_id; // for comparison with database
            } else {
                $new_tpl[] = $tpl_id;
            }
        }
        $in_db = $app->db->queryAllRecords('SELECT `assigned_template_id`, `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ' . $clientId);
        if(is_array($in_db) && count($in_db) > 0) {
            // check which templates were removed from this client
            foreach($in_db as $item) {
                if(in_array($item['assigned_template_id'], $used_assigned) == false) {
                    // delete this one
                    $app->db->query('DELETE FROM `client_template_assigned` WHERE `assigned_template_id` = ' . $item['assigned_template_id']);
                }
            }
        }
        if(count($new_tpl) > 0) {
            foreach($new_tpl as $item) {
                // add new template to client (includes those from old-style without assigned_template_id)
                $app->db->query('INSERT INTO `client_template_assigned` (`client_id`, `client_template_id`) VALUES (' . $clientId . ', ' . $item . ')');
            }
        }
        unset($new_tpl);
        unset($in_db);
        unset($templates);
        unset($used_assigned);
        return true;
    }
   function apply_client_templates($clientId) {
        global $app;
        
@@ -20,7 +75,12 @@
        $sql = "SELECT template_master, template_additional FROM client WHERE client_id = " . $app->functions->intval($clientId);
        $record = $app->db->queryOneRecord($sql);
        $masterTemplateId = $record['template_master'];
        $additionalTemplateStr = $record['template_additional'];
        if($record['template_additional'] != '') {
            // we have to call the update_client_templates function
            $templates = explode('/', $record['template_additional']);
            $this->update_client_templates($clientId, $templates);
        }
        /*
         * if the master-Template is custom there is NO changing
@@ -40,8 +100,9 @@
         * if != -1)
         */
        $addTpl = explode('/', $additionalTemplateStr);
        foreach ($addTpl as $item){
            if (trim($item) != ''){
        $addTpls = $app->db->queryAllRecords('SELECT `client_template_id` FROM `client_template_assigned` WHERE `client_id` = ' . $app->functions->intval($clientId));
        foreach ($addTpls as $addTpl){
            $item = $addTpl['client_template_id'];
                $sql = "SELECT * FROM client_template WHERE template_id = " . $app->functions->intval($item);
                $addLimits = $app->db->queryOneRecord($sql);
                $app->log('Template processing subtemplate ' . $item . ' for client ' . $clientId, LOGLEVEL_DEBUG);
@@ -115,7 +176,6 @@
                                    /* choose the lower index of the two SELECT items */
                                    $limits[$k] = $limit_values[min(array_search($limits[$k], $limit_values), array_search($v, $limit_values))];
                                break;
                                }
                            }
                        }
                    }