1<?php
2/* $Id: crm.class.php,v 1.26 2004/05/18 10:39:58 mouzaia Exp $ */
3
4class CRM {
5	var $templatesAreIn="";
6
7    function opCreate(){
8	global $conn;
9
10	$id = mgw_genID('mgw__seq_contacts_history');
11	$company = isset($_POST["company"])?(int) $_POST["company"]:0;
12	$person = $conn->QMagic(isset($_POST["person"])?$_POST["person"]:"");
13	$date = $conn->DBTimeStamp($_POST["dateYear"]."-".$_POST["dateMonth"]."-".$_POST["dateDay"]." ".$_POST["dateHour"].":".$_POST["dateMinute"]);
14	$userid = (int) $_POST["userid"];
15	$todo = $conn->QMagic(isset($_POST["todo"])?$_POST["todo"]:"");
16	$note = $conn->QMagic(isset($_POST["note"])?$_POST["note"]:"");
17
18	queryRights2("root_modules_contact_crm_create");
19
20    $sql = "select id from mgw_companies where (ispublic=1 or owner=".$_SESSION["MGW"]->userid.") and id=".(int)$company;
21    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
22
23    if ($res->RecordCount()==1) {
24	  $sql = "INSERT INTO mgw_contacts_history (contactid, contact_company, contact_person, contact_date, contact_userid, contact_todo, contact_comment, lu_user, lu_date) VALUES ($id, $company, $person, $date, $userid, $todo, $note, ".$_SESSION["MGW"]->userid.", ".$conn->DBTimeStamp(time()).")";
25	  if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
26    }
27
28	return true;
29    }
30
31    function opUpdate($id){
32	global $conn;
33
34	$company = isset($_POST["company"])?(int) $_POST["company"]:0;
35	$person = $conn->QMagic(isset($_POST["person"])?$_POST["person"]:"");
36	$date = $conn->DBTimeStamp($_POST["dateYear"]."-".$_POST["dateMonth"]."-".$_POST["dateDay"]." ".$_POST["dateHour"].":".$_POST["dateMinute"]);
37	$userid = (int) $_POST["userid"];
38	$todo = $conn->QMagic(isset($_POST["todo"])?$_POST["todo"]:"");
39	$note = $conn->QMagic(isset($_POST["note"])?$_POST["note"]:"");
40
41	queryRights2("root_modules_contact_crm_modify");
42
43    $sql = "select id from mgw_companies where (ispublic=1 or owner=".$_SESSION["MGW"]->userid.") and id=".(int)$company;
44    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
45
46    if ($res->RecordCount()==1) {
47  	  $sql="UPDATE mgw_contacts_history SET contact_company=$company, contact_person=$person, contact_date=$date, contact_userid=$userid, contact_todo=$todo, contact_comment=$note, lu_user=".$_SESSION["MGW"]->userid.", lu_date=".$conn->DBTimeStamp(time())." WHERE contactid = ".(int)$id;
48	  if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
49	}
50
51	return true;
52    }
53
54    function opDelete($id){
55	global $conn;
56
57	queryRights2("root_modules_contact_crm_delete");
58
59    $sql = "select id from mgw_companies a, mgw_contacts_history b where (a.ispublic=1 or a.owner=".$_SESSION["MGW"]->userid.") and a.id=b.contact_company and b.contactid=".(int)$id;
60    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
61
62    if ($res->RecordCount()==1) {
63  	  $sql = "DELETE FROM mgw_contacts_history WHERE contactid=".(int)$id;
64	  if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
65    }
66	return true;
67    }
68
69    function viewDelete($id){
70	global $smarty, $conn, $appconf;
71	queryRights2("root_modules_contact_crm_delete");
72
73	$sql = "SELECT a.*, b.name1 FROM mgw_contacts_history a LEFT JOIN mgw_companies b ON a.contact_company = b.id WHERE (b.ispublic=1 or b.owner=".$_SESSION["MGW"]->userid.") and contactid=".(int)$id;
74	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
75	$row = $res->FetchRow();
76
77	$smarty->assign("menuid", "4");
78	$smarty->assign("itemid", $id);
79	$smarty->assign("parms", str_replace("delete","list",$_SERVER["QUERY_STRING"]));
80	$smarty->assign("reallydeleteitem", Lang::getLanguageString("reallydelcontact"));
81	$smarty->assign("deleteheader", Lang::getLanguageString("deletecontacth"));
82	$smarty->assign("deletedetail1", $row["name1"]);
83	$smarty->assign("deletedetail2", $row["contact_date"]);
84	$smarty->assign("deletedetail3", $row["contact_person"]);
85	$smarty->assign("suclass", "submit");
86
87	return true;
88    }
89
90    /* Like viewNewForm, but will be directly called from contact list
91       and has company prefilled, person maybe prefilled.
92     */
93    function viewDirectNewForm()
94    {
95	global $smarty, $conn;
96	queryRights2("root_modules_contact_crm_create");
97	$compid = isset($_GET["compid"])?(int) $_GET["compid"] : 0;
98	$person = isset($_GET["personid"])?(int) $_GET["personid"] : 0;
99	$concat = "title,honorific_prefix,firstname,lastname";
100	$sql = "SELECT ".$concat." AS contact_person FROM mgw_contacts WHERE id = ".$person;
101	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
102	if($row = $res->FetchRow()) {
103	    $row2 = array();
104	    $row['title'] = Lang::getLanguageString($row["title"]);
105	    $row2["contact_person"] = implode(" ",$row);
106	    $smarty->assign("td",$row2);
107	}
108
109	include(INCLUDEPATH . "mSelectBox.class.php");
110	$sb1 = new mSelectBox("userid");
111	$sb1->setContentType("sql");
112	$sb1->setSQLvalue("id");
113	$sb1->setSQLcontent("name");
114	$concat=$conn->Concat("lastname", "', '", "firstname");
115	$sb1->addContent("SELECT *, $concat AS name FROM mgw_users ORDER BY lastname, firstname");
116	$smarty->assign("usersb", $sb1->draw($_SESSION["MGW"]->userid));
117
118	$sb2 = new mSelectBox("company");
119	$sb2->setContentType("sql");
120	$sb2->setSQLvalue("id");
121	$sb2->setSQLcontent("name1");
122	$sb2->addContent("SELECT * FROM mgw_companies WHERE (ispublic=1 or owner=".$_SESSION["MGW"]->userid.") ORDER BY name1");
123	$sb2->addOption("0", Lang::getLanguageString("chosecompanytext"), "pre");
124	$smarty->assign("companysb", $sb2->draw($compid));
125
126	$smarty->assign("suname", "create");
127	$smarty->assign("suvalue", Lang::getLanguageString("create"));
128	$smarty->assign("suclass", "submit");
129	$smarty->assign("mode", "direct");
130	$smarty->assign("contentheader", Lang::getLanguageString("createnewcontact"));
131
132	return true;
133    }
134
135    function viewNewForm(){
136	global $smarty, $conn;
137	queryRights2("root_modules_contact_crm_create");
138	include(INCLUDEPATH . "mSelectBox.class.php");
139	$sb1 = new mSelectBox("userid");
140	$sb1->setContentType("sql");
141	$sb1->setSQLvalue("id");
142	$sb1->setSQLcontent("name");
143	$concat=$conn->Concat("lastname", "', '", "firstname");
144	$sb1->addContent("SELECT *, $concat AS name FROM mgw_users ORDER BY lastname, firstname");
145	$smarty->assign("usersb", $sb1->draw());
146
147	$sb2 = new mSelectBox("company");
148	$sb2->setContentType("sql");
149	$sb2->setSQLvalue("id");
150	$sb2->setSQLcontent("name1");
151	$sb2->addContent("SELECT * FROM mgw_companies WHERE (ispublic=1 or owner=".$_SESSION["MGW"]->userid.") ORDER BY name1");
152	$sb2->addOption("0", Lang::getLanguageString("chosecompanytext"), "pre");
153	$smarty->assign("companysb", $sb2->draw());
154
155	$smarty->assign("suname", "create");
156	$smarty->assign("suvalue", Lang::getLanguageString("create"));
157	$smarty->assign("suclass", "submit");
158	$smarty->assign("mode", "new");
159	$smarty->assign("contentheader", Lang::getLanguageString("createnewcontact"));
160
161	return true;
162    }
163
164    function viewEditForm($id){
165	global $conn, $smarty;
166	queryRights2("root_modules_contact_crm_modify");
167	include(INCLUDEPATH . "mSelectBox.class.php");
168
169	$backid = isset($_GET["backid"])?(int) $_GET["backid"]:0;
170
171	$sql = "SELECT a.* FROM mgw_contacts_history a, mgw_companies b WHERE (b.ispublic=1 or b.owner=".$_SESSION["MGW"]->userid.") and a.contactid=".(int)$id;
172	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
173	$row = $res->FetchRow();
174
175	$sb1 = new mSelectBox("userid");
176	$sb1->setContentType("sql");
177	$sb1->setSQLvalue("id");
178	$sb1->setSQLcontent("name");
179	$concat=$conn->Concat("lastname", "', '", "firstname");
180	$sb1->addContent("SELECT *, $concat AS name FROM mgw_users ORDER BY lastname, firstname");
181	$smarty->assign("usersb", $sb1->draw($row["contact_userid"]));
182
183	$sb2 = new mSelectBox("company");
184	$sb2->setContentType("sql");
185	$sb2->setSQLvalue("id");
186	$sb2->setSQLcontent("name1");
187	$sb2->addContent("SELECT * FROM mgw_companies WHERE (ispublic=1 or owner=".$_SESSION["MGW"]->userid.") ORDER BY name1");
188	$sb2->addOption("0", Lang::getLanguageString("chosecompanytext"), "pre");
189	$smarty->assign("companysb", $sb2->draw($row["contact_company"]));
190
191	$smarty->assign("td", $row);
192	$smarty->assign("id",$id);
193	$smarty->assign("endyear", date("Y")+1);
194	$smarty->assign("backid",$backid);
195	$smarty->assign("suname", "edit");
196	$smarty->assign("suvalue", Lang::getLanguageString("edit"));
197	$smarty->assign("suclass", "submit");
198	$smarty->assign("contentheader", Lang::getLanguageString("editcontact"));
199
200	return true;
201    }
202
203    function viewDetails($id){
204	global $conn, $smarty;
205
206	$concat = $conn->Concat("lastname","', '","firstname");
207	$sql = "SELECT name1, contact_company, contact_person, contact_date, contactid, ".$concat." AS contact_username, contact_todo, contact_comment FROM mgw_contacts_history a LEFT OUTER JOIN mgw_users c ON a.contact_userid = c.id LEFT OUTER JOIN mgw_companies d ON a.contact_company = d.id WHERE (d.ispublic=1 or d.owner=".$_SESSION["MGW"]->userid.") and a.contactid = ".(int)$id;
208
209	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
210	$row = $res->FetchRow();
211
212	$row['contact_date'] = $conn->UnixDate($row['contact_date']);
213	$row["contact_todo"] = nl2br($row["contact_todo"]);
214	$row["contact_comment"] = nl2br($row["contact_comment"]);
215
216	$smarty->assign("td", $row);
217
218	return true;
219    }
220
221
222    function viewDetailsList($id){
223	global $conn, $smarty;
224
225	$icons = getIconStrings();
226	$concat = $conn->Concat("c.lastname", "', '", "c.firstname");
227	$sql = "SELECT d.owner, d.name1, b.contact_person, b.contact_date, b.contactid, ".$concat." AS contact_username FROM mgw_contacts_history a, mgw_contacts_history b LEFT OUTER JOIN mgw_users c ON b.contact_userid = c.id LEFT OUTER JOIN mgw_companies d ON d.id = b.contact_company WHERE (d.ispublic=1 or d.owner=".$_SESSION["MGW"]->userid.") and a.contactid = ".(int)$id." AND a.contact_company = b.contact_company ORDER BY b.contact_date DESC";
228
229	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
230
231	while($row = $res->FetchRow()){
232	    $row['contact_date'] = $conn->UnixDate($row['contact_date']);
233	    $smarty->append("td", $row);
234	    $id = $row["contactid"];
235	    $smarty->assign("detail_overview", Lang::getLanguageString("detail_view") . " " . $row["name1"]);
236
237	    $detlink = "<a href=\"javascript:openSelector($id, '".SID."')\" class=\"edit\">".$icons["details"]."</a>";
238	    // modify only for user with appropriate rights
239	    if(queryRights2_bool("root_modules_contact_crm_modify")){
240		$editlink = "<a href=\"index.php?mod=contact&amp;obj=crm&amp;view=editform&amp;id=$id&amp;backid=$id&amp;".SID."\" class=\"edit\">".$icons["edit"]."</a>";
241	    }
242	    else {
243		$editlink = "";
244	    }
245
246	    // delete only for user with appropriate rights
247	    if(queryRights2_bool("root_modules_contact_crm_delete")){
248		$dellink = "<a href=\"index.php?mod=contact&amp;obj=crm&amp;view=delete&amp;id=$id&amp;backid=$id&amp;".SID."\" class=\"delete\">".$icons["delete"]."</a>";
249	    }
250	    else {
251		$dellink = "";
252	    }
253
254	    $urls[] = $detlink . $icons["space"] . $editlink . $icons["space"] . $dellink;
255
256	    $smarty->assign('urls', $urls);
257	}
258
259	$smarty->assign("hascreaterights", queryRights2_bool("root_modules_contact_crm_create"));
260	$smarty->assign('at', alphabet_table("mod=contact&amp;obj=crm&amp;sort=alpha&amp;view=list"));
261    }
262
263    function viewList(){
264	global $conn, $smarty;
265
266	$icons = getIconStrings();
267
268	$sql0 = "SELECT contact_company, MAX(contact_date) AS ldate FROM mgw_contacts_history GROUP BY contact_company";
269	if(!$res0 = $conn->Execute($sql0)) exit(showSQLerror($sql0));
270
271	if(isset($_GET["sort"]) and $_GET["sort"] == "alpha") {
272	    reset($res0);
273	    $list = $conn->QMagic($_GET['list']."%");
274	    $where = " (";
275	    while ($row0=$res0->FetchRow()) $where .= "( contact_company  = ".$row0["contact_company"]." AND contact_date = '".$row0["ldate"]."' AND c.name1 LIKE $list) OR ";
276	    $where .= " 1=0)";
277	}
278
279	elseif(isset($_POST["op"]) and $_POST["op"] == "qsearch") {
280	    reset($res0);
281	    $where = " (";
282	    $searchterm = $conn->QMagic("%".$_POST["searchterm"]."%");
283	    while ($row0=$res0->FetchRow()){
284		$where .= "( contact_company  = ".$row0["contact_company"]." AND contact_date = '".$row0["ldate"]."' AND (c.name1 LIKE ".$searchterm." OR c.name2 LIKE ".$searchterm.")) OR ";
285	    }
286	    $where .= "1=0)";
287	}
288	else{
289	    $where = " (";
290	    while ($row0 = $res0->FetchRow()) {
291		$where .= "( contact_company = '".$row0["contact_company"]."' AND contact_date = '".$row0["ldate"]."') OR ";
292	    }
293	    $where .= "1=0)";
294	}
295
296	$concat = $conn->Concat("b.lastname","', '","b.firstname");
297	$sql2 = "SELECT c.owner, c.name1, c.id, contact_date, contact_person, contactid, ".$concat." AS last_contact_internal FROM mgw_contacts_history a LEFT OUTER JOIN mgw_users b ON a.contact_userid = b.id LEFT OUTER JOIN mgw_companies c ON a.contact_company = c.id WHERE (c.ispublic=1 or c.owner=".$_SESSION["MGW"]->userid.") and ".$where;
298
299	if(!$res2 = $conn->Execute($sql2)) exit(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
300
301	while($row = $res2->FetchRow()){
302	    $row['contact_date'] = $conn->UnixDate($row['contact_date']);
303
304	    $smarty->append("td", $row);
305	    $id = $row["contactid"];
306
307	    // aktion urls zusammenbauen
308	    $detlink = "<a href=\"index.php?mod=contact&amp;obj=crm&amp;view=detail_list&amp;id=$id&amp;".SID."\" class=\"edit\">".$icons["details"]."</a>";
309
310	    // modify only for user with appropriate rights
311	    if(queryRights2_bool("root_modules_contact_crm_modify")){
312		$editlink = "<a href=\"index.php?mod=contact&amp;obj=crm&amp;view=editform&amp;id=$id&amp;".SID."\" class=\"edit\">".$icons["edit"]."</a>";
313	    }
314	    else {
315		$editlink = "";
316	    }
317
318	    // delete only for user with appropriate rights
319	    if(queryRights2_bool("root_modules_contact_crm_delete")){
320		$dellink = "<a href=\"index.php?mod=contact&amp;obj=crm&amp;view=delete&amp;id=$id&amp;".SID."\" class=\"delete\">".$icons["delete"]."</a>";
321	    }
322	    else {
323		$dellink = "";
324	    }
325
326	    $urls[] = $detlink . $icons["space"] . $editlink . $icons["space"] . $dellink;
327	    $smarty->assign('urls', $urls);
328	}
329
330	$smarty->assign("hascreaterights", queryRights2_bool("root_modules_contact_crm_create"));
331	$smarty->assign('at', alphabet_table("mod=contact&amp;obj=crm&amp;sort=alpha&amp;view=list"));
332	$smarty->assign('suclass', "submit");
333
334	return true;
335    }
336
337	function display($template) {
338		global $smarty,$appconf,$myEnv;
339		if (strstr($template,$appconf["gentemplates"])) {
340
341		} elseif (!empty($this->templatesAreIn)) {
342		    $smarty->template_dir=ROOTPATH ."/modules/" . $this->templatesAreIn . "/templates/" .$appconf["theme"] ."/" . $myEnv["output"];
343		}
344		$smarty->display($template);
345	}
346
347}
348?>
349